2 次代碼提交 f1145ff9ef ... 2b6c718ea4

作者 SHA1 備註 提交日期
  cheny 2b6c718ea4 Merge branch 'master' of http://git.cc-lotus.info/service-platform/service-market 4 年之前
  cheny de8343c416 123 4 年之前
共有 5 個文件被更改,包括 205 次插入2 次删除
  1. 4 0
      app/controller/product.js
  2. 3 0
      app/router.js
  3. 73 1
      app/service/product.js
  4. 120 0
      app/utils/utils.js
  5. 5 1
      package.json

+ 4 - 0
app/controller/product.js

@@ -27,6 +27,10 @@ class ProductController extends Controller {
     const data = await this.service.allquery(this.ctx.query);
     this.ctx.ok({ data });
   }
+  // 导出测试
+  async exportexcel(){
+    const data = await this.service.exportexcel(this.ctx.request.body);
+  }
 }
 
 module.exports = CrudController(ProductController, meta);

+ 3 - 0
app/router.js

@@ -29,6 +29,9 @@ module.exports = app => {
   router.get('product', '/api/market/product/newfetch/:id', controller.product.newfetch);
   router.resources('product', '/api/market/product', controller.product); // index、create、show、destroy
   router.post('product', '/api/market/product/update/:id', controller.product.update);
+  
+  // 导出测试
+  router.post('product', '/api/market/product/exportexcel', controller.product.exportexcel);
 
   // 产品类型表设置路由
   router.resources('protype', '/api/market/protype', controller.protype); // index、create、show、destroy

+ 73 - 1
app/service/product.js

@@ -5,7 +5,8 @@ const assert = require('assert');
 const _ = require('lodash');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
-
+const XLSX = require('xlsx');
+var utils = require('../utils/utils.js');
 
 class ProductService extends CrudService {
   constructor(ctx) {
@@ -79,6 +80,77 @@ class ProductService extends CrudService {
     }
     return data;
   }
+  // 导出测试
+  async exportexcel({totaltype}){
+    let productList = await this.model.find({ totaltype });
+    for(let product of productList){
+      product.totaltype = utils.getName("totaltype",product.totaltype);
+      product.phase = utils.getName("phase",product.phase);
+      product.priceunit = utils.getName("priceunit",product.priceunit);
+      product.field = utils.getName("field",product.field);
+      product.coopermode = utils.getName("coopermode",product.coopermode);
+      product.business = utils.getName("business",product.business);
+      product.mature = utils.getName("mature",product.mature);
+    }
+    // 需要打出的列表
+    const _headers = [
+      { key: 'totaltype', title: '分类' },
+      { key: 'name', title: '名称' },
+      { key: 'product_type_name', title: '类型名称' },
+      { key: 'introduction', title: '简介' },
+      { key: 'phase', title: '研发阶段' },
+      { key: 'price', title: '单价' },
+      { key: 'priceunit', title: '单位' },
+      { key: 'field', title: '所属领域' },
+      { key: 'scope', title: '服务范围' },
+      { key: 'coopermode', title: '合作方式' },
+      { key: 'business', title: '交易方式' },
+      { key: 'budget', title: '投入预算' },
+      { key: 'end_date', title: '需求截止日期' },
+      { key: 'difficult_problem', title: '难题及瓶颈问题' },
+      { key: 'demand', title: '企业解决需求' },
+      { key: 'company', title: '单位名称' },
+      { key: 'address', title: '单位地址' },
+      { key: 'team', title: '技术团队情况' },
+      { key: 'property', title: '知识产权情况' },
+      { key: 'mature', title: '技术成熟度' },
+      { key: 'coopercompany', title: '合作企业' },
+      { key: 'other', title: '其他需求' },
+      { key: 'contact_user', title: '联系人' },
+      { key: 'contact_tel', title: '联系电话' }
+  ];
+  
+  const _data = productList;
+  
+  const headers = _headers.map(({ title }) => title)
+      .map((v, i) => Object.assign({}, { v: v, position: String.fromCharCode(65 + i) + 1 }))
+      .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
+  
+  const data = _data
+      .map((v, i) => _headers.map(({ key }, j) => Object.assign({}, { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) })))
+      .reduce((prev, next) => prev.concat(next))
+      .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {});
+  
+  // 合并 headers 和 data
+  const output = Object.assign({}, headers, data);
+  
+  // 获取所有单元格的位置
+  const outputPos = Object.keys(output);
+  
+  // 计算出范围
+  const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
+  
+  // 构建 workbook 对象
+  const wb = {
+      SheetNames: ['sheet1'],
+      Sheets: {
+          'sheet1': Object.assign({}, output, { '!ref': ref })
+      }
+  };
+  console.log(new Date().getTime());
+  // 导出 Excel
+  XLSX.writeFile(wb, 'D:/'+new Date().getTime()+'.xlsx');
+  }
 }
 
 module.exports = ProductService;

+ 120 - 0
app/utils/utils.js

@@ -0,0 +1,120 @@
+'use strict';
+
+
+exports.getName = function(type,value) {
+  let name = ""
+  switch(type){
+    case "totaltype":
+      switch(value){
+        case "0":
+        name = '技术'
+        break;
+        case "1":
+        name = '产品'
+        break;
+        case "2":
+        name = '服务'
+        break;
+      }
+    break;
+
+    case "phase":
+      switch(value){
+        case "0":
+        name = '阶段成果'
+        break;
+        case "1":
+        name = '最终成果'
+        break;
+      }
+    break;
+
+    case "priceunit":
+      switch(value){
+        case "0":
+        name = '公斤'
+        break;
+        case "1":
+        name = '套'
+        break;
+        case "2":
+        name = '件'
+        break;
+      }
+    break;
+
+    case "field":
+      switch(value){
+        case "0":
+        name = '先进制造'
+        break;
+        case "1":
+        name = '新材料'
+        break;
+        case "2":
+        name = '光电子'
+        break;
+        case "3":
+        name = '信息技术'
+        break;
+        case "4":
+        name = '文化和科技融合'
+        break;
+      }
+    break;
+
+    case "coopermode":
+      switch(value){
+        case "0":
+        name = '技术转让'
+        break;
+        case "1":
+        name = '技术服务'
+        break;
+        case "2":
+        name = '技术许可'
+        break;
+        case "3":
+        name = '信息技术'
+        break;
+        case "4":
+        name = '文化和科技融合'
+        break;
+      }
+    break;
+
+    case "business":
+      switch(value){
+        case "0":
+        name = '公用'
+        break;
+        case "1":
+        name = '转让'
+        break;
+        case "2":
+        name = '竞价'
+        break;
+      }
+    break;
+
+    case "mature":
+      switch(value){
+        case "0":
+        name = '实验室'
+        break;
+        case "1":
+        name = '小试'
+        break;
+        case "2":
+        name = '中试'
+        break;
+        case "3":
+        name = '成熟'
+        break;
+      }
+    break;
+
+  }
+
+  return name;
+};

+ 5 - 1
package.json

@@ -9,9 +9,13 @@
   "dependencies": {
     "egg": "^2.15.1",
     "egg-scripts": "^2.11.0",
+    "excel-export": "^0.5.1",
+    "fs": "0.0.1-security",
     "lodash": "^4.17.15",
+    "naf-framework-mongoose": "^0.6.11",
+    "sequelize": "^6.3.3",
     "silly-datetime": "^0.1.2",
-    "naf-framework-mongoose": "^0.6.11"
+    "xlsx": "^0.15.6"
   },
   "devDependencies": {
     "autod": "^3.0.1",