springboot3。0。4RBAC(2)对接菜单页
新建控制器返回菜单列表,
这里会用到树形结构节点类和构建工具类public class TreeNode implements Serializable { private static final long serialVersionUID = 1L; /** * 主键 */ private Long id; /** * 上级ID */ private Long pid; /** * 子节点列表 */ private List children = new ArrayList<>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } } public class TreeUtils { /** * 根据pid,构建树节点 */ public static List build(List treeNodes, Long pid) { //pid不能为空 AssertUtils.isNull(pid, "pid"); List treeList = new ArrayList<>(); for(T treeNode : treeNodes) { if (pid.equals(treeNode.getPid())) { treeList.add(findChildren(treeNodes, treeNode)); } } return treeList; } /** * 查找子节点 */ private static T findChildren(List treeNodes, T rootNode) { for(T treeNode : treeNodes) { if(rootNode.getId().equals(treeNode.getPid())) { rootNode.getChildren().add(findChildren(treeNodes, treeNode)); } } return rootNode; } /** * 构建树节点 */ public static List build(List treeNodes) { List result = new ArrayList<>(); //list转map Map nodeMap = new LinkedHashMap<>(treeNodes.size()); for(T treeNode : treeNodes){ nodeMap.put(treeNode.getId(), treeNode); } for(T node : nodeMap.values()) { T parent = nodeMap.get(node.getPid()); if(parent != null && !(node.getId().equals(parent.getId()))){ parent.getChildren().add(node); continue; } result.add(node); } return result; } }
编写vue代码,该代码已经提交到git
菜单页目前可以展开
其中前端树形结构用的是el-table里面的展开功能