SysMenuController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Attribute;
  2. using Dto;
  3. using Enums;
  4. using Extensions;
  5. using Filters;
  6. using Infrastructure;
  7. using Infrastructure.Model;
  8. using Mapster;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Middleware;
  11. using Model;
  12. using Model.Base;
  13. using Services;
  14. using Util;
  15. namespace Controllers
  16. {
  17. /// <summary>
  18. /// 系统菜单
  19. /// </summary>
  20. [ApiExplorerSettings(GroupName = "sys")]
  21. public class SysMenuController : BaseController
  22. {
  23. private readonly ISysRoleService sysRoleService;
  24. private readonly ISysMenuService sysMenuService;
  25. private readonly ISysRoleMenuService sysRoleMenuService;
  26. public SysMenuController(
  27. ISysRoleService sysRoleService,
  28. ISysMenuService sysMenuService,
  29. ISysRoleMenuService sysRoleMenuService)
  30. {
  31. this.sysRoleService = sysRoleService;
  32. this.sysMenuService = sysMenuService;
  33. this.sysRoleMenuService = sysRoleMenuService;
  34. }
  35. /// <summary>
  36. /// 获取菜单树列表
  37. /// </summary>
  38. /// <returns></returns>
  39. [HttpGet("/v1/tel/sysServer/menu/tree")]
  40. public IActionResult TreeMenuList([FromQuery] MenuQueryDto menu)
  41. {
  42. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  43. long userId = loginUser.userId;
  44. return SUCCESS(sysMenuService.SelectTreeMenuList(menu, userId), "yyyy-MM-dd HH:mm:ss");
  45. }
  46. /// <summary>
  47. /// 获取菜单列表
  48. /// </summary>
  49. /// <returns></returns>
  50. [ActionPermissionFilter(Permission = "system:menu:list")]
  51. [HttpGet("list")]
  52. public IActionResult MenuList([FromQuery] MenuQueryDto menu)
  53. {
  54. long userId = HttpContext.GetUId();
  55. return SUCCESS(sysMenuService.SelectMenuList(menu, userId), "yyyy-MM-dd HH:mm:ss");
  56. }
  57. /// <summary>
  58. /// 根据菜单编号获取详细信息
  59. /// </summary>
  60. /// <param name="menuId"></param>
  61. /// <returns></returns>
  62. [HttpGet("{menuId}")]
  63. [ActionPermissionFilter(Permission = "system:menu:query")]
  64. public IActionResult GetMenuInfo(int menuId = 0)
  65. {
  66. return SUCCESS(sysMenuService.GetMenuByMenuId(menuId), "yyyy-MM-dd HH:mm:ss");
  67. }
  68. /// <summary>
  69. /// 根据菜单编号获取菜单列表,菜单管理首次进入
  70. /// </summary>
  71. /// <param name="menuId"></param>
  72. /// <returns></returns>
  73. [HttpGet("list/{menuId}")]
  74. [ActionPermissionFilter(Permission = "system:menu:query")]
  75. public IActionResult GetMenuList(int menuId = 0)
  76. {
  77. long userId = HttpContext.GetUId();
  78. return SUCCESS(sysMenuService.GetMenusByMenuId(menuId, userId), "yyyy-MM-dd HH:mm:ss");
  79. }
  80. /// <summary>
  81. /// 获取角色菜单信息
  82. /// 加载对应角色菜单列表树
  83. /// </summary>
  84. /// <param name="roleId"></param>
  85. /// <returns></returns>
  86. [HttpGet("/v1/tel/sysServer/menu/getRoleTree")]
  87. public IActionResult RoleMenuTreeselect([FromQuery] RoleMenuQueryDto menu)
  88. {
  89. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  90. var menus = sysMenuService.SelectMenuList(new MenuQueryDto(), loginUser.userId);
  91. var setCheckedKeys = sysRoleService.SelectUserRoleMenus(menu.roleId);
  92. return SUCCESS(new
  93. {
  94. setCheckedKeys,
  95. menus = sysMenuService.BuildMenuTreeSelect(menus),
  96. });
  97. }
  98. [Verify]
  99. [HttpGet("/v1/tel/sysServer/menu/getUserMenu")]
  100. public IActionResult GetMenus()
  101. {
  102. long userId = HttpContext.GetUId();
  103. var menus = sysMenuService.SelectMenuList(new MenuQueryDto(), userId);
  104. return SUCCESS(sysMenuService.BuildLeftMenuTreeSelect(menus));
  105. }
  106. /// <summary>
  107. /// 修改菜单
  108. /// </summary>
  109. /// <param name="menuDto"></param>
  110. /// <returns></returns>
  111. [HttpPut("/v1/tel/sysServer/menu/update")]
  112. public IActionResult MenuEdit([FromBody] MenuDto menuDto)
  113. {
  114. if (menuDto == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  115. //if (UserConstants.NOT_UNIQUE.Equals(sysMenuService.CheckMenuNameUnique(MenuDto)))
  116. //{
  117. // return ToResponse(ApiResult.Error($"修改菜单'{MenuDto.menuName}'失败,菜单名称已存在"));
  118. //}
  119. var config = new TypeAdapterConfig();
  120. //映射规则
  121. config.ForType<SysMenu, MenuDto>()
  122. .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);//忽略字段名称的大小写;//忽略除以上配置的所有字段
  123. var modal = menuDto.Adapt<SysMenu>(config).ToUpdate(HttpContext);
  124. if (modal.menuType == "3" && !modal.path.StartsWith("http"))
  125. {
  126. return ToResponse(ApiResult.Error($"修改菜单'{modal.name}'失败,地址必须以http(s)://开头"));
  127. }
  128. if (modal.menuId.Equals(modal.parentId))
  129. {
  130. return ToResponse(ApiResult.Error($"修改菜单'{modal.name}'失败,上级菜单不能选择自己"));
  131. }
  132. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  133. modal.UpdateBy = loginUser.username;
  134. long result = sysMenuService.EditMenu(modal);
  135. return SUCCESS(result);
  136. }
  137. /// <summary>
  138. /// 添加菜单
  139. /// </summary>
  140. /// <param name="menuDto"></param>
  141. /// <returns></returns>
  142. [HttpPost("/v1/tel/sysServer/menu/add")]
  143. public IActionResult MenuAdd([FromBody] MenuDto menuDto)
  144. {
  145. var config = new TypeAdapterConfig();
  146. //映射规则
  147. config.ForType<SysMenu, MenuDto>()
  148. .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);
  149. var menu = menuDto.Adapt<SysMenu>(config).ToCreate(HttpContext);
  150. if (menu == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  151. if (UserConstants.NOT_UNIQUE.Equals(sysMenuService.CheckMenuNameUnique(menu)))
  152. {
  153. return ToResponse(ApiResult.Error($"新增菜单'{menu.name}'失败,菜单名称已存在"));
  154. }
  155. if (menu.menuType == "3" && !menu.path.StartsWith("http"))
  156. {
  157. return ToResponse(ApiResult.Error($"新增菜单'{menu.name}'失败,地址必须以http(s)://开头"));
  158. }
  159. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  160. menu.CreateBy = loginUser.username;
  161. menu.UpdateBy = loginUser.username;
  162. menu.DelFlag = "0";
  163. long result = sysMenuService.AddMenu(menu);
  164. return SUCCESS(result);
  165. }
  166. /// <summary>
  167. /// 菜单删除
  168. /// </summary>
  169. /// <param name="menuId"></param>
  170. /// <returns></returns>
  171. [HttpDelete("/v1/tel/sysServer/menu/removeById/{id}")]
  172. public IActionResult Remove(int id)
  173. {
  174. if (sysMenuService.HasChildByMenuId(id))
  175. {
  176. return ToResponse(ResultCode.CUSTOM_ERROR, "存在子菜单,不允许删除");
  177. }
  178. if (sysRoleMenuService.CheckMenuExistRole(id))
  179. {
  180. return ToResponse(ResultCode.CUSTOM_ERROR, "菜单已分配,不允许删除");
  181. }
  182. int result = sysMenuService.DeleteMenuById(id);
  183. return SUCCESS(result);
  184. }
  185. /// <summary>
  186. /// 保存排序
  187. /// </summary>
  188. /// <param name="id"></param>
  189. /// <param name="value"></param>
  190. /// <returns></returns>
  191. [ActionPermissionFilter(Permission = "system:menu:update")]
  192. [HttpGet("ChangeSort")]
  193. [Log(Title = "保存排序", BusinessType = BusinessType.UPDATE)]
  194. public IActionResult ChangeSort(int id = 0, int value = 0)
  195. {
  196. MenuDto MenuDto = new()
  197. {
  198. menuId = id,
  199. sortOrder = value
  200. };
  201. if (MenuDto == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  202. int result = sysMenuService.ChangeSortMenu(MenuDto);
  203. return ToResponse(result);
  204. }
  205. }
  206. }