Authored by yyq

export csv

@@ -7,6 +7,7 @@ const _ = require('lodash'); @@ -7,6 +7,7 @@ const _ = require('lodash');
7 const moment = require('moment'); 7 const moment = require('moment');
8 const ActivityModel = require('../models/activity'); 8 const ActivityModel = require('../models/activity');
9 const {excel_export} = require('../../../utils/excel'); 9 const {excel_export} = require('../../../utils/excel');
  10 +const {csvExport} = require('../../../utils/csv');
10 11
11 const DO_SUCCESS = '操作成功'; 12 const DO_SUCCESS = '操作成功';
12 const DO_FAILED = '操作失败!'; 13 const DO_FAILED = '操作失败!';
@@ -83,12 +84,10 @@ const zeroBuy = { @@ -83,12 +84,10 @@ const zeroBuy = {
83 84
84 req.ctx(ActivityModel).getZerobuyExportList(req.query.id) 85 req.ctx(ActivityModel).getZerobuyExportList(req.query.id)
85 .then(conf => { 86 .then(conf => {
86 - return excel_export({  
87 - title: conf.cols,  
88 - data: conf.rows,  
89 - fileName: `zerobuy_${id}_${new Date().getTime()}`,  
90 - sheetName: conf.name,  
91 - res 87 + return csvExport(res, {
  88 + cols: conf.cols,
  89 + rows: conf.rows,
  90 + fileName: `zerobuy_${id}_${new Date().getTime()}`
92 }); 91 });
93 }).catch(next); 92 }).catch(next);
94 }, 93 },
@@ -51,6 +51,7 @@ @@ -51,6 +51,7 @@
51 "feed": "^1.1.0", 51 "feed": "^1.1.0",
52 "geetest": "^4.1.2", 52 "geetest": "^4.1.2",
53 "influx-batch-sender": "^0.1.9", 53 "influx-batch-sender": "^0.1.9",
  54 + "json2csv": "^4.3.3",
54 "jsonp": "^0.2.1", 55 "jsonp": "^0.2.1",
55 "lodash": "^4.17.4", 56 "lodash": "^4.17.4",
56 "mOxie": "^1.5.5", 57 "mOxie": "^1.5.5",
  1 +const _ = require('lodash');
  2 +const Json2csvParser = require('json2csv').Parser;
  3 +
  4 +const sleep = (time) => {
  5 + return new Promise((resolve) => {
  6 + setTimeout(resolve, time || 1000);
  7 + });
  8 +};
  9 +
  10 +const csvExport = async(res, info = {}) => {
  11 + await res.setHeader('Content-disposition', `attachment; filename=${info.fileName}.csv`);
  12 + await res.writeHead(200, {'Content-Type': 'text/csv;charset=utf-8'});
  13 +
  14 + let fields = info.cols || [];
  15 + let data = [];
  16 +
  17 + info.rows = info.rows || [];
  18 +
  19 + const json2csvParser = new Json2csvParser({fields});
  20 +
  21 + let block = [];
  22 +
  23 + _.forEach(info.rows, (value) => {
  24 + block.push(_.zipObject(fields, value));
  25 +
  26 + if (block.length > 4000) {
  27 + data.push(block);
  28 + block = [];
  29 + }
  30 + });
  31 +
  32 + block.length && data.push(block);
  33 +
  34 + for (let i = 0; i < data.length; i++) {
  35 + await res.write(json2csvParser.parse(data[i]));
  36 + await sleep(200); // 解决循环写入cpu占用过高
  37 + }
  38 +
  39 + await res.end();
  40 +};
  41 +
  42 +module.exports = {
  43 + csvExport
  44 +};
@@ -2046,7 +2046,7 @@ combined-stream@~1.0.6: @@ -2046,7 +2046,7 @@ combined-stream@~1.0.6:
2046 dependencies: 2046 dependencies:
2047 delayed-stream "~1.0.0" 2047 delayed-stream "~1.0.0"
2048 2048
2049 -commander@, commander@^2.13.0, commander@^2.14.1, commander@^2.16.0: 2049 +commander@, commander@^2.13.0, commander@^2.14.1, commander@^2.15.1, commander@^2.16.0:
2050 version "2.19.0" 2050 version "2.19.0"
2051 resolved "http://npm.yohops.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a" 2051 resolved "http://npm.yohops.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
2052 2052
@@ -5517,6 +5517,15 @@ json-stringify-safe@5.0.1, json-stringify-safe@~5.0.1: @@ -5517,6 +5517,15 @@ json-stringify-safe@5.0.1, json-stringify-safe@~5.0.1:
5517 version "5.0.1" 5517 version "5.0.1"
5518 resolved "http://npm.yohops.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" 5518 resolved "http://npm.yohops.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
5519 5519
  5520 +json2csv@^4.3.3:
  5521 + version "4.3.3"
  5522 + resolved "http://npm.yohops.com/json2csv/-/json2csv-4.3.3.tgz#56fcd75f8dde4a8fa2ea82e6314cd55f2c1fe68c"
  5523 + dependencies:
  5524 + commander "^2.15.1"
  5525 + jsonparse "^1.3.1"
  5526 + lodash.get "^4.4.2"
  5527 + lodash.set "^4.3.2"
  5528 +
5520 json3@^3.3.2: 5529 json3@^3.3.2:
5521 version "3.3.2" 5530 version "3.3.2"
5522 resolved "http://npm.yohops.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1" 5531 resolved "http://npm.yohops.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
@@ -5554,7 +5563,7 @@ jsonparse@0.0.5: @@ -5554,7 +5563,7 @@ jsonparse@0.0.5:
5554 version "0.0.5" 5563 version "0.0.5"
5555 resolved "http://npm.yohops.com/jsonparse/-/jsonparse-0.0.5.tgz#330542ad3f0a654665b778f3eb2d9a9fa507ac64" 5564 resolved "http://npm.yohops.com/jsonparse/-/jsonparse-0.0.5.tgz#330542ad3f0a654665b778f3eb2d9a9fa507ac64"
5556 5565
5557 -jsonparse@^1.2.0: 5566 +jsonparse@^1.2.0, jsonparse@^1.3.1:
5558 version "1.3.1" 5567 version "1.3.1"
5559 resolved "http://npm.yohops.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280" 5568 resolved "http://npm.yohops.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
5560 5569
@@ -5962,6 +5971,10 @@ lodash.foreach@^4.3.0: @@ -5962,6 +5971,10 @@ lodash.foreach@^4.3.0:
5962 version "4.5.0" 5971 version "4.5.0"
5963 resolved "http://npm.yohops.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53" 5972 resolved "http://npm.yohops.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53"
5964 5973
  5974 +lodash.get@^4.4.2:
  5975 + version "4.4.2"
  5976 + resolved "http://npm.yohops.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
  5977 +
5965 lodash.isarguments@^3.0.0: 5978 lodash.isarguments@^3.0.0:
5966 version "3.1.0" 5979 version "3.1.0"
5967 resolved "http://npm.yohops.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" 5980 resolved "http://npm.yohops.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
@@ -6043,6 +6056,10 @@ lodash.restparam@^3.0.0: @@ -6043,6 +6056,10 @@ lodash.restparam@^3.0.0:
6043 version "3.6.1" 6056 version "3.6.1"
6044 resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" 6057 resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
6045 6058
  6059 +lodash.set@^4.3.2:
  6060 + version "4.3.2"
  6061 + resolved "http://npm.yohops.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
  6062 +
6046 lodash.some@^4.4.0: 6063 lodash.some@^4.4.0:
6047 version "4.6.0" 6064 version "4.6.0"
6048 resolved "http://npm.yohops.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d" 6065 resolved "http://npm.yohops.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d"