|
@ -4,81 +4,127 @@ const MD5 = require('crypto-js/md5'); |
|
|
const moment = require('moment'); |
|
|
const moment = require('moment'); |
|
|
const uuid = require('uuid'); |
|
|
const uuid = require('uuid'); |
|
|
|
|
|
|
|
|
async function login (ctx, next) { |
|
|
async function getDataRange(ctx) { |
|
|
// const transaction = await ctx.fs.dc.orm.transaction();
|
|
|
//null为所有权限,不加条件过滤
|
|
|
try { |
|
|
let userIds = null; |
|
|
const models = ctx.fs.dc.models; |
|
|
let departmentNames = null; |
|
|
const params = ctx.request.body; |
|
|
let userNames = null; |
|
|
|
|
|
let dataRange = null; |
|
|
let emisLoginRes = null |
|
|
const { adminHr, id, department, allDepartment } = ctx.fs.api.userInfo; |
|
|
if (params.username && params.password) { |
|
|
//人资管理-所有权限
|
|
|
emisLoginRes = await ctx.app.fs.emisRequest.post('login', { |
|
|
if (adminHr.filter(admin => admin.id == id).length) { |
|
|
data: { ...params, code: 'HR' } |
|
|
dataRange = 1; |
|
|
}) |
|
|
} else { |
|
|
} else if (params.token) { |
|
|
const models = ctx.fs.dc.models; |
|
|
emisLoginRes = await ctx.app.fs.emisRequest.get('user-info', { |
|
|
//获取用户对应角色的最高数据范围
|
|
|
query: { |
|
|
let roleRes = await models.Role.findAll({ |
|
|
token: params.token, code: 'HR' |
|
|
include: [{ |
|
|
|
|
|
model: models.UserRole, |
|
|
|
|
|
where: { userId: id } |
|
|
|
|
|
}] |
|
|
|
|
|
}) |
|
|
|
|
|
if (roleRes.some(r => r.dataRange && r.dataRange === 1)) { |
|
|
|
|
|
dataRange = 1; |
|
|
|
|
|
} else { |
|
|
|
|
|
const departments = allDepartment.departments; |
|
|
|
|
|
dataRange = 2; |
|
|
|
|
|
userIds = []; |
|
|
|
|
|
userNames = []; |
|
|
|
|
|
if (department && departments) { |
|
|
|
|
|
let userDeps = department.map(d => d.id); |
|
|
|
|
|
departmentNames = department.map(d => d.name); |
|
|
|
|
|
let deps = departments.filter(d => userDeps.indexOf(d.id) > -1) |
|
|
|
|
|
if (deps && deps.length > 0) { |
|
|
|
|
|
deps.map(d => d.users.map(u => { |
|
|
|
|
|
if (userIds.indexOf(u.id) < 0) { |
|
|
|
|
|
userIds.push(u.id); |
|
|
|
|
|
userNames.push(u.name); |
|
|
|
|
|
} |
|
|
|
|
|
})); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return { dataRange, userIds, departmentNames, userNames }; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
if (!emisLoginRes) { |
|
|
throw "无此用户,请使用正确的登录信息" |
|
|
throw "无此用户,请使用正确的登录信息" |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
|
|
|
emisLoginRes.authorized = true |
|
|
emisLoginRes.authorized = true |
|
|
emisLoginRes.expired = moment().add(1, 'day') |
|
|
emisLoginRes.expired = moment().add(1, 'day') |
|
|
emisLoginRes.hrUserInfo = undefined |
|
|
emisLoginRes.hrUserInfo = undefined |
|
|
|
|
|
|
|
|
await ctx.redis.hmset(emisLoginRes.token, { |
|
|
await ctx.redis.hmset(emisLoginRes.token, { |
|
|
expired: moment().add(1, 'day'), |
|
|
expired: moment().add(1, 'day'), |
|
|
userInfo: JSON.stringify(emisLoginRes) |
|
|
userInfo: JSON.stringify(emisLoginRes) |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
ctx.status = 200; |
|
|
ctx.body = emisLoginRes; |
|
|
ctx.body = emisLoginRes; |
|
|
} |
|
|
} |
|
|
// await transaction.commit();
|
|
|
// await transaction.commit();
|
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
// await transaction.rollback();
|
|
|
// await transaction.rollback();
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
ctx.status = 400; |
|
|
ctx.status = 400; |
|
|
let message = typeof error == 'string' ? error |
|
|
let message = typeof error == 'string' ? error |
|
|
: error.response && error.response.body && error.response.body.message ? |
|
|
: error.response && error.response.body && error.response.body.message ? |
|
|
error.response.body.message |
|
|
error.response.body.message |
|
|
: "登录失败" |
|
|
: "登录失败" |
|
|
if (message == '账号或密码错误') { |
|
|
if (message == '账号或密码错误') { |
|
|
message = '无此用户,请使用正确的登录信息' |
|
|
message = '无此用户,请使用正确的登录信息' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ctx.body = { |
|
|
ctx.body = { |
|
|
message: message |
|
|
message: message |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function logout (ctx) { |
|
|
async function logout(ctx) { |
|
|
try { |
|
|
try { |
|
|
const models = ctx.fs.dc.models; |
|
|
const models = ctx.fs.dc.models; |
|
|
const params = ctx.request.body; |
|
|
const params = ctx.request.body; |
|
|
|
|
|
|
|
|
await ctx.app.fs.emisRequest.put('logout', { |
|
|
await ctx.app.fs.emisRequest.put('logout', { |
|
|
data: params |
|
|
data: params |
|
|
}) |
|
|
}) |
|
|
await ctx.redisTools.hdelall(token); |
|
|
await ctx.redisTools.hdelall(token); |
|
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
ctx.status = 204; |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
ctx.status = 400; |
|
|
ctx.status = 400; |
|
|
ctx.body = { |
|
|
ctx.body = { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = { |
|
|
module.exports = { |
|
|
login, |
|
|
login, |
|
|
logout, |
|
|
logout, |
|
|
|
|
|
getDataRange |
|
|
}; |
|
|
}; |