@lambo-design/shared 1.0.0-beta.355 → 1.0.0-beta.357

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/shared",
3
- "version": "1.0.0-beta.355",
3
+ "version": "1.0.0-beta.357",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -0,0 +1,87 @@
1
+ /**
2
+ * 获取所有子菜单 - 优化版
3
+ * 使用 Map 缓存 + 迭代,复杂度从 O(n²×depth) 降到 O(n×depth)
4
+ * @param {Array} permissionList 权限列表
5
+ * @param {string|number} pid 父级ID
6
+ * @param {Object} appInfo 应用信息
7
+ * @returns {Array} 所有子节点
8
+ */
9
+ export const getChildPermissions = (permissionList, pid, appInfo) => {
10
+ if (!permissionList || !pid) return [];
11
+
12
+ // 建立 id -> permission 的 Map,O(n)
13
+ const permissionMap = new Map();
14
+ for (const permission of permissionList) {
15
+ if (permission && permission.hasOwnProperty('permissionId')) {
16
+ permissionMap.set(permission.permissionId, permission);
17
+ }
18
+ }
19
+
20
+ // 使用迭代检查是否为子节点,避免递归栈溢出
21
+ const isChildOf = (permission) => {
22
+ let current = permission;
23
+ const visited = new Set(); // 防止循环引用
24
+
25
+ while (current && current.pid) {
26
+ // 循环引用检测
27
+ if (visited.has(current.permissionId)) return false;
28
+ visited.add(current.permissionId);
29
+
30
+ if (current.pid == pid) return true;
31
+ current = permissionMap.get(current.pid); // O(1) 查找
32
+ }
33
+ return false;
34
+ };
35
+
36
+ const res = [];
37
+ for (const permission of permissionList) {
38
+ if (isChildOf(permission)) {
39
+ const per2 = deepCopy(permission);
40
+ per2.appId = pid;
41
+ if (per2.pid == pid) {
42
+ per2.pid = "0";
43
+ }
44
+ per2.appInfo = deepCopy(appInfo);
45
+ res.push(per2);
46
+ }
47
+ }
48
+
49
+ return res;
50
+ }
51
+
52
+
53
+ export const getRootPermission = (permissionList, permission) => {
54
+ if (!permissionList || !permission) return permission;
55
+
56
+ // 构建 Map,O(n)
57
+ const permissionMap = new Map();
58
+ for (const item of permissionList) {
59
+ if (item && item.permissionId != null) {
60
+ const key = `${item.permissionId}|${item.appId || ''}`;
61
+ permissionMap.set(key, item);
62
+ }
63
+ }
64
+
65
+ // 迭代向上查找根节点
66
+ let current = permission;
67
+ const visited = new Set();
68
+
69
+ while (current) {
70
+ if (!current.pid || current.pid === '0') {
71
+ return current;
72
+ }
73
+
74
+ // 循环引用检测
75
+ const key = `${current.permissionId}|${current.appId || ''}`;
76
+ if (visited.has(key)) return permission;
77
+ visited.add(key);
78
+
79
+ // O(1) 查找父节点
80
+ const parentKey = `${current.pid}|${current.appId || ''}`;
81
+ current = permissionMap.get(parentKey);
82
+
83
+ if (!current) return permission;
84
+ }
85
+
86
+ return permission;
87
+ }
@@ -130,7 +130,7 @@ function buildMenuTree(childrenMap, parentId, crumbs, root, pageChildren) {
130
130
  return menuTree
131
131
  }
132
132
 
133
- function generatorMenuRouter(permissionList, appIds, generateType = 'router') {
133
+ export function generatorMenuRouter(permissionList, appIds, generateType = 'router') {
134
134
  const pageNode = {
135
135
  path: '/page',
136
136
  name: 'page',
@@ -1,7 +1,7 @@
1
1
  import config from "../../config/config"
2
2
  import {deepCopy} from "../assist";
3
3
  import {getLocalStorage, getUrlParams} from "../platform"
4
- import generatorDynamicRouterNew from "./generatorDynamicRouter";
4
+ import {generatorMenuRouter as generatorMenuRouterNew} from "./generatorDynamicRouter";
5
5
  /**
6
6
  * 判断是否存在路由
7
7
  * @param $router
@@ -46,7 +46,7 @@ export const hasRoutePath = ($router, path) => {
46
46
  * @returns []
47
47
  */
48
48
  export const generatorMenuList = (permissionList,appId,generateType = 'router') => {
49
- let menuRouter = generatorDynamicRouterNew(permissionList,appId,generateType);
49
+ let menuRouter = generatorMenuRouterNew(permissionList,appId,generateType);
50
50
  return menuRouter && menuRouter.menuData ? menuRouter.menuData : [];
51
51
  };
52
52
 
package/utils/platform.js CHANGED
@@ -967,25 +967,53 @@ export const isChildPermission = (permissionList, pid, per) => {
967
967
  return false;
968
968
  }
969
969
  /**
970
- * 获取所有子菜单
971
- * @param permissionList
972
- * @param pid
970
+ * 获取所有子菜单 - 优化版
971
+ * 使用 Map 缓存 + 迭代,复杂度从 O(n²×depth) 降到 O(n×depth)
972
+ * @param {Array} permissionList 权限列表
973
+ * @param {string|number} pid 父级ID
974
+ * @param {Object} appInfo 应用信息
975
+ * @returns {Array} 所有子节点
973
976
  */
974
977
  export const getChildPermissions = (permissionList, pid, appInfo) => {
975
- let res = []
976
- if (permissionList && pid) {
977
- for (let permission of permissionList) {
978
- if (isChildPermission(permissionList, pid, permission)) {
979
- let per2 = deepCopy(permission);
980
- per2.appId = pid;
981
- if (per2.pid == pid) {
982
- per2.pid = "0";
983
- }
984
- per2.appInfo = deepCopy(appInfo)
985
- res.push(per2);
978
+ if (!permissionList || !pid) return [];
979
+
980
+ // 建立 id -> permission Map,O(n)
981
+ const permissionMap = new Map();
982
+ for (const permission of permissionList) {
983
+ if (permission && permission.hasOwnProperty('permissionId')) {
984
+ permissionMap.set(permission.permissionId, permission);
985
+ }
986
+ }
987
+
988
+ // 使用迭代检查是否为子节点,避免递归栈溢出
989
+ const isChildOf = (permission) => {
990
+ let current = permission;
991
+ const visited = new Set(); // 防止循环引用
992
+
993
+ while (current && current.pid) {
994
+ // 循环引用检测
995
+ if (visited.has(current.permissionId)) return false;
996
+ visited.add(current.permissionId);
997
+
998
+ if (current.pid == pid) return true;
999
+ current = permissionMap.get(current.pid); // O(1) 查找
1000
+ }
1001
+ return false;
1002
+ };
1003
+
1004
+ const res = [];
1005
+ for (const permission of permissionList) {
1006
+ if (isChildOf(permission)) {
1007
+ const per2 = deepCopy(permission);
1008
+ per2.appId = pid;
1009
+ if (per2.pid == pid) {
1010
+ per2.pid = "0";
986
1011
  }
1012
+ per2.appInfo = deepCopy(appInfo);
1013
+ res.push(per2);
987
1014
  }
988
1015
  }
1016
+
989
1017
  return res;
990
1018
  }
991
1019
  /**
@@ -1031,16 +1059,38 @@ export const buildSinglePermission = (permissionList, menu) => {
1031
1059
  * @returns {*|{pid}|{}}
1032
1060
  */
1033
1061
  export const getRootPermission = (permissionList, permission) => {
1034
- if (permissionList, permission) {
1035
- if (!permission.hasOwnProperty('pid')) {
1036
- return permission
1037
- } else if (permission.pid == '0') {
1038
- return permission
1039
- } else {
1040
- let parentPermission = permissionList.find(item => item.permissionId == permission.pid && item.appId == permission.appId)
1041
- return getRootPermission(permissionList, parentPermission);
1062
+ if (!permissionList || !permission) return permission;
1063
+
1064
+ // 构建 Map,O(n)
1065
+ const permissionMap = new Map();
1066
+ for (const item of permissionList) {
1067
+ if (item && item.permissionId != null) {
1068
+ const key = `${item.permissionId}|${item.appId || ''}`;
1069
+ permissionMap.set(key, item);
1042
1070
  }
1043
1071
  }
1072
+
1073
+ // 迭代向上查找根节点
1074
+ let current = permission;
1075
+ const visited = new Set();
1076
+
1077
+ while (current) {
1078
+ if (!current.pid || current.pid === '0') {
1079
+ return current;
1080
+ }
1081
+
1082
+ // 循环引用检测
1083
+ const key = `${current.permissionId}|${current.appId || ''}`;
1084
+ if (visited.has(key)) return permission;
1085
+ visited.add(key);
1086
+
1087
+ // O(1) 查找父节点
1088
+ const parentKey = `${current.pid}|${current.appId || ''}`;
1089
+ current = permissionMap.get(parentKey);
1090
+
1091
+ if (!current) return permission;
1092
+ }
1093
+
1044
1094
  return permission;
1045
1095
  }
1046
1096
  /**