SysMenuController.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package com.boman.system.controller;
  2. import java.util.List;
  3. import com.boman.domain.SysMenu;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.validation.annotation.Validated;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.boman.domain.constant.Constants;
  15. import com.boman.domain.constant.UserConstants;
  16. import com.boman.common.core.utils.SecurityUtils;
  17. import com.boman.common.core.utils.StringUtils;
  18. import com.boman.common.core.web.controller.BaseController;
  19. import com.boman.domain.dto.AjaxResult;
  20. import com.boman.common.log.annotation.Log;
  21. import com.boman.common.log.enums.BusinessType;
  22. import com.boman.common.security.annotation.PreAuthorize;
  23. import com.boman.system.service.ISysMenuService;
  24. /**
  25. * 菜单信息
  26. *
  27. * @author ruoyi
  28. */
  29. @RestController
  30. @RequestMapping("/menu")
  31. public class SysMenuController extends BaseController
  32. {
  33. @Autowired
  34. private ISysMenuService menuService;
  35. /**
  36. * 获取菜单列表
  37. */
  38. @PreAuthorize(hasPermi = "system:menu:list")
  39. @GetMapping("/list")
  40. public AjaxResult list(SysMenu menu)
  41. {
  42. Long userId = SecurityUtils.getUserId();
  43. List<SysMenu> menus = menuService.selectMenuList(menu, userId);
  44. return AjaxResult.success(menus);
  45. }
  46. /**
  47. * 获取所有菜单列表
  48. */
  49. @PostMapping("/listAll")
  50. public List<SysMenu> listAll(@RequestBody SysMenu menu)
  51. {
  52. return menuService.selectMenuListAll(menu);
  53. }
  54. /**
  55. * 获取某人的所有菜单
  56. */
  57. @GetMapping("/listMenusByUserId/{userId}")
  58. public List<SysMenu> list(@PathVariable Long userId) {
  59. return menuService.selectMenuList(new SysMenu(), userId);
  60. }
  61. /**
  62. * 根据菜单编号获取详细信息
  63. */
  64. @PreAuthorize(hasPermi = "system:menu:query")
  65. @GetMapping(value = "/{id}")
  66. public AjaxResult getInfo(@PathVariable Long id)
  67. {
  68. return AjaxResult.success(menuService.selectMenuById(id));
  69. }
  70. /**
  71. * 获取菜单下拉树列表
  72. */
  73. @GetMapping("/treeselect")
  74. public AjaxResult treeselect(SysMenu menu)
  75. {
  76. Long userId = SecurityUtils.getUserId();
  77. List<SysMenu> menus = menuService.selectMenuList(menu, userId);
  78. return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
  79. }
  80. /**
  81. * 加载对应角色菜单列表树, 不包含叶子结点 不包含叶子结点 不包含叶子结点 重要的事情说三遍
  82. */
  83. @GetMapping(value = "/treeMenuNotAddLeafNode/{roleId}")
  84. public AjaxResult treeMenuNotAddLeafNode(@PathVariable("roleId") Long roleId)
  85. {
  86. Long userId = SecurityUtils.getUserId();
  87. List<SysMenu> menus = menuService.selectMenuList(userId);
  88. AjaxResult ajax = AjaxResult.success();
  89. ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
  90. ajax.put("menus", menuService.buildMenuTreeSelectNotAddLeafNode(menus));
  91. return ajax;
  92. }
  93. /**
  94. * 功能描述: before: 根据roleId查找的叶子结点去匹配所对应的菜单树,
  95. * after: 不需要传roleId,查全部的,这样可以保证所有的角色都可以拿到所有的的菜单
  96. *
  97. * @param roleId roleId
  98. * @return com.boman.domain.dto.AjaxResult
  99. */
  100. @GetMapping(value = "/listTreeByRoleId/{roleId}")
  101. public AjaxResult listTreeByRoleId(@PathVariable("roleId") Long roleId) {
  102. return AjaxResult.success(menuService.listTreeByRoleId(null));
  103. }
  104. /**
  105. * 功能描述: 根据菜单id,找到此菜单以及子菜单下所有的叶子节点
  106. *
  107. * @param menuId menuId
  108. * @return com.boman.domain.dto.AjaxResult
  109. */
  110. /**
  111. * 功能描述: 根据菜单id,找到此菜单以及子菜单下所有的叶子节点
  112. *
  113. * @param menuId menuId
  114. * @return com.boman.domain.dto.AjaxResult
  115. */
  116. @GetMapping(value = "/allLeafNodeById/{menuId}")
  117. public AjaxResult allLeafNodeById(@PathVariable("menuId") Long menuId) {
  118. return AjaxResult.success(menuService.allLeafNodeById(menuId));
  119. }
  120. /**
  121. * 功能描述: 根据menuId和roleId,找到此菜单以及子菜单下所有的叶子节点, 同时匹配叶子结点对应的头部的规则
  122. *
  123. * @param menuId menuId
  124. * @param roleId roleIdmenu/listTreeByRoleId/1
  125. * @return com.boman.domain.dto.AjaxResult
  126. */
  127. @GetMapping(value = "/listMenus/roleId/{roleId}/menuId/{menuId}")
  128. public AjaxResult listMenus(@PathVariable("roleId") Long roleId, @PathVariable(value = "menuId", required = false) Long menuId) {
  129. return AjaxResult.success(menuService.listMenus(roleId, menuId));
  130. }
  131. /**
  132. * 新增菜单
  133. */
  134. @PreAuthorize(hasPermi = "system:menu:add")
  135. @Log(title = "菜单管理", businessType = BusinessType.INSERT)
  136. @PostMapping
  137. public AjaxResult add(@Validated @RequestBody SysMenu menu)
  138. {
  139. if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
  140. {
  141. return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  142. }
  143. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
  144. && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
  145. {
  146. return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  147. }
  148. menu.setCreateBy(SecurityUtils.getUsername());
  149. return menuService.insertMenu(menu);
  150. }
  151. /**
  152. * 修改菜单
  153. */
  154. @PreAuthorize(hasPermi = "system:menu:edit")
  155. @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
  156. @PutMapping
  157. public AjaxResult edit(@Validated @RequestBody SysMenu menu)
  158. {
  159. if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
  160. {
  161. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  162. }
  163. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
  164. && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
  165. {
  166. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  167. }
  168. else if (menu.getId().equals(menu.getParentId()))
  169. {
  170. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
  171. }
  172. menu.setUpdateBy(SecurityUtils.getUsername());
  173. return toAjax(menuService.updateMenu(menu));
  174. }
  175. /**
  176. * 删除菜单
  177. */
  178. @PreAuthorize(hasPermi = "system:menu:remove")
  179. @Log(title = "菜单管理", businessType = BusinessType.DELETE)
  180. @DeleteMapping("/{id}")
  181. public AjaxResult remove(@PathVariable("id") Long id)
  182. {
  183. if (menuService.hasChildById(id))
  184. {
  185. return AjaxResult.error("存在子菜单,不允许删除");
  186. }
  187. if (menuService.checkMenuExistRole(id))
  188. {
  189. return AjaxResult.error("菜单已分配,不允许删除");
  190. }
  191. return toAjax(menuService.deleteMenuById(id));
  192. }
  193. /**
  194. * 逻辑删除菜单
  195. */
  196. @PreAuthorize(hasPermi = "system:menu:remove")
  197. @Log(title = "菜单管理", businessType = BusinessType.DELETE)
  198. @DeleteMapping("/remove/{id}")
  199. public AjaxResult delete(@PathVariable("id") Long id)
  200. {
  201. if (menuService.hasChildById(id))
  202. {
  203. return AjaxResult.error("存在子菜单,不允许删除");
  204. }
  205. if (menuService.checkMenuExistRole(id))
  206. {
  207. return AjaxResult.error("菜单已分配,不允许删除");
  208. }
  209. return toAjax(menuService.removeMenuById(id));
  210. }
  211. /**
  212. * 获取路由信息
  213. *
  214. * @return 路由信息
  215. */
  216. @GetMapping("getRouters")
  217. public AjaxResult getRouters()
  218. {
  219. Long userId = SecurityUtils.getUserId();
  220. List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
  221. return AjaxResult.success(menuService.buildMenus(menus));
  222. }
  223. /**
  224. * 根据用户ID查询按钮
  225. *
  226. * @param userId 用户ID
  227. * @return 菜单列表
  228. */
  229. @GetMapping("listBtnByUserId/{userId}")
  230. public List<SysMenu> listBtnByUserId(@PathVariable("userId") Long userId) {
  231. return menuService.listBtnByUserId(userId);
  232. }
  233. }