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.

149 lines
4.3 KiB

'use strict'
const moment = require('moment')
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 '所选安心云结构物和巡检结构物重复!!!'
}
1 year ago
await models.ProjectBind.update({ axyProjectId, structureId:structrueId }, { where: { id } })
ctx.status = 204
1 year ago
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
1 year ago
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: '关联关系列表失败',
}
}
}
module.exports = {
findAnxinyunProject,
addorEditRelation,
getRelationList,
deleteRelation,
}