SysRoleController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Attribute;
  2. using Common;
  3. using Dto;
  4. using Enums;
  5. using Extensions;
  6. using Filters;
  7. using Infrastructure;
  8. using Infrastructure.Model;
  9. using Mapster;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Middleware;
  12. using Model;
  13. using Model.Base;
  14. using Services;
  15. using Util;
  16. namespace Controllers
  17. {
  18. /// <summary>
  19. /// 角色信息
  20. /// </summary>
  21. [Verify]
  22. public class SysRoleController : BaseController
  23. {
  24. private readonly ISysRoleService sysRoleService;
  25. private readonly ISysMenuService sysMenuService;
  26. public SysRoleController(
  27. ISysRoleService sysRoleService,
  28. ISysMenuService sysMenuService)
  29. {
  30. this.sysRoleService = sysRoleService;
  31. this.sysMenuService = sysMenuService;
  32. }
  33. /// <summary>
  34. /// 获取系统角色管理
  35. /// </summary>
  36. /// <returns></returns>
  37. [HttpGet("/v1/tel/sysServer/role/page")]
  38. public IActionResult List([FromQuery] SysRole role, [FromQuery] PagerInfo pager)
  39. {
  40. var list = sysRoleService.SelectRoleList(role, pager);
  41. return SUCCESS(list, TIME_FORMAT_FULL);
  42. }
  43. /// <summary>
  44. /// 获取所有角色列表
  45. /// </summary>
  46. /// <returns></returns>
  47. [HttpGet("/v1/tel/sysServer/role/list")]
  48. public IActionResult AllList()
  49. {
  50. return SUCCESS(sysRoleService.GetList(), TIME_FORMAT_FULL);
  51. }
  52. /// <summary>
  53. /// 根据角色编号获取详细信息
  54. /// </summary>
  55. /// <param name="roleId"></param>
  56. /// <returns></returns>
  57. [HttpGet("{roleId}")]
  58. public IActionResult GetInfo(long roleId = 0)
  59. {
  60. var info = sysRoleService.SelectRoleById(roleId);
  61. return SUCCESS(info, TIME_FORMAT_FULL);
  62. }
  63. /// <summary>
  64. /// 添加角色
  65. /// </summary>
  66. /// <param name="dto"></param>
  67. /// <returns></returns>
  68. [HttpPost("/v1/tel/sysServer/role/add")]
  69. public IActionResult RoleAdd([FromBody] SysRoleDto dto)
  70. {
  71. SysRole sysRoleDto = dto.Adapt<SysRole>();
  72. if (UserConstants.NOT_UNIQUE.Equals(sysRoleService.CheckRoleKeyUnique(sysRoleDto)))
  73. {
  74. return ToResponse(ApiResult.Error((int)ResultCode.CUSTOM_ERROR, $"新增角色'{sysRoleDto.roleName}'失败,角色权限已存在"));
  75. }
  76. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  77. sysRoleDto.CreateBy = loginUser.username;
  78. sysRoleDto.UpdateBy = loginUser.username;
  79. sysRoleDto.DelFlag = "0";
  80. long roleId = sysRoleService.InsertRole(sysRoleDto);
  81. return SUCCESS(roleId);
  82. }
  83. /// <summary>
  84. /// 修改角色
  85. /// </summary>
  86. /// <param name="dto"></param>
  87. /// <returns></returns>
  88. [HttpPut("/v1/tel/sysServer/role/update")]
  89. public IActionResult RoleEdit([FromBody] SysRoleDto dto)
  90. {
  91. if (dto == null || dto.roleId <= 0)
  92. {
  93. return ToResponse(ApiResult.Error(101, "请求参数错误"));
  94. }
  95. SysRole sysRoleDto = dto.Adapt<SysRole>();
  96. sysRoleService.CheckRoleAllowed(sysRoleDto);
  97. var info = sysRoleService.SelectRoleById(sysRoleDto.roleId);
  98. if (info != null && info.roleCode != sysRoleDto.roleCode)
  99. {
  100. if (UserConstants.NOT_UNIQUE.Equals(sysRoleService.CheckRoleKeyUnique(sysRoleDto)))
  101. {
  102. return ToResponse(ApiResult.Error($"编辑角色'{sysRoleDto.roleName}'失败,角色权限已存在"));
  103. }
  104. }
  105. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  106. sysRoleDto.UpdateBy = loginUser.username;
  107. int upResult = sysRoleService.UpdateRole(sysRoleDto);
  108. if (upResult > 0)
  109. {
  110. return SUCCESS(upResult);
  111. }
  112. return SUCCESS(ApiResult.Error($"修改角色'{sysRoleDto.roleName}'失败,请联系管理员"));
  113. }
  114. /// <summary>
  115. /// 根据角色分配菜单
  116. /// </summary>
  117. /// <param name="sysRoleDto"></param>
  118. /// <returns></returns>
  119. [HttpPut("/v1/tel/sysServer/role/menu")]
  120. public IActionResult DataScope([FromBody] SysRoleDto sysRoleDto)
  121. {
  122. if (sysRoleDto == null || sysRoleDto.roleId <= 0) return ToResponse(ApiResult.Error(101, "请求参数错误"));
  123. SysRole sysRole = sysRoleDto.Adapt<SysRole>();
  124. sysRoleDto.createBy = HttpContext.GetName();
  125. sysRoleService.CheckRoleAllowed(sysRole);
  126. bool result = sysRoleService.AuthDataScope(sysRoleDto);
  127. return SUCCESS(result);
  128. }
  129. /// <summary>
  130. /// 角色删除
  131. /// </summary>
  132. /// <param name="roleId"></param>
  133. /// <returns></returns>
  134. [HttpDelete("/v1/tel/sysServer/role/removeById/{id}")]
  135. public IActionResult Remove(int id)
  136. {
  137. long[] roleIds = { id };
  138. int result = sysRoleService.DeleteRoleByRoleId(roleIds);
  139. return SUCCESS(result);
  140. }
  141. /// <summary>
  142. /// 修改角色状态
  143. /// </summary>
  144. /// <param name="roleDto">角色对象</param>
  145. /// <returns></returns>
  146. [HttpPut("changeStatus")]
  147. [Log(Title = "修改角色状态", BusinessType = BusinessType.UPDATE)]
  148. [ActionPermissionFilter(Permission = "system:role:edit")]
  149. public IActionResult ChangeStatus([FromBody] SysRole roleDto)
  150. {
  151. sysRoleService.CheckRoleAllowed(roleDto);
  152. int result = sysRoleService.UpdateRoleStatus(roleDto);
  153. return ToResponse(result);
  154. }
  155. /// <summary>
  156. /// 角色导出
  157. /// </summary>
  158. /// <returns></returns>
  159. [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "角色导出")]
  160. [HttpGet("export")]
  161. [ActionPermissionFilter(Permission = "system:role:export")]
  162. public IActionResult Export()
  163. {
  164. var list = sysRoleService.SelectRoleAll();
  165. string sFileName = ExportExcel(list, "sysrole", "角色");
  166. return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
  167. }
  168. /// <summary>
  169. /// 导出角色菜单
  170. /// </summary>
  171. /// <param name="roleId"></param>
  172. /// <returns></returns>
  173. [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "角色菜单导出")]
  174. [HttpGet("exportRoleMenu")]
  175. [ActionPermissionFilter(Permission = "system:role:export")]
  176. public IActionResult ExportRoleMenu(int roleId)
  177. {
  178. MenuQueryDto dto = new() { delFlag = "0", menuTypeIds = "M,C,F" };
  179. var list = sysMenuService.SelectRoleMenuListByRole(dto, roleId);
  180. var result = ExportExcelMini(list, roleId.ToString(), "角色菜单");
  181. return ExportExcel(result.Item2, result.Item1);
  182. }
  183. }
  184. }