'use strict'; const Hex = require('crypto-js/enc-hex'); const MD5 = require('crypto-js/md5'); const moment = require('moment'); const uuid = require('uuid'); async function login (ctx, next) { // const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const params = ctx.request.body; let emisLoginRes = null if (params.username && params.password) { emisLoginRes = await ctx.app.fs.emisRequest.post('login', { data: { ...params, code: 'HR' } }) } else if (params.token) { emisLoginRes = await ctx.app.fs.emisRequest.get('user-info', { query: { token: params.token, code: 'HR' } }) } if (!emisLoginRes) { throw "无此用户,请使用正确的登录信息" } else { emisLoginRes.authorized = true emisLoginRes.expired = moment().add(1, 'day') emisLoginRes.hrUserInfo = undefined await ctx.redis.hmset(emisLoginRes.token, { expired: moment().add(1, 'day'), userInfo: JSON.stringify(emisLoginRes) }); ctx.status = 200; ctx.body = emisLoginRes; } // await transaction.commit(); } catch (error) { // await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; let message = typeof error == 'string' ? error : error.response.body.message || "登录失败" if (message == '账号或密码错误') { message = '无此用户,请使用正确的登录信息' } ctx.body = { message: message } } } async function logout (ctx) { try { const models = ctx.fs.dc.models; const params = ctx.request.body; await ctx.app.fs.emisRequest.put('logout', { data: params }) await ctx.redisTools.hdelall(token); ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { } } } module.exports = { login, logout, };