|
|
@ -3,50 +3,60 @@ const moment = require('moment') |
|
|
|
const fs = require('fs'); |
|
|
|
|
|
|
|
async function get(ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { id } = ctx.request.query |
|
|
|
let roleResourceList = await models.RoleResource.findAndCountAll({ |
|
|
|
order: [['id', 'desc']], |
|
|
|
where: { roleId: id } |
|
|
|
}); |
|
|
|
ctx.status = 200 |
|
|
|
ctx.body = roleResourceList; |
|
|
|
} 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; |
|
|
|
const { id } = ctx.request.query |
|
|
|
let roleResourceList = await models.RoleResource.findAndCountAll({ |
|
|
|
order: [['id', 'desc']], |
|
|
|
where: { roleId: id } |
|
|
|
}); |
|
|
|
ctx.status = 200 |
|
|
|
ctx.body = roleResourceList; |
|
|
|
} 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 { roleId, resourceId } = ctx.request.body |
|
|
|
await models.RoleResource.destroy({ |
|
|
|
where: { roleId: roleId } |
|
|
|
}) |
|
|
|
const transaction = await ctx.fs.dc.orm.transaction(); |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { roleId, resourceId, dataRange } = ctx.request.body |
|
|
|
await models.Role.update( |
|
|
|
{ dataRange: dataRange }, |
|
|
|
{ |
|
|
|
where: { id: roleId }, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
await models.RoleResource.destroy({ |
|
|
|
where: { roleId: roleId }, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
|
|
|
|
let storageData = resourceId.map(e => { |
|
|
|
return { |
|
|
|
roleId: roleId, |
|
|
|
resId: e |
|
|
|
} |
|
|
|
}) |
|
|
|
await models.RoleResource.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 = resourceId.map(e => { |
|
|
|
return { |
|
|
|
roleId: roleId, |
|
|
|
resId: e |
|
|
|
} |
|
|
|
}) |
|
|
|
await models.RoleResource.bulkCreate(storageData, { 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 = { |
|
|
|
get, |
|
|
|
add, |
|
|
|
get, |
|
|
|
add, |
|
|
|
} |