'use strict'; const Hex = require('crypto-js/enc-hex'); const MD5 = require('crypto-js/md5'); const moment = require('moment'); let axyTokenCache = { token: null, orgId: null, expireTime: null //过期时间 } const getAnxinyunToken = async function (ctx) { try { if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) { if (ctx.app.fs.opts.axyProject.split('/').length === 3) { const dataToAxy = { p: 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('project/login', { data: dataToAxy }) if (axyResponse.authorized) { axyTokenCache.token = axyResponse.token //放进缓存 axyTokenCache.orgId = axyResponse.orgId //放进缓存 axyTokenCache.expireTime = moment().add(20, 'hour').format('YYYY-MM-DD HH:mm:ss') } } } return axyTokenCache } catch (error) { ctx.fs.logger.error(`sechedule: laborAttendance, error: ${error}`); } } async function axyData (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 (err) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${err}`); ctx.status = 400; ctx.body = { name: 'FindError', message: '获取安心云数据失败' }; } } const pumpInformation = async function (ctx) { let errMsg = { message: '获取泵站数据失败' } try { const models = ctx.fs.dc.models; const { page, limit, name } = ctx.query; const res = await models.PumpInformation.findAll(); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } const getPumpStation = async function (ctx) { try { const { key, methodType, field } = ctx.query; let res if (field) { res = await ctx.redis[methodType]("pumpStation_" + key, field) || [] } else { res = await ctx.redis[methodType]("pumpStation_" + key) || [] } ctx.status = 200; ctx.body = JSON.parse(res) } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '获取所有泵站信息失败' } } } const getVideoUrl = async function (ctx) { let errMsg = { message: '获取视频监控url' } try { const models = ctx.fs.dc.models; const res = await models.Video.findAll(); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } module.exports = { axyData, pumpInformation, getPumpStation, getVideoUrl }