Authored by lzhy

增加导出功能

... ... @@ -60,7 +60,7 @@ import _ from 'lodash';
import moment from 'moment';
import qs from 'querystringify';
import FinanceService from 'services/finance/finance-service';
import baseExportApi from 'util/excel';
export default {
data() {
return {
... ... @@ -292,8 +292,10 @@ export default {
},
//导出列表
exportList() {
const queryString = qs.stringify(this.filterValues, true);
const href = `${FinanceService.exportSettlementDetail}${queryString}`;
this.enableFilter = true;
const queryString = { ...this.filterValues(), excelConf: 'shopBill' };
const params = qs.stringify(queryString, true);
const href = `${baseExportApi}${params}`;
window.open(href, '_blank');
},
},
... ...
... ... @@ -175,10 +175,17 @@ class FinanceService extends Service {
shopBillList(params) {
return this.post(apiUrl.queryShopBillList, params);
}
/**
* 导出交易账务明细
* @param params
*/
exportBillList(params) {
return this.post('/export/excel', params);
}
}
FinanceService.exportBalanceList = '/Api/erp/exportBalanceList'; // 导出对账单列表, 结算单列表
FinanceService.exportBalanceDetail = '/Api/erp/exportBalanceDetail'; // 导出对账单详情
FinanceService.exportSettlementDetail = '/Api/erp/exportSettlementDetail'; // 导出结算单详情
FinanceService.exportInventory = '/Api/erp/exportInventoryLedgerList'; // 导出结算单库存
export default FinanceService;
... ...
const baseExportApi = '/Api/export/excel';
export default baseExportApi;
... ...
... ... @@ -15,6 +15,7 @@ const FileController = require('./file-controller');
const ImportController = require('./import-controller');
const CaptchaController = require('./captcha-controller');
const GeeCaptchaController = require('./gee-captcha-controller');
const OutputController = require('./output-controller');
let router = Express.Router(); // eslint-disable-line
... ... @@ -23,6 +24,7 @@ router.post('/logout', middleware(UserController, 'logout'));
router.post('/switchShop', before, auth, middleware(UserController, 'switchShop'));
router.post('/upload/image', before, auth, middleware(FileController, 'uploadImage'));
router.post('/import', before, auth, middleware(ImportController, 'import'));
router.get('/export/excel', before, middleware(OutputController, 'exportExcel'));
router.post('/config', middleware(UserController, 'config'));
router.get('/captcha.jpg', middleware(CaptchaController, 'captcha'));
router.get('/geeCaptcha', middleware(GeeCaptchaController, 'captcha'));
... ...
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;
... ...
const exportExcelConfs = {
shopBill: {
apiUrl: '/erp/queryShopBillList',
conf: {
cols: [
{
caption: '账务ID',
type: 'number',
},
{
caption: '结算时间',
type: 'string',
},
{
caption: '订单号',
type: 'string',
},
{
caption: 'SKU',
type: 'string',
},
{
caption: '数量',
type: 'number',
},
{
caption: '商品名称',
type: 'string',
},
{
caption: '账务类型',
type: 'string',
},
{
caption: '业务描述',
type: 'string',
},
{
caption: '成交价/优惠价',
type: 'number',
},
{
caption: '商家应收/分摊比例',
type: 'number',
},
{
caption: '商家应收',
type: 'number',
},
{
caption: '提现服务费',
type: 'number',
},
{
caption: '商家实收',
type: 'number',
},
{
caption: '提现状态',
type: 'string',
},
{
caption: '可提现日期',
type: 'string',
},
{
caption: '提现申请日期',
type: 'string',
},
],
rows: [
'id',
'businessTime',
'orderCode',
'productSku',
'quantity',
'productName',
'billTypeDesc',
'clearingTypeName',
'lastPrice',
'clearingDiscount',
'shopDeserveAmount',
'withdrawServiceAmount',
'shopNetAmount',
'statusName',
'withdrawalTime',
'withdrawApplyTime',
],
},
},
};
module.exports = exportExcelConfs;
... ...
'use strict';
const _ = require('lodash');
const Context = require('../framework/context');
const Api = require('../common/api');
const config = global.yoho.config;
class ExportExcelService extends Context {
constructor() {
super();
this.api = this.instance(Api);
}
//获取要导出的数据
getExportData(apiUrl, params) {
const apiArr = _.split(apiUrl, '/');
const platform = apiArr[1];
const method = apiArr[2];
return this.api
.post(config.apiDomain[platform][method], params)
.then(res => {
if (res.code !== 200) {
return Promise.reject({ code: 500, message: res.message });
}
return res.data;
})
.catch(error => {
return Promise.reject({ code: 400, message: error.message });
});
}
}
module.exports = ExportExcelService;
... ...