From 05f92069d3864faec00c413a3bb541cf06c5bee7 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Tue, 26 Jul 2022 21:51:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E7=BB=BC?= =?UTF-8?q?=E5=90=88=2020%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/data/index.js | 36 +++++++++++++ api/app/lib/routes/data/index.js | 5 ++ api/utils/xlsxDownload.js | 77 +++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 api/app/lib/controllers/data/index.js create mode 100644 api/utils/xlsxDownload.js diff --git a/api/app/lib/controllers/data/index.js b/api/app/lib/controllers/data/index.js new file mode 100644 index 00000000..0a14e7a7 --- /dev/null +++ b/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 +}; \ No newline at end of file diff --git a/api/app/lib/routes/data/index.js b/api/app/lib/routes/data/index.js index 9ecc0f52..7b761592 100644 --- a/api/app/lib/routes/data/index.js +++ b/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) { diff --git a/api/utils/xlsxDownload.js b/api/utils/xlsxDownload.js new file mode 100644 index 00000000..fd688bf6 --- /dev/null +++ b/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 +} \ No newline at end of file