Browse Source

(*)权限配置相关接口修改

master
zmh 2 years ago
parent
commit
b96ff858ef
  1. 38
      api/app/lib/controllers/auth/index.js
  2. 169
      api/app/lib/controllers/role/index.js

38
api/app/lib/controllers/auth/index.js

@ -80,7 +80,43 @@ async function login(ctx, next) {
expired: moment().add(1, 'day'), expired: moment().add(1, 'day'),
userInfo: JSON.stringify(emisLoginRes) userInfo: JSON.stringify(emisLoginRes)
}); });
const { adminHr, id } = emisLoginRes;
let codes = [];
//人资管理员-所有权限
if (adminHr.some(admin => admin.id == id)) {
const resource = await models.Resource.findAll({
attributes: ['code'],
order: [['id', 'asc']]
})
codes = resource.map(r => r.code);
} else {
const userResources = await models.RoleResource.findAll({
attributes: ['id', 'resId'],
include: [{
required: true,
model: models.Role,
attributes: [],
include: [{
model: models.UserRole,
attributes: [],
where: { userId: id }
}],
}, {
required: true,
model: models.Resource,
attributes: ['id', 'name', 'code'],
}],
order: [['id', 'asc']]
});
let rslt = [];
userResources && userResources.map(ur => {
if (!rslt.some(r => r.id === ur.resId)) {
rslt.push(ur.resource);
codes.push(ur.resource.code);
}
})
}
emisLoginRes.codes = codes;
ctx.status = 200; ctx.status = 200;
ctx.body = emisLoginRes; ctx.body = emisLoginRes;
} }

169
api/app/lib/controllers/role/index.js

@ -3,96 +3,119 @@ const moment = require('moment')
const fs = require('fs'); const fs = require('fs');
async function get(ctx) { async function get(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
let roleList = await models.Role.findAndCountAll({ let roleList = await models.Role.findAndCountAll({
where: { delete: true }, where: { delete: false },
order: [['id', 'desc']], order: [['id', 'desc']],
}); });
ctx.status = 200 ctx.status = 200
ctx.body = roleList; ctx.body = roleList;
} 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 = { name: 'FindError', message: '查询角色数据失败' } ctx.body = { name: 'FindError', message: '查询角色数据失败' }
} }
} }
async function add(ctx) { async function add(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { name } = ctx.request.body const { name } = ctx.request.body
const role = await models.Role.findOne({ const role = await models.Role.findOne({
where: { name } where: { name: name, delete: false }
}) })
if (role && !role.delete) { if (role) {
throw '当前角色已存在' throw '当前角色已存在'
} }
let storageData = { name ,delete:true} let storageData = { name, delete: true }
await models.Role.create(storageData) await models.Role.create(storageData)
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 = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined
} }
} }
} }
async function edit(ctx) { async function edit(ctx) {
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
// const { pepUserId, provinces, cities, businessLines } = ctx.request.body // const { pepUserId, provinces, cities, businessLines } = ctx.request.body
const { id, name } = ctx.request.body const { id, name } = ctx.request.body
const role = await models.Role.findOne({ const oldRole = await models.Role.findOne({
where: { id } where: { id }
}) })
if (!role) { if (!oldRole) {
throw '当前角色不存在' throw '当前角色不存在'
} }
let storageData = { name }
await models.Role.update(storageData, { const role = await models.Role.findOne({
where: { id } where: { name: name, id: { $not: id }, delete: false }
}) })
ctx.status = 204; if (!role) {
} catch (error) { throw '当前角色已存在'
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); }
ctx.status = 400; let storageData = { name }
ctx.body = { await models.Role.update(storageData, {
message: typeof error == 'string' ? error : undefined where: { id }
} })
} ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
} }
async function del(ctx) { async function del(ctx) {
try { const transaction = await ctx.fs.dc.orm.transaction();
const { models } = ctx.fs.dc; try {
const { id } = ctx.query const { models } = ctx.fs.dc;
const { id } = ctx.query
await models.Role.update({ await models.Role.update({ delete: true },
delete: false}, {
{where: {id } where: { id },
}) transaction
ctx.status = 204; })
} catch (error) { await models.RoleResource.destroy({
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); where: {
ctx.status = 400; roleId: id
ctx.body = { },
message: typeof error == 'string' ? error : undefined transaction
} })
} await models.UserRole.destroy({
where: {
roleId: id
},
transaction
})
await transaction.commit();
ctx.status = 204;
} catch (error) {
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
} }
module.exports = { module.exports = {
get, get,
add, add,
edit, edit,
del, del,
} }
Loading…
Cancel
Save