Browse Source

文件下载综合 20%

release_0.0.4
巴林闲侠 2 years ago
parent
commit
05f92069d3
  1. 36
      api/app/lib/controllers/data/index.js
  2. 5
      api/app/lib/routes/data/index.js
  3. 77
      api/utils/xlsxDownload.js

36
api/app/lib/controllers/data/index.js

@ -0,0 +1,36 @@
'use strict';
const { simpleExcelDown } = require('../../../../utils/xlsxDownload');
async function dataExport (ctx) {
try {
// const models = ctx.fs.dc.models;
// const { userId } = ctx.fs.api
// const { ids } = ctx.query;
// const exportData = await models.BusCar.destroy({
// where: {
// id: { $in: ids.split(',') }
// }
// })
// const fileName = `摄像头信息列表_${userId}_${moment().format('YYYYMMDDHHmmss')}` + '.csv'
// const filePath = await simpleExcelDown({ data: exportData, header, fileName: fileName })
// const fileData = fs.readFileSync(filePath);
// ctx.status = 200;
// ctx.set('Content-Type', 'application/x-xls');
// ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName));
// ctx.body = fileData;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
module.exports = {
dataExport
};

5
api/app/lib/routes/data/index.js

@ -7,9 +7,14 @@ const project = require('../../controllers/data/project');
const overspeed = require('../../controllers/data/overspeed');
const bus = require('../../controllers/data/bus');
const publicity = require('../../controllers/data/publicity');
const data = require('../../controllers/data');
module.exports = function (app, router, opts) {
// 数据导出
app.fs.api.logAttr['GET/data/export'] = { content: '导出数据', visible: true };
router.get('/data/export', data.dataExport);
// 运政
//客运车
async function setVehicleType (ctx, next) {

77
api/utils/xlsxDownload.js

@ -0,0 +1,77 @@
'use strict';
const fs = require('fs');
const xlsx = require('better-xlsx');
const path = require('path')
const moment = require('moment')
//递归创建目录 同步方法
async function makeDir (dir) {
if (!fs.existsSync(dir)) {
makeDir(path.dirname(dir))
fs.mkdirSync(dir, function (err) {
if (err) {
throw err
}
});
}
}
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss') } = {}) {
const fileDirPath = path.join(__dirname, `../../downloadFiles`)
makeDir(fileDirPath)
const file = new xlsx.File();
const sheet_1 = file.addSheet('sheet_1');
// header
const headerStyle = new xlsx.Style();
headerStyle.align.h = 'center';
headerStyle.align.v = 'center';
headerStyle.border.right = 'thin';
headerStyle.border.rightColor = '#000000';
headerStyle.border.bottom = 'thin';
headerStyle.border.bottomColor = '#000000';
const headerRow = sheet_1.addRow();
const indexCell = headerRow.addCell();
indexCell.value = '序号'
indexCell.style = headerStyle
for (let h of header) {
const cell = headerRow.addCell();
cell.value = h.title;
cell.style = headerStyle
}
// data
const style = new xlsx.Style();
style.align.h = 'left';
style.align.v = 'center';
style.border.right = 'thin';
style.border.rightColor = '#000000';
style.border.bottom = 'thin';
style.border.bottomColor = '#000000';
for (let i = 0; i < data.length; i++) {
const row = sheet_1.addRow();
const indexCell = row.addCell();
indexCell.value = i + 1
indexCell.style = headerStyle
for (let h of header) {
const cell = row.addCell();
cell.value = data[i][h.key];
cell.style = style
}
}
const savePath = path.join(fileDirPath, fileName)
await new Promise(function (resolve, reject) {
file.saveAs()
.pipe(fs.createWriteStream(savePath))
.on('finish', () => {
resolve()
});
})
return savePath
}
module.exports = {
simpleExcelDown,
makeDir
}
Loading…
Cancel
Save