SysMenuController.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.boman.system.controller;
  2. import java.util.List;
  3. import com.boman.system.api.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.common.core.constant.Constants;
  15. import com.boman.common.core.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.common.core.web.domain.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. @PreAuthorize(hasPermi = "system:menu:query")
  50. @GetMapping(value = "/{id}")
  51. public AjaxResult getInfo(@PathVariable Long id)
  52. {
  53. return AjaxResult.success(menuService.selectMenuById(id));
  54. }
  55. /**
  56. * 获取菜单下拉树列表
  57. */
  58. @GetMapping("/treeselect")
  59. public AjaxResult treeselect(SysMenu menu)
  60. {
  61. Long userId = SecurityUtils.getUserId();
  62. List<SysMenu> menus = menuService.selectMenuList(menu, userId);
  63. return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
  64. }
  65. /**
  66. * 加载对应角色菜单列表树, 不包含叶子结点 不包含叶子结点 不包含叶子结点 重要的事情说三遍
  67. */
  68. @GetMapping(value = "/treeMenuNotAddLeafNode/{roleId}")
  69. public AjaxResult treeMenuNotAddLeafNode(@PathVariable("roleId") Long roleId)
  70. {
  71. Long userId = SecurityUtils.getUserId();
  72. List<SysMenu> menus = menuService.selectMenuList(userId);
  73. AjaxResult ajax = AjaxResult.success();
  74. ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
  75. ajax.put("menus", menuService.buildMenuTreeSelectNotAddLeafNode(menus));
  76. return ajax;
  77. }
  78. @GetMapping(value = "/allLeafNodeById/{menuId}")
  79. public AjaxResult allLeafNodeById(@PathVariable("menuId") Long menuId) {
  80. return AjaxResult.success(menuService.allLeafNodeById(menuId));
  81. }
  82. /**
  83. * 新增菜单
  84. */
  85. @PreAuthorize(hasPermi = "system:menu:add")
  86. @Log(title = "菜单管理", businessType = BusinessType.INSERT)
  87. @PostMapping
  88. public AjaxResult add(@Validated @RequestBody SysMenu menu)
  89. {
  90. if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
  91. {
  92. return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  93. }
  94. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
  95. && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
  96. {
  97. return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  98. }
  99. menu.setCreateBy(SecurityUtils.getUsername());
  100. return menuService.insertMenu(menu);
  101. }
  102. /**
  103. * 修改菜单
  104. */
  105. @PreAuthorize(hasPermi = "system:menu:edit")
  106. @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
  107. @PutMapping
  108. public AjaxResult edit(@Validated @RequestBody SysMenu menu)
  109. {
  110. if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
  111. {
  112. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  113. }
  114. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
  115. && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS))
  116. {
  117. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  118. }
  119. else if (menu.getId().equals(menu.getParentId()))
  120. {
  121. return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
  122. }
  123. menu.setUpdateBy(SecurityUtils.getUsername());
  124. return toAjax(menuService.updateMenu(menu));
  125. }
  126. /**
  127. * 删除菜单
  128. */
  129. @PreAuthorize(hasPermi = "system:menu:remove")
  130. @Log(title = "菜单管理", businessType = BusinessType.DELETE)
  131. @DeleteMapping("/{id}")
  132. public AjaxResult remove(@PathVariable("id") Long id)
  133. {
  134. if (menuService.hasChildById(id))
  135. {
  136. return AjaxResult.error("存在子菜单,不允许删除");
  137. }
  138. if (menuService.checkMenuExistRole(id))
  139. {
  140. return AjaxResult.error("菜单已分配,不允许删除");
  141. }
  142. return toAjax(menuService.deleteMenuById(id));
  143. }
  144. /**
  145. * 获取路由信息
  146. *
  147. * @return 路由信息
  148. */
  149. @GetMapping("getRouters")
  150. public AjaxResult getRouters()
  151. {
  152. Long userId = SecurityUtils.getUserId();
  153. List<SysMenu> menus = menuService.selectMenuTreeById(userId);
  154. return AjaxResult.success(menuService.buildMenus(menus));
  155. }
  156. }