Browse Source

(*)角色数据范围入库

master
zmh 2 years ago
parent
commit
b8313cb8c7
  1. 2
      api/app/lib/controllers/role/index.js
  2. 84
      api/app/lib/controllers/roleResource/index.js

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

@ -31,7 +31,7 @@ async function add(ctx) {
throw '当前角色已存在' throw '当前角色已存在'
} }
let storageData = { name, delete: true } let storageData = { name, delete: false }
await models.Role.create(storageData) await models.Role.create(storageData)
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {

84
api/app/lib/controllers/roleResource/index.js

@ -3,50 +3,60 @@ 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;
const { id } = ctx.request.query const { id } = ctx.request.query
let roleResourceList = await models.RoleResource.findAndCountAll({ let roleResourceList = await models.RoleResource.findAndCountAll({
order: [['id', 'desc']], order: [['id', 'desc']],
where: { roleId: id } where: { roleId: id }
}); });
ctx.status = 200 ctx.status = 200
ctx.body = roleResourceList; ctx.body = roleResourceList;
} 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 { const transaction = await ctx.fs.dc.orm.transaction();
const { models } = ctx.fs.dc; try {
const { roleId, resourceId } = ctx.request.body const { models } = ctx.fs.dc;
await models.RoleResource.destroy({ const { roleId, resourceId, dataRange } = ctx.request.body
where: { roleId: roleId } await models.Role.update(
}) { dataRange: dataRange },
{
where: { id: roleId },
transaction
})
await models.RoleResource.destroy({
where: { roleId: roleId },
transaction
})
let storageData = resourceId.map(e => { let storageData = resourceId.map(e => {
return { return {
roleId: roleId, roleId: roleId,
resId: e resId: e
} }
}) })
await models.RoleResource.bulkCreate(storageData); await models.RoleResource.bulkCreate(storageData, { transaction });
ctx.status = 204; await transaction.commit();
} catch (error) { ctx.status = 204;
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); } catch (error) {
ctx.status = 400; await transaction.rollback();
ctx.body = { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
message: typeof error == 'string' ? error : undefined ctx.status = 400;
} ctx.body = {
} message: typeof error == 'string' ? error : undefined
}
}
} }
module.exports = { module.exports = {
get, get,
add, add,
} }
Loading…
Cancel
Save