output-controller.js
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const Context = require('../framework/context');
const nodeExcel = require('excel-export');
const ExportExcelService = require('../service/export-excel-service');
const _ = require('lodash');
const exportExcelConfs = require('../service/excel-conf/exprot-excel-conf');
const config = global.yoho.config;
const moment = require('moment');
// 暂时弃用
class OutputController extends Context {
constructor() {
super();
this.exportExcelService = this.instance(ExportExcelService);
}
productList(req, res) {
const conf = {
name: 'mysheet',
cols: [
{
caption: '列1',
type: 'string',
},
{
caption: '列2',
type: 'string',
},
],
rows: [
['1', '2'],
['3', '2'],
['4', '2'],
],
};
const result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=productList.xlsx');
res.end(result, 'binary');
}
/**
* 导出
* @param req
* @param res
*/
exportExcel(req, res) {
//获取导出表格的配置文件
const { apiUrl, conf } = exportExcelConfs[req.query.excelConf];
const searchParams = this.createSearchParams(req);
//请求服务数据
this.exportExcelService.getExportData(apiUrl, searchParams).then(result => {
const totalPage = result.totalPage || 0;
if (totalPage < 1) {
res.end('', 'binary');
}
const apis = [];
for (let i = 1; i <= totalPage; i++) {
searchParams.pageNo = i;
apis.push(this.exportExcelService.getExportData(apiUrl, searchParams));
}
Promise.all(apis).then(result => {
//获取结果集中的records
const excel = this.createExcelData(result, conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=download.xlsx');
res.end(excel, 'binary');
});
});
}
/**
* 组织查询数据
* @param req
*/
createSearchParams(req) {
//获取导出配置
const currentShop = _.find(req.user.shops, shop => shop.shopsId === _.parseInt(req.cookies._sign));
let baseParams = {};
if (currentShop) {
baseParams = {
pid: req.user.uid,
shopsId: currentShop.shopsId,
shopId: currentShop.shopsId,
shop: currentShop.shopsId,
supplierId: currentShop.shopsBrands.length
? req.user.supplier_id
? req.user.supplier_id
: _.first(currentShop.shopsBrands).supplierId
: 0,
platform_id: config.platform,
};
}
return { ...req.query, ...baseParams };
}
/**
* 组织表格要用的数据
* @param columnFields
* @param rows
*/
createExcelData(dataList, config) {
const { cols, rows } = config;
const conf = { ...{}, cols, rows: [] };
const list = _.flattenDeep(_.map(dataList, 'records'));
//组织导出数据
_.forEach(list, item => {
//组织导出的每一列数据
const excelRow = [];
_.forEach(rows, row => {
if (!item.hasOwnProperty(row)) {
excelRow.push(String(''));
return true;
}
//如果导出的列是时间,则格式化时间
if ((row.indexOf('time') > -1 || row.indexOf('Time') > -1) && item[row] > 0) {
excelRow.push(String(moment(item[row] * 1000).format('YYYY-MM-DD HH:mm:ss')));
return true;
}
if (_.isNaN(item[row])) {
excelRow.push(String(item[row]));
return true;
}
excelRow.push(item[row]);
});
conf.rows.push(excelRow);
});
return nodeExcel.execute(conf);
}
}
module.exports = OutputController;