diff --git a/api/app/lib/controllers/userRole/index.js b/api/app/lib/controllers/userRole/index.js index 682980d..493580e 100644 --- a/api/app/lib/controllers/userRole/index.js +++ b/api/app/lib/controllers/userRole/index.js @@ -3,97 +3,146 @@ const moment = require('moment') const fs = require('fs'); async function get(ctx) { - try { - const { models } = ctx.fs.dc; - let userRoleList = await models.UserRole.findAndCountAll({ - order: [['id', 'desc']] - }); - ctx.status = 200 - ctx.body = userRoleList; - } catch (error) { - ctx.fs.logger.error(`path:${ctx.path},error:${error}`) - ctx.status = 400; - ctx.body = { name: 'FindError', message: '查询用户信息失败' } - } + try { + const { models } = ctx.fs.dc; + let userRoleList = await models.UserRole.findAndCountAll({ + order: [['id', 'desc']] + }); + ctx.status = 200 + ctx.body = userRoleList; + } catch (error) { + ctx.fs.logger.error(`path:${ctx.path},error:${error}`) + ctx.status = 400; + ctx.body = { name: 'FindError', message: '查询用户信息失败' } + } } async function add(ctx) { - try { - const { models } = ctx.fs.dc; - const { userId, roleId } = ctx.request.body - await models.UserRole.destroy({ - where: { roleId: roleId } - }) + try { + const { models } = ctx.fs.dc; + const { userId, roleId } = ctx.request.body + await models.UserRole.destroy({ + where: { roleId: roleId } + }) - let storageData = userId.map(e => { - return { - roleId: roleId, - userId: e - } - }) - await models.UserRole.bulkCreate(storageData); - 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 - } - } + let storageData = userId.map(e => { + return { + roleId: roleId, + userId: e + } + }) + await models.UserRole.bulkCreate(storageData); + 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 edit(ctx) { - try { - const { models } = ctx.fs.dc; - // const { pepUserId, provinces, cities, businessLines } = ctx.request.body - const { id, name } = ctx.request.body + try { + const { models } = ctx.fs.dc; + // const { pepUserId, provinces, cities, businessLines } = ctx.request.body + const { id, name } = ctx.request.body - const role = await models.Role.findOne({ - where: { id } - }) + const role = await models.Role.findOne({ + where: { id } + }) - if (!role) { - throw '当前角色不存在' - } - let storageData = { name } - await models.Role.update(storageData, { - 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 - } - } + if (!role) { + throw '当前角色不存在' + } + let storageData = { name } + await models.Role.update(storageData, { + 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) { - try { - const { models } = ctx.fs.dc; - const { id } = ctx.request.body + try { + const { models } = ctx.fs.dc; + const { id } = ctx.request.body - await models.SalesDistribution.update({ - delete: false - }, - { - 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 - } - } + await models.SalesDistribution.update({ + delete: false + }, + { + 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 getUserResources(ctx) { + try { + const { adminHr } = ctx.fs.api.userInfo; + const { models } = ctx.fs.dc; + const { userId } = ctx.params; + let codes = []; + //人资管理员-所有权限 + if (adminHr.some(admin => admin.id == userId)) { + 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: userId } + }], + }, { + 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); + } + }) + } + ctx.status = 200; + ctx.body = codes; + } catch (error) { + ctx.fs.logger.error(`path:${ctx.path},error:${error}`) + ctx.status = 400; + ctx.body = { name: 'FindError', message: '获取用户角色资源失败' } + } +} module.exports = { - get, - add, - edit, - del, + get, + add, + edit, + del, + getUserResources } \ No newline at end of file diff --git a/api/app/lib/models/role_resource.js b/api/app/lib/models/role_resource.js index a4c74c8..9b0fcf6 100644 --- a/api/app/lib/models/role_resource.js +++ b/api/app/lib/models/role_resource.js @@ -39,5 +39,13 @@ module.exports = dc => { indexes: [] }); dc.models.RoleResource = RoleResource; + + const Role = dc.models.Role; + RoleResource.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' }); + Role.hasMany(RoleResource, { foreignKey: 'roleId', sourceKey: 'id' }); + + const Resource = dc.models.Resource; + RoleResource.belongsTo(Resource, { foreignKey: 'resId', targetKey: 'id' }); + Resource.hasMany(RoleResource, { foreignKey: 'resId', sourceKey: 'id' }); return RoleResource; }; \ No newline at end of file diff --git a/api/app/lib/routes/userRole/index.js b/api/app/lib/routes/userRole/index.js index 98bf549..9f043ee 100644 --- a/api/app/lib/routes/userRole/index.js +++ b/api/app/lib/routes/userRole/index.js @@ -4,11 +4,14 @@ const userRole = require('../../controllers/userRole'); module.exports = function (app, router, opts) { - app.fs.api.logAttr['GET/roleUser/list'] = { content: '角色用户列表', visible: true }; - router.get('/roleUser/list', userRole.get); + app.fs.api.logAttr['GET/roleUser/list'] = { content: '角色用户列表', visible: true }; + router.get('/roleUser/list', userRole.get); - app.fs.api.logAttr['POST/roleUser/add'] = { content: '角色添加用户成功', visible: true }; - router.post('/roleUser/add', userRole.add); + app.fs.api.logAttr['POST/roleUser/add'] = { content: '角色添加用户成功', visible: true }; + router.post('/roleUser/add', userRole.add); + + app.fs.api.logAttr['GET/user/:userId/role/resources'] = { content: '获取用户角色资源', visible: true }; + router.get('/user/:userId/role/resources', userRole.getUserResources); }; \ No newline at end of file