menuTree.js
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export function buildMenuTree(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const tree = [];
// 首先将所有菜单项放入映射中
menuList.forEach((menu) => {
menuMap.set(menu.menuId, { ...menu, children: [] });
});
// 构建树形结构
menuList.forEach((menu) => {
const menuItem = menuMap.get(menu.menuId);
if (menu.parentMenuId === 0) {
// 如果是顶级菜单,直接添加到树中
tree.push(menuItem);
} else {
// 如果不是顶级菜单,找到其父菜单并添加到父菜单的children中
const parent = menuMap.get(menu.parentMenuId);
if (parent) {
parent.children.push(menuItem);
}
}
});
return tree;
}
export function getBindingLeafMenuIds(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const bindingMenuIds = new Set();
const parentMenuIds = new Set();
// 首先处理所有菜单项
menuList.forEach((menu) => {
menuMap.set(menu.menuId, menu);
// 记录所有父级menuId
if (menu.parentMenuId !== 0) {
parentMenuIds.add(menu.parentMenuId);
}
// 记录所有isBinding为true的menuId
if (menu.isBinding) {
bindingMenuIds.add(menu.menuId);
}
});
// 从bindingMenuIds中移除所有父级menuId
parentMenuIds.forEach((parentId) => {
bindingMenuIds.delete(parentId);
});
return Array.from(bindingMenuIds);
}