瀏覽代碼

根据用户权限加载菜单数据树形结构

RuoYi 5 年之前
父節點
當前提交
140be61003

+ 13 - 3
ruoyi/src/main/java/com/ruoyi/project/system/controller/SysMenuController.java

@@ -14,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.framework.aspectj.lang.annotation.Log;
 import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
+import com.ruoyi.framework.security.LoginUser;
+import com.ruoyi.framework.security.service.TokenService;
 import com.ruoyi.framework.web.controller.BaseController;
 import com.ruoyi.framework.web.domain.AjaxResult;
 import com.ruoyi.project.system.domain.SysMenu;
@@ -33,6 +36,9 @@ public class SysMenuController extends BaseController
     @Autowired
     private ISysMenuService menuService;
 
+    @Autowired
+    private TokenService tokenService;
+
     /**
      * 获取菜单列表
      */
@@ -40,7 +46,9 @@ public class SysMenuController extends BaseController
     @GetMapping("/list")
     public AjaxResult list(SysMenu menu)
     {
-        List<SysMenu> menus = menuService.selectMenuList(menu);
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        Long userId = loginUser.getUser().getUserId();
+        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
         return AjaxResult.success(menuService.buildMenuTree(menus));
     }
 
@@ -58,9 +66,11 @@ public class SysMenuController extends BaseController
      * 获取菜单下拉树列表
      */
     @GetMapping("/treeselect")
-    public AjaxResult treeselect(SysMenu dept)
+    public AjaxResult treeselect(SysMenu menu)
     {
-        List<SysMenu> menus = menuService.selectMenuList(dept);
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        Long userId = loginUser.getUser().getUserId();
+        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
         return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
     }
 

+ 8 - 0
ruoyi/src/main/java/com/ruoyi/project/system/mapper/SysMenuMapper.java

@@ -27,6 +27,14 @@ public interface SysMenuMapper
     public List<String> selectMenuPerms();
 
     /**
+     * 根据用户查询系统菜单列表
+     * 
+     * @param menu 菜单信息
+     * @return 菜单列表
+     */
+    public List<SysMenu> selectMenuListByUserId(SysMenu menu);
+
+    /**
      * 根据用户ID查询权限
      * 
      * @param userId 用户ID

+ 3 - 2
ruoyi/src/main/java/com/ruoyi/project/system/service/ISysMenuService.java

@@ -14,12 +14,13 @@ import com.ruoyi.project.system.domain.vo.RouterVo;
 public interface ISysMenuService
 {
     /**
-     * 查询系统菜单列表
+     * 根据用户查询系统菜单列表
      * 
      * @param menu 菜单信息
+     * @param userId 用户ID
      * @return 菜单列表
      */
-    public List<SysMenu> selectMenuList(SysMenu menu);
+    public List<SysMenu> selectMenuList(SysMenu menu, Long userId);
 
     /**
      * 根据用户ID查询权限

+ 13 - 2
ruoyi/src/main/java/com/ruoyi/project/system/service/impl/SysMenuServiceImpl.java

@@ -15,6 +15,7 @@ import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.web.domain.TreeSelect;
 import com.ruoyi.project.system.domain.SysMenu;
+import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.domain.vo.MetaVo;
 import com.ruoyi.project.system.domain.vo.RouterVo;
 import com.ruoyi.project.system.mapper.SysMenuMapper;
@@ -44,9 +45,19 @@ public class SysMenuServiceImpl implements ISysMenuService
      * @return 菜单列表
      */
     @Override
-    public List<SysMenu> selectMenuList(SysMenu menu)
+    public List<SysMenu> selectMenuList(SysMenu menu, Long userId)
     {
-        List<SysMenu> menuList = menuMapper.selectMenuList(menu);
+        List<SysMenu> menuList = null;
+        // 管理员显示所有菜单信息
+        if (SysUser.isAdmin(userId))
+        {
+            menuList = menuMapper.selectMenuList(menu);
+        }
+        else
+        {
+            menu.getParams().put("userId", userId);
+            menuList = menuMapper.selectMenuListByUserId(menu);
+        }
         return menuList;
     }
 

+ 16 - 0
ruoyi/src/main/resources/mybatis/system/SysMenuMapper.xml

@@ -47,6 +47,22 @@
 		from sys_menu m where m.menu_type in ('M', 'C') and m.visible = 0
 		order by m.parent_id, m.order_num
 	</select>
+	
+	<select id="selectMenuListByUserId" parameterType="SysMenu" resultMap="SysMenuResult">
+		select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, ifnull(m.perms,'') as perms, m.is_frame, m.menu_type, m.icon, m.order_num, m.create_time
+		from sys_menu m
+		left join sys_role_menu rm on m.menu_id = rm.menu_id
+		left join sys_user_role ur on rm.role_id = ur.role_id
+		left join sys_role ro on ur.role_id = ro.role_id
+		where ur.user_id = #{params.userId}
+		<if test="menuName != null and menuName != ''">
+            AND menu_name like concat('%', #{menuName}, '%')
+		</if>
+		<if test="visible != null and visible != ''">
+            AND visible = #{visible}
+		</if>
+		order by m.parent_id, m.order_num
+	</select>
     
     <select id="selectMenuTreeByUserId" parameterType="Long" resultMap="SysMenuResult">
 		select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, ifnull(m.perms,'') as perms, m.is_frame, m.menu_type, m.icon, m.order_num, m.create_time