diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java index ae70159..b693c87 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java @@ -3,14 +3,17 @@ package com.ningdatech.pmapi.common.util; import cn.hutool.core.collection.CollUtil; import com.google.common.collect.Maps; +import com.ningdatech.basic.util.CollUtils; import com.ningdatech.basic.util.StrPool; import com.ningdatech.pmapi.common.model.entity.MenuTreeEntity; -import com.ningdatech.pmapi.sys.model.entity.RoleMenu; +import com.ningdatech.pmapi.sys.model.enumeration.MenuTypeEnum; import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO; -import org.apache.commons.lang3.StringUtils; import java.io.Serializable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -62,7 +65,6 @@ public final class TreeUtil { List allIds = treeList.stream().map(node -> node.getId()).collect(Collectors.toList()); for (E baseNode : treeList) { if (!allIds.contains(baseNode.getPid()) || selfIdEqSelfParent.contains(baseNode.getPid())) { - baseNode.setPid(null); baseNode.setTopMenu(baseNode.getName()); trees.add(baseNode); } @@ -70,39 +72,6 @@ public final class TreeUtil { return trees; } - /** - * 构建Tree结构 - * - * @param treeList 待转换的集合 - * @param title 菜单名 - * @return 树结构 - */ - public static > List buildTree(List treeList, String title) { - if (CollUtil.isEmpty(treeList)) { - return treeList; - } - //记录自己是自己的父节点的id集合 - List selfIdEqSelfParent = new ArrayList<>(); - - // 遍历两次 - foreachNodes(treeList, selfIdEqSelfParent); - foreachNodesWithTopMenu(treeList); - - // 找出根节点集合 - List trees = new ArrayList<>(); - - List allIds = treeList.stream().map(node -> node.getId()).collect(Collectors.toList()); - for (E baseNode : treeList) { - if (!allIds.contains(baseNode.getPid()) || selfIdEqSelfParent.contains(baseNode.getPid())) { - baseNode.setPid(null); - baseNode.setTopMenu(baseNode.getName()); - trees.add(baseNode); - } - } -// checkTitle(trees,title); - return trees; - } - private static > void foreachNodes(List treeList, List selfIdEqSelfParent) { nodeMap = Maps.newConcurrentMap(); @@ -149,260 +118,41 @@ public final class TreeUtil { } } - private static > - void checkTitle(List trees, String title) { - if (StringUtils.isBlank(title)) { - return; - } - for (E tree : trees) { - //筛选 从顶级往下查 - //递归筛选 - checkTitleNode(tree, title, trees, trees); - } - } - - private static > void checkTitleNode(MenuTreeEntity node, String title, List parents, List tops) { - if (!node.getTitle().contains(title)) { - if (CollUtil.isEmpty(node.getChildren())) { - removeNode(parents, node, tops); - } else { - Iterator iterator = node.getChildren().iterator(); - while (iterator.hasNext()) { - checkTitleNode(iterator.next(), title, node.getChildren(), tops); - } - } - } else { - //匹配到了 - return; - } - } - - private static > - void removeNode(List parents, MenuTreeEntity node, List tops) { - parents.remove(node); - //如果该层节点都被删了 要删除父级 - if (Objects.nonNull(node.getPid())) { - MenuTreeEntity parent = nodeMap.get(node.getPid()); - if (Objects.nonNull(parent)) { - if (Objects.nonNull(parent.getPid())) { - MenuTreeEntity parentN = nodeMap.get(parent.getPid()); - removeNode(parentN.getChildren(), parent, tops); - } else { - tops.remove(parent); - } - } - } else { - tops.remove(node); - } - } - - public static List buildTree(List treeList, List roleMenus) { - if (CollUtil.isEmpty(treeList)) { - return treeList; - } - //记录自己是自己的父节点的id集合 - List selfIdEqSelfParent = new ArrayList<>(); - - // 遍历两次 - foreachNodesCheckRoleWithoutParent(treeList, selfIdEqSelfParent, roleMenus); - foreachNodesWithTopMenu(treeList); - - // 找出根节点集合 - List trees = new ArrayList<>(); - - List allIds = treeList.stream().map(MenuRoleVO::getId).collect(Collectors.toList()); - for (MenuRoleVO baseNode : treeList) { - if (!allIds.contains(baseNode.getPid()) || selfIdEqSelfParent.contains(baseNode.getPid())) { - baseNode.setPid(null); - for (RoleMenu roleMenu : roleMenus) { - if (baseNode.getId().equals(roleMenu.getMenuId()) && - 2 != baseNode.getHasPermission()) { - baseNode.setTopMenu(baseNode.getName()); - trees.add(baseNode); - } - } - } - } - return trees; - } - - private static void foreachNodesWithRole(List treeList, - List selfIdEqSelfParent, List roleMenus) { - nodeMap = Maps.newConcurrentMap(); - for (MenuRoleVO parent : treeList) { - Serializable id = parent.getId(); - nodeMap.put(parent.getId(), parent); - for (MenuRoleVO children : treeList) { - if (parent != children) { - //parent != children 这个来判断自己的孩子不允许是自己,因为有时候,根节点的parent会被设置成为自己 - if (id.equals(children.getPid())) { - parent.initChildren(); - if (0L == parent.getPid()) { - children.setTopMenu(parent.getPath()); - children.setLevel(2); - } else { - children.setTopMenu(parent.getTopMenu()); - children.setLevel(parent.getLevel() + 1); - } - parent.getChildren().add(children); - } - } else if (id.equals(parent.getPid())) { - selfIdEqSelfParent.add(id); - } - - for (RoleMenu roleMenu : roleMenus) { - if (children.getId().equals(roleMenu.getMenuId())) { - children.setHasPermission(1); - } - } - } - } - } - - public static List buildUserTree(List treeList, List roleMenus) { + public static List buildUserTree(List treeList) { if (CollUtil.isEmpty(treeList)) { return Collections.emptyList(); } - //记录自己是自己的父节点的id集合 - List selfIdEqSelfParent = new ArrayList<>(); - - // 遍历两次 - foreachNodesCheckRole(treeList, selfIdEqSelfParent, roleMenus); - foreachNodesWithTopMenu(treeList); - - // 找出根节点集合 - List trees = new ArrayList<>(); - - List allIds = treeList.stream().map(node -> node.getId()).collect(Collectors.toList()); - for (MenuRoleVO baseNode : treeList) { - if (!allIds.contains(baseNode.getPid()) || selfIdEqSelfParent.contains(baseNode.getPid())) { - baseNode.setPid(null); - Boolean isUserMenu = checkPermisson(baseNode.getId(), roleMenus); - if (isUserMenu) { - baseNode.setTopMenu(baseNode.getName()); - checkRedirect(baseNode); - trees.add(baseNode); - } else { - if (baseNode.getSize() > 0) { - baseNode.setTopMenu(baseNode.getName()); - checkRedirect(baseNode); - trees.add(baseNode); - } - } - } - } - return trees; + Map> menuGroup = CollUtils.group(treeList, MenuTreeEntity::getPid); + Map menuMap = CollUtils.listToMap(treeList, MenuTreeEntity::getId); + return buildUserMenuTree(menuGroup, menuMap, StrPool.DEF_PARENT_ID); } - //判断角色权限用 不用父级 - private static void foreachNodesCheckRoleWithoutParent(List treeList, List selfIdEqSelfParent, List roleMenus) { - nodeMap = Maps.newConcurrentMap(); - for (MenuRoleVO parent : treeList) { - Serializable id = parent.getId(); - nodeMap.put(parent.getId(), parent); - for (MenuRoleVO children : treeList) { - if (parent != children) { - //parent != children 这个来判断自己的孩子不允许是自己,因为有时候,根节点的parent会被设置成为自己 - if (id.equals(children.getPid())) { - parent.initChildren(); - - if (!checkPermisson(children.getId(), roleMenus)) { - continue; - } - - if (0L == parent.getPid()) { - children.setTopMenu(parent.getName()); - children.setLevel(2); - } else { - children.setTopMenu(parent.getTopMenu()); - children.setLevel(parent.getLevel() + 1); - } - parent.getChildren().add(children); - } - } else if (id.equals(parent.getPid())) { - selfIdEqSelfParent.add(id); - } - - for (RoleMenu roleMenu : roleMenus) { - if (children.getId().equals(roleMenu.getMenuId())) { - children.setHasPermission(1); - } - } - } + private static List buildUserMenuTree(Map> menuGroup, Map menuMap, Long parentId) { + List currMenus = menuGroup.get(parentId); + if (currMenus == null) { + return Collections.emptyList(); } - } - - //判断当前登录权限用 - private static void foreachNodesCheckRole(List treeList, List selfIdEqSelfParent, List roleMenus) { - nodeMap = Maps.newConcurrentMap(); - for (MenuRoleVO parent : treeList) { - Serializable id = parent.getId(); - nodeMap.put(parent.getId(), parent); - for (MenuRoleVO children : treeList) { - if (parent != children) { - //parent != children 这个来判断自己的孩子不允许是自己,因为有时候,根节点的parent会被设置成为自己 - if (id.equals(children.getPid())) { - parent.initChildren(); - - if (!checkPermisson(children.getId(), roleMenus)) { - continue; - } - - if (0L == parent.getPid()) { - children.setTopMenu(parent.getName()); - children.setLevel(2); - } else { - children.setTopMenu(parent.getTopMenu()); - children.setLevel(parent.getLevel() + 1); - } - parent.getChildren().add(children); - } - } else if (id.equals(parent.getPid())) { - selfIdEqSelfParent.add(id); - } + ArrayList menus = new ArrayList<>(currMenus); + for (MenuRoleVO curr : currMenus) { + if (curr.getPid().equals(StrPool.DEF_PARENT_ID)) { + curr.setTopMenu(curr.getName()); + } else { + curr.setTopMenu(menuMap.get(curr.getPid()).getTopMenu()); } - - //1层 2层 再加一下 - for (MenuRoleVO children : treeList) { - if (parent != children) { - if (id.equals(children.getPid())) { - - if (parent.getChildren().contains(children) || - children.getSize() == 0) { - continue; - } - - if (0L == parent.getPid()) { - children.setTopMenu(parent.getName()); - children.setLevel(2); - } else { - children.setTopMenu(parent.getTopMenu()); - children.setLevel(parent.getLevel() + 1); - } - parent.getChildren().add(children); - } + List childMenus = menuGroup.get(curr.getId()); + if (childMenus != null) { + Map> groupByType = CollUtils.group(childMenus, MenuRoleVO::getMenuType); + menus.addAll(groupByType.getOrDefault(MenuTypeEnum.BUTTON, Collections.emptyList())); + curr.setChildren(groupByType.get(MenuTypeEnum.MENU)); + for (MenuRoleVO child : curr.getChildren()) { + child.setTopMenu(curr.getTopMenu()); + child.setChildren(buildUserMenuTree(menuGroup, menuMap, child.getId())); } } } + return menus; } - /** - * 判断角色是否有该菜单权限 - * - * @param roleId - * @param roleMenus - * @return - */ - private static Boolean checkPermisson(Long roleId, List roleMenus) { - Boolean isUserMenu = false; - for (RoleMenu roleMenu : roleMenus) { - if (roleId.equals(roleMenu.getMenuId())) { - isUserMenu = true; - break; - } - } - return isUserMenu; - } /** * 动态判断redirect diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java b/pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java index 7c89151..d141dc4 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java @@ -47,9 +47,9 @@ public class MenuController { @ApiOperation("查询系统所有的菜单") @GetMapping("/list") - public List allTree(@Valid @RequestParam(required = false, value = "title") String title) { + public List allTree() { List list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort)); - return TreeUtil.buildTree(list, title); + return TreeUtil.buildTree(list); } @ApiOperation("查询系统所有数据权限的菜单") @@ -70,7 +70,7 @@ public class MenuController { @GetMapping("/myMenu") public List currentUserMenu() { List list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort)); - return menuManage.buildUserMenu(list, LoginUserUtil.loginUserDetail()); + return menuManage.buildUserMenu(list); } @ApiOperation(value = "查询菜单", notes = "查询菜单") diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java index 5e029a5..e6ba96c 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java @@ -8,7 +8,6 @@ import com.ningdatech.pmapi.sys.model.entity.Menu; import com.ningdatech.pmapi.sys.model.entity.RoleMenu; import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO; import com.ningdatech.pmapi.sys.service.IRoleMenuService; -import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; import com.ningdatech.pmapi.user.util.LoginUserUtil; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -29,14 +28,11 @@ public class MenuManage { private final IRoleMenuService roleMenuService; - public List buildUserMenu(List list, UserInfoDetails loginUser) { - /*if (Objects.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoleIdList())) { - return Collections.emptyList(); - }*/ + public List buildUserMenu(List list) { List roleMenus = roleMenuService.list(Wrappers.lambdaQuery(RoleMenu.class) .in(RoleMenu::getRoleId, LoginUserUtil.getRoleIdList())); List menuRoles = CollUtils.convert(list, w -> BeanUtil.copyProperties(w, MenuRoleVO.class)); - return TreeUtil.buildUserTree(menuRoles, roleMenus); + return TreeUtil.buildUserTree(menuRoles); } } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java b/pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java index 886b997..7201c9c 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java @@ -2,6 +2,7 @@ package com.ningdatech.pmapi.sys.model.vo; import com.baomidou.mybatisplus.annotation.TableField; import com.ningdatech.pmapi.common.model.entity.MenuTreeEntity; +import com.ningdatech.pmapi.sys.model.enumeration.MenuTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; @@ -28,15 +29,10 @@ public class MenuRoleVO extends MenuTreeEntity { private static final long serialVersionUID = 1L; /** - * 权限" - */ - @ApiModelProperty(value = "权限") - private String permission; - /** * 类型;[0-菜单 1-目录 2-按钮] */ @ApiModelProperty(value = "类型") - private Integer type; + private MenuTypeEnum menuType; /** * 组件