role.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import BaseController from '../../base';
  2. import { AdminRoute } from '../../../decorator/router_register';
  3. import { PageGetDto } from '../../../dto/comm';
  4. import { DeleteRoleDto, CreateRoleDto, UpdateRoleDto, InfoRoleDto } from '../../../dto/admin/sys/role';
  5. export default class SysRoleController extends BaseController {
  6. /**
  7. * @api {get} /admin/sys/role/list 获取全部系统角色列表
  8. * @apiGroup 系统角色
  9. * @apiUse Auth
  10. * @apiUse BaseRes
  11. * @apiSuccess {SysRole[]} data 角色列表
  12. */
  13. @AdminRoute('/sys/role/list', 'get')
  14. async list() {
  15. this.res({
  16. data: await this.service.admin.sys.role.list(),
  17. });
  18. }
  19. /**
  20. * @api {get} /admin/sys/role/page 获取系统角色列表
  21. * @apiGroup 系统角色
  22. * @apiUse Auth
  23. * @apiUse BaseRes
  24. * @apiUse Page
  25. * @apiSuccess {SysRole[]} data.list 角色列表
  26. */
  27. @AdminRoute('/sys/role/page', 'get')
  28. async page() {
  29. const dto = await this.ctx.validate<PageGetDto>(PageGetDto, this.getQuery());
  30. this.res({
  31. data: {
  32. list: await this.service.admin.sys.role.page(dto.current - 1, dto.pageSize),
  33. pagination: {
  34. page: dto.current,
  35. size: dto.pageSize,
  36. total: await this.service.admin.sys.role.count(),
  37. },
  38. },
  39. });
  40. }
  41. /**
  42. * @api {post} /admin/sys/role/delete 删除角色
  43. * @apiGroup 系统角色
  44. * @apiUse Auth
  45. * @apiUse BaseRes
  46. * @apiParam {Number[]} roleIds 角色编号列表
  47. */
  48. @AdminRoute('/sys/role/delete', 'post')
  49. async delete() {
  50. const dto = await this.ctx.validate<DeleteRoleDto>(DeleteRoleDto);
  51. const count = await this.service.admin.sys.role.countUserIdByRole(dto.roleIds);
  52. if (count > 0) {
  53. this.res({
  54. code: 10008,
  55. });
  56. return;
  57. }
  58. await this.service.admin.sys.role.delete(dto.roleIds);
  59. await this.service.admin.sys.menu.refreshOnlineUserPerms();
  60. this.res();
  61. }
  62. /**
  63. * @api {post} /admin/sys/role/add 新增角色
  64. * @apiGroup 系统角色
  65. * @apiUse Auth
  66. * @apiUse BaseRes
  67. * @apiParam {String} name 角色名称
  68. * @apiParam {String} label 角色表示
  69. * @apiParam {String} remark 备注
  70. * @apiParam {Number[]} menus 关联菜单ID列表
  71. * @apiParam {Number[]} depts 关联部门ID列表
  72. */
  73. @AdminRoute('/sys/role/add', 'post')
  74. async add() {
  75. const dto = await this.ctx.validate<CreateRoleDto>(CreateRoleDto);
  76. this.res({
  77. data: await this.service.admin.sys.role.add(dto, this.ctx.token.uid),
  78. });
  79. }
  80. /**
  81. * @api {post} /admin/sys/role/update 更新角色
  82. * @apiGroup 系统角色
  83. * @apiUse Auth
  84. * @apiUse BaseRes
  85. * @apiParam {String} name 角色名称
  86. * @apiParam {String} label 角色表示
  87. * @apiParam {String} remark 备注
  88. * @apiParam {Number[]} menus 关联菜单ID列表
  89. * @apiParam {Number[]} depts 关联部门ID列表
  90. * @apiParam {Number} roleId 角色编号
  91. */
  92. @AdminRoute('/sys/role/update', 'post')
  93. async update() {
  94. const dto = await this.ctx.validate<UpdateRoleDto>(UpdateRoleDto);
  95. await this.service.admin.sys.role.update(dto);
  96. await this.service.admin.sys.menu.refreshOnlineUserPerms();
  97. this.res();
  98. }
  99. /**
  100. * @api {get} /admin/sys/role/list 获取全部系统角色列表
  101. * @apiGroup 系统角色
  102. * @apiUse Auth
  103. * @apiUse BaseRes
  104. * @apiSuccess {SysRole} data 角色信息
  105. */
  106. @AdminRoute('/sys/role/info', 'get')
  107. async info() {
  108. const dto = await this.ctx.validate<InfoRoleDto>(InfoRoleDto, this.getQuery());
  109. this.res({
  110. data: await this.service.admin.sys.role.info(parseInt(dto.roleId)),
  111. });
  112. }
  113. // 新增 获取所有role按钮
  114. @AdminRoute('/role_menu', 'get')
  115. async role_menu() {
  116. this.res({
  117. data: await this.service.admin.sys.role.role_menu(),
  118. });
  119. }
  120. /**
  121. *通过角色id获取menu
  122. */
  123. @AdminRoute('/menu_by_roleid', 'get')
  124. async menu_by_roleid() {
  125. const dto = await this.ctx.validate<InfoRoleDto>(InfoRoleDto, this.getQuery());
  126. this.res({
  127. data: await this.service.admin.sys.role.menu_by_roleid(parseInt(dto.roleId)),
  128. });
  129. }
  130. }