|
|
|
'use strict';
|
|
|
|
const request = require('superagent');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
async function projectList(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const { limit, page, name, justStructure } = ctx.query;
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
where: {
|
|
|
|
|
|
|
|
},
|
|
|
|
// include: [{
|
|
|
|
// as: 'company',
|
|
|
|
// model: models.Company,
|
|
|
|
// attributes: ['id', 'name'],
|
|
|
|
// },],
|
|
|
|
}
|
|
|
|
if (limit) {
|
|
|
|
options.limit = Number(limit)
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
options.offset = Number(page) * Number(limit)
|
|
|
|
}
|
|
|
|
if (name) {
|
|
|
|
options.where.name = { $like: `%${name}%` }
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = []
|
|
|
|
if (justStructure) {
|
|
|
|
res = await models.Project.findAndCountAll({
|
|
|
|
attributes: ['id', 'name'],
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res = await models.Project.findAndCountAll(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = res
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": "获取结构列表失败"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function postAddProject(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const data = ctx.request.body;
|
|
|
|
const { img, longitude, latitude, name, type, describe } = data
|
|
|
|
|
|
|
|
let errMsg = data.id ? '结构物编辑失败' : '结构物新增失败'
|
|
|
|
let project = { img, longitude, latitude, name, type, describe, userId: userInfo.id }
|
|
|
|
|
|
|
|
const alikeProject = await models.Project.findOne({
|
|
|
|
where: {
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if ((!data.id && alikeProject) || (alikeProject && alikeProject.id !== data.id)) {
|
|
|
|
errMsg = '已有相同结构物名称'
|
|
|
|
throw errMsg
|
|
|
|
}
|
|
|
|
if (data && data.id) {
|
|
|
|
await models.Project.update(project, {
|
|
|
|
where: {
|
|
|
|
id: data.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await models.Project.create(project)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delProject(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const { id } = ctx.params
|
|
|
|
|
|
|
|
//删除结构物
|
|
|
|
await models.Project.destroy({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const pointId = []
|
|
|
|
const pointLIst = await models.Point.findAll({
|
|
|
|
where: {
|
|
|
|
projectId: id,
|
|
|
|
},
|
|
|
|
attributes: ['id'],
|
|
|
|
})
|
|
|
|
pointLIst.map(v => pointId.push(v.id))
|
|
|
|
|
|
|
|
//点位
|
|
|
|
await models.Point.destroy({
|
|
|
|
where: {
|
|
|
|
projectId: id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
//巡检计划
|
|
|
|
const planId = []
|
|
|
|
const planLIst = await models.PatrolPlan.findAll({
|
|
|
|
where: {
|
|
|
|
structureId: id,
|
|
|
|
},
|
|
|
|
attributes: ['id'],
|
|
|
|
})
|
|
|
|
planLIst.map(v => planId.push(v.id))
|
|
|
|
await models.PatrolPlan.destroy({
|
|
|
|
where: {
|
|
|
|
structureId: id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//巡检记录
|
|
|
|
await models.PatrolRecord.destroy({
|
|
|
|
where: {
|
|
|
|
pointId: { $in: pointId },
|
|
|
|
patrolPlanId: { $in: planId }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": '删除结构物失败'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let wxAccessToken = {
|
|
|
|
access_token: null,
|
|
|
|
time: null
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addPosition(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const data = ctx.request.body;
|
|
|
|
const { longitude, latitude, name, describe, qrCode, projectId, img, equipmentNo, equipmentModel } = data;
|
|
|
|
|
|
|
|
let errMsg = data.id ? '点位编辑失败' : '点位新增失败'
|
|
|
|
let pointData = { longitude, latitude, name, describe, qrCode, projectId, img, equipmentNo, equipmentModel }
|
|
|
|
|
|
|
|
const alikeProject = await models.Point.findOne({
|
|
|
|
where: {
|
|
|
|
id: data.id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (data && data.id) {
|
|
|
|
if (qrCode) {
|
|
|
|
const { domain, appId, appSecret } = ctx.app.fs.opts.wx;
|
|
|
|
// 获取小程序AccessToken, 两小时过期
|
|
|
|
if (!wxAccessToken.access_token || new Date().getTime() - wxAccessToken.time >= 7200000) {
|
|
|
|
const wxAccessTokenRes = await request.get(`${domain}/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`);
|
|
|
|
if (wxAccessTokenRes.body.access_token) {
|
|
|
|
wxAccessToken.access_token = wxAccessTokenRes.body.access_token;
|
|
|
|
wxAccessToken.time = new Date().getTime();
|
|
|
|
} else {
|
|
|
|
throw '请求微信AccessToken失败';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 获取小程序码
|
|
|
|
const QRCodeRes = await request.post(
|
|
|
|
`${domain}/wxa/getwxacodeunlimit?access_token=${wxAccessToken.access_token}`,
|
|
|
|
{
|
|
|
|
"page": "package/inspectionInput/inspectionInput",
|
|
|
|
"scene": data.id
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!QRCodeRes.body.errcode) {
|
|
|
|
const pathname = path.join(__dirname, `${alikeProject.name}.jpeg`);
|
|
|
|
// 写入临时文件
|
|
|
|
fs.writeFileSync(pathname, QRCodeRes.body, async function (err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const fileInfo = await ctx.app.fs.attachment.upload(pathname, { uploadPath: 'project' });
|
|
|
|
fs.unlinkSync(pathname); // 删除临时文件
|
|
|
|
let fkey = fileInfo.key;
|
|
|
|
await models.Point.update({ ...alikeProject, qrCode: fkey }, {
|
|
|
|
where: {
|
|
|
|
id: data.id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
wxAccessToken.access_token = null;
|
|
|
|
wxAccessToken.time = null;
|
|
|
|
throw '生成二维码失败 ' + QRCodeRes.body.errmsg;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await models.Point.update(pointData, {
|
|
|
|
where: {
|
|
|
|
id: data.id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
await models.Point.create(pointData)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function position(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const { limit, page, projectId } = ctx.query;
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
where: {
|
|
|
|
id: projectId
|
|
|
|
},
|
|
|
|
include: [{
|
|
|
|
model: models.Point,
|
|
|
|
},],
|
|
|
|
}
|
|
|
|
if (limit) {
|
|
|
|
options.limit = Number(limit)
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
options.offset = Number(page) * Number(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = await models.Project.findAndCountAll(options)
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = res
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": "获取结构列表失败"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delPosition(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const { id } = ctx.params
|
|
|
|
|
|
|
|
const pointOne = await models.Point.findOne({
|
|
|
|
where: {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
attributes: ['projectId'],
|
|
|
|
})
|
|
|
|
if (pointOne) {
|
|
|
|
const patrolPlanLIst = await models.PatrolPlan.findAll({
|
|
|
|
where: {
|
|
|
|
structureId: pointOne.projectId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
for (var u of patrolPlanLIst) {
|
|
|
|
const points = []
|
|
|
|
u.points.map(r => {
|
|
|
|
if (r.id == id) {
|
|
|
|
} else {
|
|
|
|
points.push(r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
u.points = points
|
|
|
|
|
|
|
|
await models.PatrolRecord.destroy({
|
|
|
|
where: {
|
|
|
|
pointId: id,
|
|
|
|
patrolPlanId: u.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (points.length > 0) {
|
|
|
|
let data = {
|
|
|
|
name: u.dataValues.name,
|
|
|
|
way: u.dataValues.way,
|
|
|
|
structureId: u.dataValues.structureId,
|
|
|
|
startTime: u.dataValues.startTime,
|
|
|
|
endTime: u.dataValues.endTime,
|
|
|
|
frequency: u.dataValues.frequency,
|
|
|
|
points: u.dataValues.points,
|
|
|
|
userId: u.dataValues.userId,
|
|
|
|
patrolCount: u.dataValues.patrolCount
|
|
|
|
}
|
|
|
|
await models.PatrolPlan.update(data, {
|
|
|
|
where: {
|
|
|
|
id: u.dataValues.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await models.PatrolPlan.destroy({
|
|
|
|
where: {
|
|
|
|
id: u.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await models.Point.destroy({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": '删除点位失败'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function qrCodeShow(ctx, next) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
let userInfo = ctx.fs.api.userInfo;
|
|
|
|
const { projectId, name } = ctx.query;
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
where: {
|
|
|
|
qrCode: { $ne: null }
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if (projectId) {
|
|
|
|
options.where.projectId = projectId
|
|
|
|
}
|
|
|
|
if (name) {
|
|
|
|
options.where.name = { $like: `%${name}%` }
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = await models.Point.findAndCountAll(options)
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = res
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
"message": "获取二维码列表失败"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function q(ctx) {
|
|
|
|
// let error = {
|
|
|
|
// name: 'FindError',
|
|
|
|
// message: "获取失败!"
|
|
|
|
// };
|
|
|
|
// const models = ctx.fs.dc.models;
|
|
|
|
// const { devices } = ctx.request.body
|
|
|
|
// const attachment = ctx.app.fs.qn_attachment
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// if (!Array.isArray(devices)) {
|
|
|
|
// error = { name: 'paramsError', message: '参数不能为空' };
|
|
|
|
// ctx.throw(400);
|
|
|
|
// }
|
|
|
|
// const devicesArr = await models.Device.findAll({
|
|
|
|
// attributes: ['deviceNo', 'periodCode', 'qrSrc'],
|
|
|
|
// where: { deviceNo: { $in: devices } }
|
|
|
|
// })
|
|
|
|
|
|
|
|
// let ids = [], idsMap = {}, qnImages = []
|
|
|
|
// devicesArr.forEach(d => {
|
|
|
|
// const qrSrc = d.qrSrc
|
|
|
|
// const deviceNo = d.deviceNo
|
|
|
|
// const periodCode = d.periodCode
|
|
|
|
// if (qrSrc) {
|
|
|
|
// if (/^\d+$/.test(qrSrc)) {
|
|
|
|
// ids.push(qrSrc)
|
|
|
|
// idsMap[qrSrc] = { deviceNo, periodCode }
|
|
|
|
// } else {
|
|
|
|
// let domain = globalCache.getQnDomain()
|
|
|
|
// let imgUrl = `${domain}/${qrSrc}`
|
|
|
|
// qnImages.push({ src: imgUrl, deviceNo, periodCode })
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
|
|
|
|
// const docs = await models.QrcodePng.findAll({
|
|
|
|
// where: {
|
|
|
|
// id: { $in: ids }
|
|
|
|
// },
|
|
|
|
// attributes: ["id", "base64"]
|
|
|
|
// })
|
|
|
|
|
|
|
|
// let pics = []
|
|
|
|
|
|
|
|
// if (docs.length > 0) {
|
|
|
|
// pics = docs.map((d) => {
|
|
|
|
// let { deviceNo, periodCode } = idsMap[d.id] || {}
|
|
|
|
// let base64 = d.base64.replace(/^data:image\/\w+;base64,/, '')
|
|
|
|
// return {
|
|
|
|
// url: Buffer.from(base64, 'base64'),
|
|
|
|
// name: deviceNo,
|
|
|
|
// periodCode
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (qnImages.length > 0) {
|
|
|
|
// let qns = await downloadImgsAsBase64(qnImages)
|
|
|
|
// pics = pics.concat(qns)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let fileUrl = await downLoadImageBiz(pics, { zipName: "二维码_" + moment().format("YYYY-MM-DD-HH-mm-ss"), attachment })
|
|
|
|
// add2CleanCache(fileUrl, attachment)
|
|
|
|
// ctx.status = 200
|
|
|
|
// ctx.body = { fileUrl }
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
// ctx.fs.logger.error(err);
|
|
|
|
// ctx.status = 400;
|
|
|
|
// ctx.body = error;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
projectList,
|
|
|
|
postAddProject,
|
|
|
|
delProject,
|
|
|
|
addPosition,
|
|
|
|
position,
|
|
|
|
delPosition,
|
|
|
|
qrCodeShow,
|
|
|
|
q
|
|
|
|
}
|