|
|
@ -3,96 +3,119 @@ const moment = require('moment') |
|
|
|
const fs = require('fs'); |
|
|
|
|
|
|
|
async function get(ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
let roleList = await models.Role.findAndCountAll({ |
|
|
|
where: { delete: true }, |
|
|
|
order: [['id', 'desc']], |
|
|
|
}); |
|
|
|
ctx.status = 200 |
|
|
|
ctx.body = roleList; |
|
|
|
} 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 roleList = await models.Role.findAndCountAll({ |
|
|
|
where: { delete: false }, |
|
|
|
order: [['id', 'desc']], |
|
|
|
}); |
|
|
|
ctx.status = 200 |
|
|
|
ctx.body = roleList; |
|
|
|
} 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 { name } = ctx.request.body |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { name } = ctx.request.body |
|
|
|
|
|
|
|
const role = await models.Role.findOne({ |
|
|
|
where: { name } |
|
|
|
}) |
|
|
|
const role = await models.Role.findOne({ |
|
|
|
where: { name: name, delete: false } |
|
|
|
}) |
|
|
|
|
|
|
|
if (role && !role.delete) { |
|
|
|
throw '当前角色已存在' |
|
|
|
} |
|
|
|
if (role) { |
|
|
|
throw '当前角色已存在' |
|
|
|
} |
|
|
|
|
|
|
|
let storageData = { name ,delete:true} |
|
|
|
await models.Role.create(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 = { name, delete: true } |
|
|
|
await models.Role.create(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 oldRole = 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 (!oldRole) { |
|
|
|
throw '当前角色不存在' |
|
|
|
} |
|
|
|
|
|
|
|
const role = await models.Role.findOne({ |
|
|
|
where: { name: name, id: { $not: id }, delete: false } |
|
|
|
}) |
|
|
|
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.query |
|
|
|
const transaction = await ctx.fs.dc.orm.transaction(); |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { id } = ctx.query |
|
|
|
|
|
|
|
await models.Role.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.Role.update({ delete: true }, |
|
|
|
{ |
|
|
|
where: { id }, |
|
|
|
transaction |
|
|
|
}) |
|
|
|
await models.RoleResource.destroy({ |
|
|
|
where: { |
|
|
|
roleId: id |
|
|
|
}, |
|
|
|
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 = { |
|
|
|
get, |
|
|
|
add, |
|
|
|
edit, |
|
|
|
del, |
|
|
|
get, |
|
|
|
add, |
|
|
|
edit, |
|
|
|
del, |
|
|
|
} |