SysMenuController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  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.ruoyi.common.annotation.Log;
  15. import com.ruoyi.common.constant.UserConstants;
  16. import com.ruoyi.common.core.controller.BaseController;
  17. import com.ruoyi.common.core.domain.AjaxResult;
  18. import com.ruoyi.common.core.domain.entity.SysMenu;
  19. import com.ruoyi.common.enums.BusinessType;
  20. import com.ruoyi.common.utils.StringUtils;
  21. import com.ruoyi.system.service.ISysMenuService;
  22. /**
  23. * 菜单信息
  24. *
  25. * @author ruoyi
  26. */
  27. @RestController
  28. @RequestMapping("/system/menu")
  29. public class SysMenuController extends BaseController
  30. {
  31. @Autowired
  32. private ISysMenuService menuService;
  33. /**
  34. * 获取菜单列表
  35. */
  36. @PreAuthorize("@ss.hasPermi('system:menu:list')")
  37. @GetMapping("/list")
  38. public AjaxResult list(SysMenu menu)
  39. {
  40. List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
  41. return success(menus);
  42. }
  43. /**
  44. * 根据菜单编号获取详细信息
  45. */
  46. @PreAuthorize("@ss.hasPermi('system:menu:query')")
  47. @GetMapping(value = "/{menuId}")
  48. public AjaxResult getInfo(@PathVariable Long menuId)
  49. {
  50. return success(menuService.selectMenuById(menuId));
  51. }
  52. /**
  53. * 获取菜单下拉树列表
  54. */
  55. @GetMapping("/treeselect")
  56. public AjaxResult treeselect(SysMenu menu)
  57. {
  58. List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
  59. return success(menuService.buildMenuTreeSelect(menus));
  60. }
  61. /**
  62. * 加载对应角色菜单列表树
  63. */
  64. @GetMapping(value = "/roleMenuTreeselect/{roleId}")
  65. public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
  66. {
  67. List<SysMenu> menus = menuService.selectMenuList(getUserId());
  68. AjaxResult ajax = AjaxResult.success();
  69. ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
  70. ajax.put("menus", menuService.buildMenuTreeSelect(menus));
  71. return ajax;
  72. }
  73. /**
  74. * 新增菜单
  75. */
  76. @PreAuthorize("@ss.hasPermi('system:menu:add')")
  77. @Log(title = "菜单管理", businessType = BusinessType.INSERT)
  78. @PostMapping
  79. public AjaxResult add(@Validated @RequestBody SysMenu menu)
  80. {
  81. if (!menuService.checkMenuNameUnique(menu))
  82. {
  83. return error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  84. }
  85. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
  86. {
  87. return error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  88. }
  89. menu.setCreateBy(getUsername());
  90. return toAjax(menuService.insertMenu(menu));
  91. }
  92. /**
  93. * 修改菜单
  94. */
  95. @PreAuthorize("@ss.hasPermi('system:menu:edit')")
  96. @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
  97. @PutMapping
  98. public AjaxResult edit(@Validated @RequestBody SysMenu menu)
  99. {
  100. if (!menuService.checkMenuNameUnique(menu))
  101. {
  102. return error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
  103. }
  104. else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
  105. {
  106. return error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
  107. }
  108. else if (menu.getMenuId().equals(menu.getParentId()))
  109. {
  110. return error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
  111. }
  112. menu.setUpdateBy(getUsername());
  113. return toAjax(menuService.updateMenu(menu));
  114. }
  115. /**
  116. * 删除菜单
  117. */
  118. @PreAuthorize("@ss.hasPermi('system:menu:remove')")
  119. @Log(title = "菜单管理", businessType = BusinessType.DELETE)
  120. @DeleteMapping("/{menuId}")
  121. public AjaxResult remove(@PathVariable("menuId") Long menuId)
  122. {
  123. if (menuService.hasChildByMenuId(menuId))
  124. {
  125. return warn("存在子菜单,不允许删除");
  126. }
  127. if (menuService.checkMenuExistRole(menuId))
  128. {
  129. return warn("菜单已分配,不允许删除");
  130. }
  131. return toAjax(menuService.deleteMenuById(menuId));
  132. }
  133. }