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
1.8 KiB
67 lines
1.8 KiB
2 years ago
|
'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: '获取安心云数据失败' };
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
module.exports = {
|
||
|
axyData,
|
||
|
}
|