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.
131 lines
3.1 KiB
131 lines
3.1 KiB
'use strict';
|
|
|
|
const crypto = require('crypto');
|
|
const moment = require('moment');
|
|
const uuid = require('uuid');
|
|
|
|
module.exports.login = async (ctx, next) => {
|
|
try {
|
|
const { models } = ctx.fs.dc;
|
|
const { username, password = '' } = ctx.request.body;
|
|
let password_ = crypto.createHash('md5').update(password).digest('hex');
|
|
|
|
let user = await models.User.findOne({
|
|
attributes: { exclude: ['password'] },
|
|
where: {
|
|
username,
|
|
password: password_
|
|
},
|
|
include: [
|
|
{ model: models.Department, }
|
|
]
|
|
})
|
|
|
|
if (!user) {
|
|
throw '用户名或密码错误'
|
|
}
|
|
|
|
let userInfo = {
|
|
...user.dataValues,
|
|
authorized: true,
|
|
token: uuid.v4(),
|
|
}
|
|
|
|
await models.UserToken.create({
|
|
token: userInfo.token,
|
|
userInfo: userInfo,
|
|
expired: moment().add(3, 'days').toDate()
|
|
})
|
|
|
|
ctx.status = 200;
|
|
ctx.body = userInfo
|
|
} catch (error) {
|
|
ctx.logger.log(error)
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : '登录失败'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports.logout = async (ctx, next) => {
|
|
try {
|
|
const { models } = ctx.fs;
|
|
const { header, query } = ctx;
|
|
const token = query.token || header.token;
|
|
|
|
await models.UserToken.destroy({
|
|
where: {
|
|
token
|
|
}
|
|
})
|
|
|
|
ctx.status = 204;
|
|
ctx.body = {}
|
|
} catch (error) {
|
|
ctx.logger.log(error)
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: ''
|
|
}
|
|
}
|
|
}
|
|
|
|
// 刷新token接口
|
|
module.exports.refreshToken = async (ctx, next) => {
|
|
try {
|
|
const { models } = ctx.fs;
|
|
const { header, query } = ctx;
|
|
const token = query.token || header.token;
|
|
if (!token) {
|
|
throw '缺少token';
|
|
}
|
|
// 查找token
|
|
const userToken = await models.UserToken.findOne({ where: { token } });
|
|
if (!userToken) {
|
|
throw '无效的token';
|
|
}
|
|
// 续期3天
|
|
userToken.expired = require('moment')().add(3, 'days').toDate();
|
|
await userToken.save();
|
|
ctx.status = 200;
|
|
ctx.body = {
|
|
token: userToken.token,
|
|
expired: userToken.expired,
|
|
userInfo: userToken.userInfo
|
|
};
|
|
} catch (error) {
|
|
ctx.logger.log(error)
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : '刷新token失败'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports.getPepUserInfoByToken = async (ctx, next) => {
|
|
try {
|
|
const { header, query } = ctx;
|
|
const token = query.token || header.token;
|
|
if (!token) { throw '缺少token' };
|
|
|
|
// 查找token
|
|
const userInfo = await ctx.app.fs.emisReq.get('/user-info', {
|
|
query: { token }
|
|
});
|
|
if (!userInfo) { throw '无效的token' };
|
|
|
|
delete userInfo.allDepartment;
|
|
delete userInfo.resources;
|
|
|
|
ctx.status = 200;
|
|
ctx.body = userInfo;
|
|
} catch (error) {
|
|
ctx.logger.log(error)
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : '获取用户信息失败'
|
|
}
|
|
}
|
|
}
|
|
|
|
|