Authored by yyq

export csv

... ... @@ -7,6 +7,7 @@ const _ = require('lodash');
const moment = require('moment');
const ActivityModel = require('../models/activity');
const {excel_export} = require('../../../utils/excel');
const {csvExport} = require('../../../utils/csv');
const DO_SUCCESS = '操作成功';
const DO_FAILED = '操作失败!';
... ... @@ -83,12 +84,10 @@ const zeroBuy = {
req.ctx(ActivityModel).getZerobuyExportList(req.query.id)
.then(conf => {
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: `zerobuy_${id}_${new Date().getTime()}`,
sheetName: conf.name,
res
return csvExport(res, {
cols: conf.cols,
rows: conf.rows,
fileName: `zerobuy_${id}_${new Date().getTime()}`
});
}).catch(next);
},
... ...
... ... @@ -51,6 +51,7 @@
"feed": "^1.1.0",
"geetest": "^4.1.2",
"influx-batch-sender": "^0.1.9",
"json2csv": "^4.3.3",
"jsonp": "^0.2.1",
"lodash": "^4.17.4",
"mOxie": "^1.5.5",
... ...
const _ = require('lodash');
const Json2csvParser = require('json2csv').Parser;
const sleep = (time) => {
return new Promise((resolve) => {
setTimeout(resolve, time || 1000);
});
};
const csvExport = async(res, info = {}) => {
await res.setHeader('Content-disposition', `attachment; filename=${info.fileName}.csv`);
await res.writeHead(200, {'Content-Type': 'text/csv;charset=utf-8'});
let fields = info.cols || [];
let data = [];
info.rows = info.rows || [];
const json2csvParser = new Json2csvParser({fields});
let block = [];
_.forEach(info.rows, (value) => {
block.push(_.zipObject(fields, value));
if (block.length > 4000) {
data.push(block);
block = [];
}
});
block.length && data.push(block);
for (let i = 0; i < data.length; i++) {
await res.write(json2csvParser.parse(data[i]));
await sleep(200); // 解决循环写入cpu占用过高
}
await res.end();
};
module.exports = {
csvExport
};
... ...
... ... @@ -2046,7 +2046,7 @@ combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"
commander@, commander@^2.13.0, commander@^2.14.1, commander@^2.16.0:
commander@, commander@^2.13.0, commander@^2.14.1, commander@^2.15.1, commander@^2.16.0:
version "2.19.0"
resolved "http://npm.yohops.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
... ... @@ -5517,6 +5517,15 @@ json-stringify-safe@5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "http://npm.yohops.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
json2csv@^4.3.3:
version "4.3.3"
resolved "http://npm.yohops.com/json2csv/-/json2csv-4.3.3.tgz#56fcd75f8dde4a8fa2ea82e6314cd55f2c1fe68c"
dependencies:
commander "^2.15.1"
jsonparse "^1.3.1"
lodash.get "^4.4.2"
lodash.set "^4.3.2"
json3@^3.3.2:
version "3.3.2"
resolved "http://npm.yohops.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
... ... @@ -5554,7 +5563,7 @@ jsonparse@0.0.5:
version "0.0.5"
resolved "http://npm.yohops.com/jsonparse/-/jsonparse-0.0.5.tgz#330542ad3f0a654665b778f3eb2d9a9fa507ac64"
jsonparse@^1.2.0:
jsonparse@^1.2.0, jsonparse@^1.3.1:
version "1.3.1"
resolved "http://npm.yohops.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
... ... @@ -5962,6 +5971,10 @@ lodash.foreach@^4.3.0:
version "4.5.0"
resolved "http://npm.yohops.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53"
lodash.get@^4.4.2:
version "4.4.2"
resolved "http://npm.yohops.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
lodash.isarguments@^3.0.0:
version "3.1.0"
resolved "http://npm.yohops.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
... ... @@ -6043,6 +6056,10 @@ lodash.restparam@^3.0.0:
version "3.6.1"
resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
lodash.set@^4.3.2:
version "4.3.2"
resolved "http://npm.yohops.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
lodash.some@^4.4.0:
version "4.6.0"
resolved "http://npm.yohops.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d"
... ...