Browse Source

xlsx简单文件通用下载

release_0.0.2
yuan_yi 3 years ago
parent
commit
8c5407347d
  1. 2
      .gitignore
  2. 98
      code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js
  3. 2
      code/VideoAccess-VCMP/api/app/lib/routes/nvr/index.js
  4. 2
      code/VideoAccess-VCMP/api/app/lib/utils/index.js
  5. 40
      code/VideoAccess-VCMP/api/app/lib/utils/xlsxDownload.js

2
.gitignore

@ -137,4 +137,4 @@ dist
*yarn.lock
*package-lock.json
*log/
*log/

98
code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js

@ -1,4 +1,5 @@
'use strict';
const fs = require('fs');
const moment = require('moment')
async function edit (ctx, next) {
@ -82,17 +83,29 @@ async function get (ctx) {
nvrId: { $in: nvrIds }
}
})
const cameraIds = cameraRes.map(c => c.id)
let createUserIds = new Set()
let cameraIds = []
for (let c of cameraRes) {
cameraIds.push(c.id)
createUserIds.add(c.createUserId)
}
// 查在安心云绑定的数据
const axbindCameraRes = cameraIds.length ?
await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } })
: []
// 查用户信息
const createUserRes = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',') || -1}/message`, { query: { token } })
for (let { dataValues: n } of nvrRes) {
const corCameras = cameraRes.filter(c => c.nvrId == n.id)
const corBind = axbindCameraRes.filter(b => corCameras.some(c => c.id == b.cameraId))
const corCreateUser = createUserRes.find(u => u.id == n.createUserId)
n.createUser = {
name: corCreateUser ? corCreateUser.username : ''
}
if (corBind.length) {
n.station = []
for (let c of corBind) {
@ -223,12 +236,52 @@ async function detail (ctx) {
async function nvrExport (ctx) {
try {
const { models } = ctx.fs.dc
const { userId, token } = ctx.fs.api
const { utils: { simpleExcelDown } } = ctx.app.fs
const header = [{
title: "设备名称",
key: "name",
}, {
title: "SIP地址",
key: "sip",
}, {
title: "设备厂家",
key: "vender",
}, {
title: "添加账号",
key: "createUser",
}, {
title: "通道数",
key: "channelCount",
}, {
title: "端口",
key: "port",
}, {
title: "设备状态",
key: "state",
}, {
title: "创建时间",
key: "createTime",
}, {
title: "项目名称",
key: "projectName",
}, {
title: "pcode",
key: "pcode",
}, {
title: "结构物",
key: "structure",
},];
const nvrRes = await models.Nvr.findAll({
where: {
createUserId: userId,
}
},
include: [{
model: models.Vender
}]
})
const nvrIds = nvrRes.map(r => r.id)
const cameraRes = await models.Camera.findAll({
@ -236,7 +289,12 @@ async function nvrExport (ctx) {
nvrId: { $in: nvrIds }
}
})
const cameraIds = cameraRes.map(c => c.id)
let createUserIds = new Set()
let cameraIds = []
for (let c of cameraRes) {
cameraIds.push(c.id)
createUserIds.add(c.createUserId)
}
// 查在安心云绑定的数据
const axbindCameraRes =
@ -244,20 +302,44 @@ async function nvrExport (ctx) {
await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } })
: []
// 查用户信息
const createUserRes = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',') || -1}/message`, { query: { token } })
let exportData = []
for (let { dataValues: n } of nvrRes) {
const corCameras = cameraRes.filter(c => c.nvrId == n.id)
const corBind = axbindCameraRes.filter(b => corCameras.some(c => c.id == b.cameraId))
const corCreateUser = createUserRes.find(u => u.id == n.createUserId)
n.createUser = {
name: corCreateUser ? corCreateUser.username : ''
}
n.vender = n.vender ? n.vender.name : ''
n.createTime = moment(n.createTime).format('YYYY-MM-DD HH:mm:ss')
let projectName = new Set(),
pcode = new Set(),
structure = new Set();
if (corBind.length) {
n.station = []
for (let c of corBind) {
n.station = n.station.concat(c.stations)
for (let station of c.stations) {
structure.add(station.structure.name)
for (let project of station.structure.projects) {
projectName.add(project.name)
pcode.add(project.url)
}
}
}
} else {
n.station = []
}
n.projectName = [...projectName].join(',')
n.pcode = [...pcode].join(',')
n.structure = [...structure].join(',')
exportData.push(n)
}
const filePath = ''
const filePath = await simpleExcelDown({ data: exportData, header, fileName: `NVR信息列表_${userId}_${moment().format('YYYYMMDDHHmmss')}` })
const fileData = fs.readFileSync(filePath);
let fileName = filePath.split('/').pop()
ctx.status = 200;

2
code/VideoAccess-VCMP/api/app/lib/routes/nvr/index.js

@ -15,6 +15,6 @@ module.exports = function (app, router, opts) {
app.fs.api.logAttr['DEL/nvr/:nvrId'] = { content: '删除nvr', visible: false };
router.del('/nvr/:nvrId', nvr.del);
app.fs.api.logAttr['GET/nvr/export'] = { content: '删除nvr', visible: false };
app.fs.api.logAttr['GET/nvr/export'] = { content: '导出nvr信息', visible: false };
router.get('/nvr/export', nvr.nvrExport);
};

2
code/VideoAccess-VCMP/api/app/lib/utils/index.js

@ -5,7 +5,7 @@ const fs = require('fs');
module.exports = async function (app, opts) {
fs.readdirSync(__dirname).forEach((filename) => {
if (!['index.js'].some(f => filename != f)) {
if (!['index.js'].some(f => filename == f)) {
const utils = require(`./${filename}`)(app, opts)
app.fs.utils = {
...app.fs.utils,

40
code/VideoAccess-VCMP/api/app/lib/utils/xlsxDownload.js

@ -1,8 +1,10 @@
'use strict';
const fs = require('fs');
const xlsx = require('better-xlsx');
const path = require('path')
const moment = require('moment')
module.exports = function (app, opts) {
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss') } = {}) {
const fileDirPath = path.join(__dirname, `../../downloadFiles`)
@ -10,16 +12,42 @@ module.exports = function (app, opts) {
const sheet_1 = file.addSheet('sheet_1');
// header
const headerRow = sheet.addRow();
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
}
//设置文本在单元格内水平垂直居中
cell.style.align.h = 'center';
cell.style.align.v = 'center';
// border(cell, 0, 0, 1, 0)
// 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 + '.xlsx')

Loading…
Cancel
Save