From 4557c16db7c25beed8d649e128cae14f8f40e14f Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Wed, 17 Apr 2024 16:05:55 +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 | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/bweb/src/main/java/com/bweb/controller/SecMenuController.java b/bweb/src/main/java/com/bweb/controller/SecMenuController.java index 5c56e99..acac466 100644 --- a/bweb/src/main/java/com/bweb/controller/SecMenuController.java +++ b/bweb/src/main/java/com/bweb/controller/SecMenuController.java @@ -202,6 +202,74 @@ public class SecMenuController { } } + @RequestMapping(value="/queryRoleMenuTree2",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询角色菜单树") + public ResponseData queryRoleMenuTree2(@RequestParam(value = "roleId" , required = false) Long roleId) { + try { + + List> mapList = new ArrayList<>(); + Map map; + + List menuList = secMenuService.getAllList(); + + // 获取最顶层菜单 + List topLevelMenuList = menuList.stream() + .filter(o -> o.getMenuPSid() == null) + .sorted(Comparator.comparing(SecMenu::getMenuSort)) + .collect(Collectors.toList()); + + for (SecMenu topLevelMenu : topLevelMenuList) { + if (topLevelMenu.getMenuType().equals(SecMenuTypeEnum.type1.getCode())) { + map = new HashMap<>(); + map.put("key", topLevelMenu.getId()); + map.put("title", topLevelMenu.getMenuName()); + // 获取下级菜单 + map.put("children", recursionMenu2(menuList, topLevelMenu.getId())); + mapList.add(map); + } + } + + return ResponseMsgUtil.success(mapList); + + } catch (Exception e) { + log.error("error!",e); + return ResponseMsgUtil.exception(e); + } + } + + /** + * 递归获取菜单 + * @param dataSource 数据源 + * @param parentMenuId 父级菜单id + * @return + */ + public List> recursionMenu2(List dataSource, Long parentMenuId) { + List> mapList = new ArrayList<>(); + Map map; + + List collect = dataSource.stream() + .filter(o -> o.getMenuPSid() != null && o.getMenuPSid().equals(parentMenuId)) + .sorted(Comparator.comparing(SecMenu::getMenuSort)) + .collect(Collectors.toList()); + for (SecMenu menu : collect) { + if (menu.getMenuType().equals(SecMenuTypeEnum.type1.getCode())) { + map = new HashMap<>(); + map.put("key", menu.getId()); + map.put("title", menu.getMenuName()); + // 获取下级菜单 + List> recursioned = recursionMenu2(dataSource, menu.getId()); + if (recursioned.isEmpty()) { + map.put("isLeaf", true); + } else { + map.put("children", recursioned); + } + mapList.add(map); + } + } + return mapList; + } + /** * 递归菜单 * @param sysMenu 系统菜单