政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
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.
 
 
 
 

67 lines
2.1 KiB

'use strict';
const moment = require('moment')
module.exports = function (app, opts) {
let axyTokenCache = {
userId: null,
orgId: null,
token: null,
expireTime: null //过期时间
}
let getAnxinyunToken = async function () {
try {
if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) {
const dataToAxy = {
pcode: 'fce4afe2-5b6a-408a-ab18-a2afa7fa027c',
username: '123456',
password: '123456',
}
const axyResponse = await app.fs.waterReq.post('/login', dataToAxy)
const axyData = axyResponse.body
if (axyData.authorized) {
axyTokenCache.userId = axyData.id //放进缓存
axyTokenCache.orgId = axyData.orgId //放进缓存
axyTokenCache.token = axyData.token //放进缓存
axyTokenCache.expireTime = moment().add(23, 'hour').format('YYYY-MM-DD HH:mm:ss')
}
}
return axyTokenCache
} catch (err) {
app.fs.logger.error(`utils: fetch anxinyun token, error: ${err}`);
}
}
let rslt = {
};
async function getDataFromAxy(url, method, data) {
try {
if (rslt[url]) {
getData(url, method, data)
return rslt[url]
} else {
let res = await getData(url, method, data)
return res
}
} catch (err) {
app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`);
}
}
async function getData(url, method = 'get', data) {
try {
const res = await app.fs.waterReq[method](`${url}`, data ? { ...data } : {})
rslt[url] = res
app.fs.waterCache = rslt
return res
} catch (err) {
app.fs.logger.error(`utils: error: ${err}`);
}
}
return {
getAnxinyunToken,
getDataFromAxy,
}
}