mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-02-05 16:31:56 +08:00
Compare commits
3 Commits
51ea8f7edd
...
e1503a0f2f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1503a0f2f | ||
|
|
088cec8adf | ||
|
|
8db4f0c8f7 |
@@ -0,0 +1,136 @@
|
|||||||
|
package com.ruoyi.common.core.utils.trees;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 树形菜单生成工具类
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public class Tree <T>{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扁平化的树形结构
|
||||||
|
*/
|
||||||
|
protected List<TreeNode> flatTrees = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所有根节点
|
||||||
|
*/
|
||||||
|
public List<TreeNode> rootTrees = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Id key标识
|
||||||
|
*/
|
||||||
|
protected String idFlag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pid key标识
|
||||||
|
*/
|
||||||
|
protected String pidFlag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* name key标识
|
||||||
|
*/
|
||||||
|
protected String nameFlag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用默认属性名
|
||||||
|
*/
|
||||||
|
public Tree() {
|
||||||
|
this("MenuId", "ParentId", "MenuName");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义修改属性名
|
||||||
|
* @param idFlag
|
||||||
|
* @param pidFlag
|
||||||
|
* @param nameFlag
|
||||||
|
*/
|
||||||
|
public Tree(String idFlag, String pidFlag, String nameFlag) {
|
||||||
|
this.idFlag = idFlag;
|
||||||
|
this.pidFlag = pidFlag;
|
||||||
|
this.nameFlag = nameFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成树形菜单
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<TreeNode> generateTrees(List<T> anyTrees) throws Exception {
|
||||||
|
|
||||||
|
// 处理给定的树形结构
|
||||||
|
screenTrees(anyTrees);
|
||||||
|
|
||||||
|
// 找到父节点下面所有子节点 - 递归
|
||||||
|
generateRecursionTrees();
|
||||||
|
|
||||||
|
return rootTrees;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从任意扁平树结构中生成待处理的扁平化的树形结构 以及 根节点
|
||||||
|
* @param anyTrees
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected void screenTrees(List<T> anyTrees) throws Exception {
|
||||||
|
for (T anyTree: anyTrees) {
|
||||||
|
TreeNode treeNode = new TreeNode();
|
||||||
|
|
||||||
|
try {
|
||||||
|
treeNode.id = (Long) anyTree.getClass().getMethod("get" + idFlag).invoke(anyTree);
|
||||||
|
treeNode.pid = (Long) anyTree.getClass().getMethod("get" + pidFlag).invoke(anyTree);
|
||||||
|
treeNode.label = (String) anyTree.getClass().getMethod("get" + nameFlag).invoke(anyTree);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Exception(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取root节点和子节点
|
||||||
|
if (treeNode.pid == 0L) {
|
||||||
|
this.rootTrees.add(treeNode);
|
||||||
|
} else {
|
||||||
|
this.flatTrees.add(treeNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用递归方式生成树形结构
|
||||||
|
*/
|
||||||
|
protected void generateRecursionTrees() {
|
||||||
|
for (TreeNode rootTreeNode : rootTrees) {
|
||||||
|
recursionFunction(rootTreeNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归函数
|
||||||
|
* @param rootTreeNode
|
||||||
|
*/
|
||||||
|
protected void recursionFunction(TreeNode rootTreeNode) {
|
||||||
|
|
||||||
|
List<TreeNode> usedTreeNode = new ArrayList<>();
|
||||||
|
|
||||||
|
for(TreeNode treeNode : flatTrees) {
|
||||||
|
if (treeNode.pid == rootTreeNode.id) {
|
||||||
|
rootTreeNode.children.add(treeNode);
|
||||||
|
// 标记使用过的node
|
||||||
|
usedTreeNode.add(treeNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除标记的node
|
||||||
|
flatTrees.removeAll(usedTreeNode);
|
||||||
|
|
||||||
|
// 终止条件
|
||||||
|
if (usedTreeNode.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递归
|
||||||
|
for (TreeNode treeNode: usedTreeNode) {
|
||||||
|
recursionFunction(treeNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.ruoyi.common.core.utils.trees;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tree node
|
||||||
|
*/
|
||||||
|
public class TreeNode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID
|
||||||
|
*/
|
||||||
|
public Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parentId
|
||||||
|
*/
|
||||||
|
public Long pid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Node name
|
||||||
|
*/
|
||||||
|
public String label;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Child nodes
|
||||||
|
*/
|
||||||
|
public List<TreeNode> children = new ArrayList<>();
|
||||||
|
}
|
||||||
@@ -130,7 +130,7 @@
|
|||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-col :span="24" v-if="form.menuType != 'F'">
|
<el-col :span="12" v-if="form.menuType != 'F'">
|
||||||
<el-form-item label="菜单图标" prop="icon">
|
<el-form-item label="菜单图标" prop="icon">
|
||||||
<el-popover
|
<el-popover
|
||||||
placement="bottom-start"
|
placement="bottom-start"
|
||||||
@@ -151,6 +151,11 @@
|
|||||||
</el-popover>
|
</el-popover>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
<el-col :span="12">
|
||||||
|
<el-form-item label="显示排序" prop="orderNum">
|
||||||
|
<el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
|
||||||
|
</el-form-item>
|
||||||
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
@@ -158,9 +163,15 @@
|
|||||||
<el-input v-model="form.menuName" placeholder="请输入菜单名称" />
|
<el-input v-model="form.menuName" placeholder="请输入菜单名称" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :span="12">
|
<el-col :span="12" v-if="form.menuType == 'C'">
|
||||||
<el-form-item label="显示排序" prop="orderNum">
|
<el-form-item prop="routeName">
|
||||||
<el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
|
<el-input v-model="form.routeName" placeholder="请输入路由名称" />
|
||||||
|
<span slot="label">
|
||||||
|
<el-tooltip content="默认不填则和路由地址相同:如地址为:`user`,则名称为`User`(注意:为避免名字的冲突,特殊情况下请自定义,保证唯一性)" placement="top">
|
||||||
|
<i class="el-icon-question"></i>
|
||||||
|
</el-tooltip>
|
||||||
|
路由名称
|
||||||
|
</span>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
|
|||||||
Reference in New Issue
Block a user