You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
238 lines
7.0 KiB
238 lines
7.0 KiB
'use strict'
|
|
const moment = require('moment')
|
|
const { QueryTypes } = require('sequelize');
|
|
const Hex = require('crypto-js/enc-hex');
|
|
const MD5 = require('crypto-js/md5');
|
|
|
|
let axyTokenCache = {
|
|
token: null,
|
|
orgId: null,
|
|
expireTime: null, //过期时间
|
|
}
|
|
|
|
async function getAnxinyunToken (ctx) {
|
|
try {
|
|
if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) {
|
|
if (ctx.app.fs.opts.axyProject.split('/').length === 3) {
|
|
const dataToAxy = {
|
|
domain: ctx.app.fs.opts.axyProject.split('/')[0],
|
|
username: ctx.app.fs.opts.axyProject.split('/')[1],
|
|
password: ctx.app.fs.opts.axyProject.split('/')[2],
|
|
}
|
|
const axyResponse = await ctx.app.fs.anxinyun.post('login', { data: dataToAxy })
|
|
if (axyResponse.authorized) {
|
|
axyTokenCache.token = axyResponse.token //放进缓存
|
|
axyTokenCache.orgId = axyResponse.orgId //放进缓存
|
|
axyTokenCache.expireTime = moment().add(1, 'hour').format('YYYY-MM-DD HH:mm:ss')
|
|
}
|
|
}
|
|
}
|
|
return axyTokenCache
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`sechedule: laborAttendance, error: ${error}`)
|
|
}
|
|
}
|
|
//调用安心云结构物接口
|
|
async function findAnxinyunProject (ctx, next) {
|
|
try {
|
|
let { type, url, params = {} } = ctx.request.body
|
|
let data = await getAnxinyunToken(ctx)
|
|
if (url && url.indexOf('{orgId}') != -1) {
|
|
url = url.replace(`{orgId}`, data.orgId)
|
|
}
|
|
const res = await ctx.app.fs.anxinyun[type](`${url}?token=${data.token}`, {
|
|
data: params.data || {},
|
|
query: params.query || {},
|
|
})
|
|
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 addorEditRelation (ctx, next) {
|
|
let err = ''
|
|
const { axyProjectId, structrueId, id } = ctx.request.body
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
//编辑
|
|
if (id) {
|
|
const res = await models.ProjectBind.findOne({ where: { id, axyProjectId, structureId: structrueId } })
|
|
if (res) {
|
|
err = '所选安心云结构物和巡检结构物重复!!!'
|
|
throw '所选安心云结构物和巡检结构物重复!!!'
|
|
}
|
|
await models.ProjectBind.update({ axyProjectId, structureId: structrueId }, { where: { id } })
|
|
|
|
ctx.status = 204
|
|
ctx.body = {
|
|
message: '编辑成功!!',
|
|
}
|
|
} else {
|
|
//新增
|
|
const res = await models.ProjectBind.findOne({ where: { axyProjectId, structureId: structrueId } })
|
|
if (res) {
|
|
err = '所选安心云结构物和巡检结构物重复!!!'
|
|
throw '所选安心云结构物和巡检结构物重复!!!'
|
|
}
|
|
await models.ProjectBind.create({ axyProjectId, structureId: structrueId })
|
|
ctx.status = 204
|
|
ctx.body = {
|
|
message: '新增成功!!',
|
|
}
|
|
}
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: err ? err : id ? '编辑失败' : '新增失败',
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getRelationList (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const { limit, page, startTime, endTime } = ctx.query
|
|
let options = {
|
|
where: {},
|
|
}
|
|
if (limit) {
|
|
options.limit = Number(limit)
|
|
}
|
|
if (page && limit) {
|
|
options.offset = Number(page) * Number(limit)
|
|
}
|
|
if (startTime && endTime) {
|
|
options.where.inspectTm = { $between: [startTime, endTime] }
|
|
}
|
|
const res = await models.ProjectBind.findAndCountAll(options)
|
|
ctx.body = res
|
|
ctx.status = 200
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: '查询关联关系列表失败',
|
|
}
|
|
}
|
|
}
|
|
|
|
async function deleteRelation (ctx, next) {
|
|
const { id } = ctx.params
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const res = await models.ProjectBind.findOne({ where: { id: Number(id) } })
|
|
if (!res) {
|
|
throw 'id不存在'
|
|
}
|
|
await models.ProjectBind.destroy({ where: { id: Number(id) } })
|
|
ctx.status = 200
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: '关联关系列表失败',
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getProjectType (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const sequelize = ctx.fs.dc.orm;
|
|
const { } = ctx.query
|
|
|
|
const res = await sequelize.query(`select distinct type from project`, { type: QueryTypes.SELECT });
|
|
ctx.body = res
|
|
ctx.status = 200
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: '获取项目类型失败',
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getProjectPublishList (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const { limit, page } = ctx.query
|
|
let options = {
|
|
where: {},
|
|
}
|
|
if (limit) {
|
|
options.limit = Number(limit)
|
|
}
|
|
if (page && limit) {
|
|
options.offset = Number(page) * Number(limit)
|
|
}
|
|
|
|
const res = await models.ProjectUser.findAndCountAll(options)
|
|
ctx.body = res
|
|
ctx.status = 200
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: '获取项目发布列表失败',
|
|
}
|
|
}
|
|
}
|
|
|
|
async function postProjectPublish (ctx, next) {
|
|
let message = '新增项目失败'
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const data = ctx.request.body
|
|
const { id, password } = data
|
|
|
|
if (id) {
|
|
message = '修改项目失败'
|
|
await models.ProjectUser.update(data, { where: { id } })
|
|
} else {
|
|
let passwords = Hex.stringify(MD5(password));
|
|
await models.ProjectUser.create({ ...data, password: passwords, del: false })
|
|
}
|
|
ctx.status = 204
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: message
|
|
}
|
|
}
|
|
}
|
|
|
|
async function delProjectPublish (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models
|
|
const { id } = ctx.params
|
|
|
|
await models.ProjectUser.destroy({ where: { id } })
|
|
|
|
ctx.status = 204
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
|
|
ctx.status = 400
|
|
ctx.body = {
|
|
message: '删除项目失败'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
findAnxinyunProject,
|
|
addorEditRelation,
|
|
getRelationList,
|
|
deleteRelation,
|
|
getProjectType,
|
|
getProjectPublishList,
|
|
postProjectPublish,
|
|
delProjectPublish
|
|
}
|
|
|