|
|
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',
|
...
|
...
|
@@ -19,7 +26,11 @@ class OutputController extends Context { |
|
|
type: 'string',
|
|
|
},
|
|
|
],
|
|
|
rows: [['1', '2'], ['3', '2'], ['4', '2']],
|
|
|
rows: [
|
|
|
['1', '2'],
|
|
|
['3', '2'],
|
|
|
['4', '2'],
|
|
|
],
|
|
|
};
|
|
|
const result = nodeExcel.execute(conf);
|
|
|
|
...
|
...
|
@@ -27,6 +38,95 @@ class OutputController extends Context { |
|
|
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; |
...
|
...
|
|