From 0ad4d69b8fc181cc829ce0e892716889316b067b Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Fri, 19 Apr 2024 16:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bweb/controller/SecMenuController.java | 81 +++---------------- .../com/hfkj/dao/SecRoleMenuRelMapperExt.java | 6 +- .../hfkj/service/SecRoleMenuRelService.java | 6 ++ .../hfkj/service/impl/SecMenuServiceImpl.java | 8 +- .../impl/SecRoleMenuRelServiceImpl.java | 7 ++ 5 files changed, 30 insertions(+), 78 deletions(-) diff --git a/bweb/src/main/java/com/bweb/controller/SecMenuController.java b/bweb/src/main/java/com/bweb/controller/SecMenuController.java index 4d3e407..e2a2ef2 100644 --- a/bweb/src/main/java/com/bweb/controller/SecMenuController.java +++ b/bweb/src/main/java/com/bweb/controller/SecMenuController.java @@ -170,44 +170,17 @@ public class SecMenuController { } } - @RequestMapping(value="/queryAssignMenuTree",method = RequestMethod.GET) + @RequestMapping(value="/queryRoleMenuArray",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "查询分配菜单树") - public ResponseData queryAssignMenuTree(@RequestParam(value = "roleId" , required = true) Long roleId) { + public ResponseData queryRoleMenuArray(@RequestParam(value = "roleId" , required = true) Long roleId) { try { - List treeModelList = new ArrayList<>(); - MenuTreeModel menuTree; + // 查询角色菜单权限 + List roleMenuList = secRoleMenuRelService.getRelListByRole(roleId); - // 获取系统菜单 - List sysMenuList = secMenuService.getAllList(); - Map roleMenuRelMap = secRoleMenuRelService.getRelListByRole(roleId) - .stream() - .collect(Collectors.toMap(SecRoleMenuRel::getMenuId, Function.identity())); - - // 获取系统顶级菜单 - List topLevelMenuList = sysMenuList.stream().filter(o -> o.getMenuPSid() == null) - .sorted(Comparator.comparing(SecMenu::getMenuSort)) - .collect(Collectors.toList()); - - for (SecMenu topLevelMenu : topLevelMenuList) { - menuTree = new MenuTreeModel(); - menuTree.setId(topLevelMenu.getId()); - menuTree.setMenuName(topLevelMenu.getMenuName()); - menuTree.setMenuType(topLevelMenu.getMenuType()); - menuTree.setMenuUrl(topLevelMenu.getMenuUrl()); - menuTree.setMenuUrlImg(topLevelMenu.getMenuUrlImg()); - menuTree.setMenuPSid(topLevelMenu.getMenuPSid()); - menuTree.setMenuSort(topLevelMenu.getMenuSort()); - menuTree.setMenuDesc(topLevelMenu.getMenuDesc()); - // 递归子菜单 - menuTree.setChildMenuList(recursionMenu(sysMenuList, roleMenuRelMap, topLevelMenu.getId())); - // 获取角色菜单 - menuTree.setAuthority(roleMenuRelMap.get(topLevelMenu.getId()) != null ? true : false); - treeModelList.add(menuTree); - } - - return ResponseMsgUtil.success(treeModelList); + return ResponseMsgUtil.success(roleMenuList.stream().map(SecRoleMenuRel::getMenuId).collect(Collectors.toList()) + .stream().map(Object::toString).collect(Collectors.toList())); } catch (Exception e) { log.error("error!",e); @@ -229,10 +202,10 @@ public class SecMenuController { } } - @RequestMapping(value="/queryRoleMenuTreeCustomized",method = RequestMethod.POST) + @RequestMapping(value="/queryMenuTree",method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "查询角色菜单树") - public ResponseData queryRoleMenuTreeCustomized(@RequestParam(value = "roleId" , required = false) Long roleId) { + public ResponseData queryMenuTree() { try { List> mapList = new ArrayList<>(); Map map; @@ -252,7 +225,7 @@ public class SecMenuController { map.put("key", ""+topLevelMenu.getId()); map.put("title", topLevelMenu.getMenuName()); // 获取下级菜单 - map.put("children", recursionMenu2(menuList, topLevelMenu.getId())); + map.put("children", recursionMenu(menuList, topLevelMenu.getId())); mapList.add(map); } } @@ -271,7 +244,7 @@ public class SecMenuController { * @param parentMenuId 父级菜单id * @return */ - public List> recursionMenu2(List dataSource, Long parentMenuId) { + public List> recursionMenu(List dataSource, Long parentMenuId) { List> mapList = new ArrayList<>(); Map map; @@ -285,7 +258,7 @@ public class SecMenuController { map.put("key", ""+menu.getId()); map.put("title", menu.getMenuName()); // 获取下级菜单 - List> recursioned = recursionMenu2(dataSource, menu.getId()); + List> recursioned = recursionMenu(dataSource, menu.getId()); if (recursioned.isEmpty()) { map.put("isLeaf", true); } else { @@ -297,36 +270,4 @@ public class SecMenuController { return mapList; } - /** - * 递归菜单 - * @param sysMenu 系统菜单 - * @param roleMenuRel 角色菜单 - * @param parentMenuId 父类菜单id - * @return - */ - public List recursionMenu(List sysMenu, Map roleMenuRel, Long parentMenuId) { - List treeModelList = new ArrayList<>(); - MenuTreeModel menuTree; - - List collect = sysMenu.stream() - .filter(o -> o.getMenuPSid() != null && o.getMenuPSid().equals(parentMenuId)) - .sorted(Comparator.comparing(SecMenu::getMenuSort)) - .collect(Collectors.toList()); - for (SecMenu menu : collect) { - menuTree = new MenuTreeModel(); - menuTree.setId(menu.getId()); - menuTree.setMenuName(menu.getMenuName()); - menuTree.setMenuType(menu.getMenuType()); - menuTree.setMenuUrl(menu.getMenuUrl()); - menuTree.setMenuUrlImg(menu.getMenuUrlImg()); - menuTree.setMenuPSid(menu.getMenuPSid()); - menuTree.setMenuSort(menu.getMenuSort()); - menuTree.setMenuDesc(menu.getMenuDesc()); - // 获取角色菜单 - menuTree.setAuthority(roleMenuRel.get(menuTree.getId()) != null ? true : false); - menuTree.setChildMenuList(recursionMenu(sysMenu, roleMenuRel, menu.getId())); - treeModelList.add(menuTree); - } - return treeModelList; - } } diff --git a/service/src/main/java/com/hfkj/dao/SecRoleMenuRelMapperExt.java b/service/src/main/java/com/hfkj/dao/SecRoleMenuRelMapperExt.java index 79229af..ce240f7 100644 --- a/service/src/main/java/com/hfkj/dao/SecRoleMenuRelMapperExt.java +++ b/service/src/main/java/com/hfkj/dao/SecRoleMenuRelMapperExt.java @@ -13,9 +13,9 @@ public interface SecRoleMenuRelMapperExt { @Insert({""}) void batchAdd(@Param("list") List list); diff --git a/service/src/main/java/com/hfkj/service/SecRoleMenuRelService.java b/service/src/main/java/com/hfkj/service/SecRoleMenuRelService.java index 9ac6259..c754792 100644 --- a/service/src/main/java/com/hfkj/service/SecRoleMenuRelService.java +++ b/service/src/main/java/com/hfkj/service/SecRoleMenuRelService.java @@ -23,6 +23,12 @@ public interface SecRoleMenuRelService { */ void delete(Long relId); + /** + * 删除 + * @param roleId 角色id + */ + void deleteByRole(Long roleId); + /** * 查询角色菜单关系 * @param roleId 角色id diff --git a/service/src/main/java/com/hfkj/service/impl/SecMenuServiceImpl.java b/service/src/main/java/com/hfkj/service/impl/SecMenuServiceImpl.java index 78e8f9a..2fd5a80 100644 --- a/service/src/main/java/com/hfkj/service/impl/SecMenuServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/impl/SecMenuServiceImpl.java @@ -166,8 +166,8 @@ public class SecMenuServiceImpl implements SecMenuService { if (secRole == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的角色"); } - // 查询角色菜单 - List roleMenuList = secRoleMenuRelService.getRelListByRole(roleId); + // 删除角色权限旧数据 + secRoleMenuRelService.deleteByRole(roleId); SecRoleMenuRel rel; List relList = new ArrayList<>(); @@ -177,11 +177,9 @@ public class SecMenuServiceImpl implements SecMenuService { rel.setMenuId(menuId); relList.add(rel); } - // 数据集合并 - relList.addAll(roleMenuList); // 根据菜单id去重 List collectList = relList.stream().filter(StreamUtil.distinctByKey(b -> b.getMenuId())).collect(Collectors.toList()); - // 绑定角色与菜单关系 + // 角色新菜单权限 secRoleMenuRelService.batchAdd(collectList); // 更新缓存 redisUtil.hdel(CACHE_ROLE_MENU, ""+roleId); diff --git a/service/src/main/java/com/hfkj/service/impl/SecRoleMenuRelServiceImpl.java b/service/src/main/java/com/hfkj/service/impl/SecRoleMenuRelServiceImpl.java index 996f427..8eba58f 100644 --- a/service/src/main/java/com/hfkj/service/impl/SecRoleMenuRelServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/impl/SecRoleMenuRelServiceImpl.java @@ -30,6 +30,13 @@ public class SecRoleMenuRelServiceImpl implements SecRoleMenuRelService { secRoleMenuRelMapper.deleteByPrimaryKey(relId); } + @Override + public void deleteByRole(Long roleId) { + SecRoleMenuRelExample example = new SecRoleMenuRelExample(); + example.createCriteria().andRoleIdEqualTo(roleId); + secRoleMenuRelMapper.deleteByExample(example); + } + @Override public List getRelListByRole(Long roleId) { SecRoleMenuRelExample example = new SecRoleMenuRelExample();