Sfoglia il codice sorgente

增加顶级菜单pid

tags/24080901
WendyYang 1 anno fa
parent
commit
d009ad233e
4 ha cambiato i file con 36 aggiunte e 294 eliminazioni
  1. +29
    -279
      pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java
  2. +3
    -3
      pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java
  3. +2
    -6
      pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java
  4. +2
    -6
      pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java

+ 29
- 279
pmapi/src/main/java/com/ningdatech/pmapi/common/util/TreeUtil.java Vedi File

@@ -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<? extends Serializable> 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 <E extends MenuTreeEntity<E, ? extends Serializable>> List<E> buildTree(List<E> treeList, String title) {
if (CollUtil.isEmpty(treeList)) {
return treeList;
}
//记录自己是自己的父节点的id集合
List<Serializable> selfIdEqSelfParent = new ArrayList<>();

// 遍历两次
foreachNodes(treeList, selfIdEqSelfParent);
foreachNodesWithTopMenu(treeList);

// 找出根节点集合
List<E> trees = new ArrayList<>();

List<? extends Serializable> 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 <E extends MenuTreeEntity<E, ? extends Serializable>>
void foreachNodes(List<E> treeList, List<Serializable> selfIdEqSelfParent) {
nodeMap = Maps.newConcurrentMap();
@@ -149,260 +118,41 @@ public final class TreeUtil {
}
}

private static <E extends MenuTreeEntity<E, ? extends Serializable>>
void checkTitle(List<E> trees, String title) {
if (StringUtils.isBlank(title)) {
return;
}
for (E tree : trees) {
//筛选 从顶级往下查
//递归筛选
checkTitleNode(tree, title, trees, trees);
}
}

private static <E extends MenuTreeEntity<E, ? extends Serializable>> void checkTitleNode(MenuTreeEntity node, String title, List<E> parents, List<E> tops) {
if (!node.getTitle().contains(title)) {
if (CollUtil.isEmpty(node.getChildren())) {
removeNode(parents, node, tops);
} else {
Iterator<E> iterator = node.getChildren().iterator();
while (iterator.hasNext()) {
checkTitleNode(iterator.next(), title, node.getChildren(), tops);
}
}
} else {
//匹配到了
return;
}
}

private static <E extends MenuTreeEntity<E, ? extends Serializable>>
void removeNode(List<E> parents, MenuTreeEntity node, List<E> 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<MenuRoleVO> buildTree(List<MenuRoleVO> treeList, List<RoleMenu> roleMenus) {
if (CollUtil.isEmpty(treeList)) {
return treeList;
}
//记录自己是自己的父节点的id集合
List<Serializable> selfIdEqSelfParent = new ArrayList<>();

// 遍历两次
foreachNodesCheckRoleWithoutParent(treeList, selfIdEqSelfParent, roleMenus);
foreachNodesWithTopMenu(treeList);

// 找出根节点集合
List<MenuRoleVO> trees = new ArrayList<>();

List<? extends Serializable> 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<MenuRoleVO> treeList,
List<Serializable> selfIdEqSelfParent, List<RoleMenu> 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<MenuRoleVO> buildUserTree(List<MenuRoleVO> treeList, List<RoleMenu> roleMenus) {
public static List<MenuRoleVO> buildUserTree(List<MenuRoleVO> treeList) {
if (CollUtil.isEmpty(treeList)) {
return Collections.emptyList();
}
//记录自己是自己的父节点的id集合
List<Serializable> selfIdEqSelfParent = new ArrayList<>();

// 遍历两次
foreachNodesCheckRole(treeList, selfIdEqSelfParent, roleMenus);
foreachNodesWithTopMenu(treeList);

// 找出根节点集合
List<MenuRoleVO> trees = new ArrayList<>();

List<? extends Serializable> 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<Long, List<MenuRoleVO>> menuGroup = CollUtils.group(treeList, MenuTreeEntity::getPid);
Map<Long, MenuRoleVO> menuMap = CollUtils.listToMap(treeList, MenuTreeEntity::getId);
return buildUserMenuTree(menuGroup, menuMap, StrPool.DEF_PARENT_ID);
}

//判断角色权限用 不用父级
private static void foreachNodesCheckRoleWithoutParent(List<MenuRoleVO> treeList, List<Serializable> selfIdEqSelfParent, List<RoleMenu> 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<MenuRoleVO> buildUserMenuTree(Map<Long, List<MenuRoleVO>> menuGroup, Map<Long, MenuRoleVO> menuMap, Long parentId) {
List<MenuRoleVO> currMenus = menuGroup.get(parentId);
if (currMenus == null) {
return Collections.emptyList();
}
}

//判断当前登录权限用
private static void foreachNodesCheckRole(List<MenuRoleVO> treeList, List<Serializable> selfIdEqSelfParent, List<RoleMenu> 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<MenuRoleVO> 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<MenuRoleVO> childMenus = menuGroup.get(curr.getId());
if (childMenus != null) {
Map<MenuTypeEnum, List<MenuRoleVO>> 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<RoleMenu> roleMenus) {
Boolean isUserMenu = false;
for (RoleMenu roleMenu : roleMenus) {
if (roleId.equals(roleMenu.getMenuId())) {
isUserMenu = true;
break;
}
}
return isUserMenu;
}

/**
* 动态判断redirect


+ 3
- 3
pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/MenuController.java Vedi File

@@ -47,9 +47,9 @@ public class MenuController {

@ApiOperation("查询系统所有的菜单")
@GetMapping("/list")
public List<Menu> allTree(@Valid @RequestParam(required = false, value = "title") String title) {
public List<Menu> allTree() {
List<Menu> 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<MenuRoleVO> currentUserMenu() {
List<Menu> list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort));
return menuManage.buildUserMenu(list, LoginUserUtil.loginUserDetail());
return menuManage.buildUserMenu(list);
}

@ApiOperation(value = "查询菜单", notes = "查询菜单")


+ 2
- 6
pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java Vedi File

@@ -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<MenuRoleVO> buildUserMenu(List<Menu> list, UserInfoDetails loginUser) {
/*if (Objects.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoleIdList())) {
return Collections.emptyList();
}*/
public List<MenuRoleVO> buildUserMenu(List<Menu> list) {
List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.lambdaQuery(RoleMenu.class)
.in(RoleMenu::getRoleId, LoginUserUtil.getRoleIdList()));
List<MenuRoleVO> menuRoles = CollUtils.convert(list, w -> BeanUtil.copyProperties(w, MenuRoleVO.class));
return TreeUtil.buildUserTree(menuRoles, roleMenus);
return TreeUtil.buildUserTree(menuRoles);
}

}

+ 2
- 6
pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/MenuRoleVO.java Vedi File

@@ -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<MenuRoleVO, Long> {
private static final long serialVersionUID = 1L;

/**
* 权限"
*/
@ApiModelProperty(value = "权限")
private String permission;
/**
* 类型;[0-菜单 1-目录 2-按钮]
*/
@ApiModelProperty(value = "类型")
private Integer type;
private MenuTypeEnum menuType;

/**
* 组件


Loading…
Annulla
Salva