@@ -9,16 +9,11 @@ import { $t } from '@/locales';
9
9
* @param routes Auth routes
10
10
*/
11
11
export function filterRoutesToMenus ( routes : RouteObject [ ] ) {
12
- console . log ( routes , 'routes' ) ;
13
12
const menus : App . Global . Menu [ ] = [ ] ;
14
13
15
- const cacheRoutes : string [ ] = [ ] ;
16
-
17
14
for ( const route of routes ) {
18
15
// 如果节点存在 path(注意:这里假设空字符串或 undefined 均视为无 path)
19
- if ( route . handle ?. keepAlive ) {
20
- cacheRoutes . push ( route . path as string ) ;
21
- }
16
+
22
17
if ( route . path && ! route . handle ?. hideInMenu ) {
23
18
// 如果存在 children,则递归处理
24
19
const newNode = getGlobalMenuByBaseRoute ( route ) ;
@@ -85,3 +80,43 @@ export function getActiveFirstLevelMenuKey(route: App.Global.TabRoute) {
85
80
86
81
return firstLevelRouteName ;
87
82
}
83
+
84
+ export function mergeMenus ( menus : App . Global . Menu [ ] , newMenus : App . Global . Menu [ ] ) {
85
+ newMenus . forEach ( newMenu => {
86
+ const newMenuKey = newMenu . key . split ( '/' ) ; // 分割路径
87
+
88
+ function findAndMergeParent ( currentMenus : App . Global . Menu [ ] , menuPath : string [ ] ) : boolean {
89
+ for ( const menu of currentMenus ) {
90
+ // 判断当前菜单的路径是否匹配,使用 startsWith 来判断路径的前缀
91
+ const menuKeyParts = menu . key . split ( '/' ) ;
92
+
93
+ // 如果路径的前缀一致,进一步递归查找子菜单
94
+ if ( menuKeyParts [ 1 ] === menuPath [ 1 ] ) {
95
+ // 如果匹配到父级菜单的路径并且这个菜单没有 children,则初始化 children
96
+ if ( ! menu . children ) {
97
+ menu . children = [ ] ;
98
+ }
99
+
100
+ // 如果 newMenu 的路径和当前菜单的路径匹配,递归查找它的子菜单
101
+ if ( menuPath . length === 3 ) {
102
+ // 如果路径已完全匹配,将 newMenu 添加到子菜单中
103
+ menu . children . push ( newMenu ) ;
104
+
105
+ return true ;
106
+ }
107
+
108
+ // 如果路径部分匹配,递归检查当前菜单的 children
109
+ return findAndMergeParent ( menu . children || [ ] , menuPath . slice ( 1 ) ) ;
110
+ }
111
+ }
112
+ return false ;
113
+ }
114
+
115
+ // 如果没有找到父级,将 newMenu 直接添加到 menus
116
+ if ( ! findAndMergeParent ( menus , newMenuKey ) ) {
117
+ menus . push ( newMenu ) ;
118
+ }
119
+ } ) ;
120
+
121
+ return menus ;
122
+ }
0 commit comments