menus.ts
4.52 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import type { MenuItem, TopMenuItem } from '@/types/menu';
/**
* 侧边栏菜单配置示例
* 支持一级、二级、三级菜单结构
*/
// 首页侧边栏菜单
export const homeMenus: MenuItem[] = [
{
id: 'dashboard',
title: '工作台',
path: '/home/dashboard',
icon: '📊',
level: 1,
},
];
// Vue 微应用侧边栏菜单(多级示例)
export const vueAppMenus: MenuItem[] = [
{
id: 'contacts',
title: '子菜单示例',
path: '/subapp-vue/contacts',
icon: '📞',
level: 1,
children: [
{
id: 'contacts-list',
title: '关于',
path: '/subapp-vue/about',
icon: '📝',
level: 2,
parentId: 'contacts',
},
{
id: 'contacts-import',
title: '通信示例',
path: '/subapp-vue/communication',
icon: '📥',
level: 2,
parentId: 'contacts',
},
],
},
{
id: 'marketing',
title: '用户管理',
path: '/subapp-vue/user',
icon: '🎯',
level: 1,
children: [],
},
{
id: 'marketing',
title: '系统管理',
path: '/subapp-vue/settings',
icon: '🎯',
level: 1,
children: [],
},
];
// React 微应用侧边栏菜单(多级示例)
export const reactAppMenus: MenuItem[] = [
{
id: 'data-analysis',
title: '数据分析',
path: '/subapp-react/analysis',
icon: '📊',
level: 1,
children: [
{
id: 'sales-analysis',
title: '销售分析',
path: '/subapp-react/analysis/sales',
icon: '💰',
level: 2,
parentId: 'data-analysis',
children: [
{
id: 'sales-overview',
title: '销售概览',
path: '/subapp-react/analysis/sales/overview',
icon: '📈',
level: 3,
parentId: 'sales-analysis',
},
{
id: 'sales-trend',
title: '销售趋势',
path: '/subapp-react/analysis/sales/trend',
icon: '📉',
level: 3,
parentId: 'sales-analysis',
},
],
},
{
id: 'customer-analysis',
title: '客户分析',
path: '/subapp-react/analysis/customer',
icon: '👤',
level: 2,
parentId: 'data-analysis',
},
],
},
{
id: 'reports',
title: '报表中心',
path: '/subapp-react/reports',
icon: '📑',
level: 1,
children: [
{
id: 'reports-custom',
title: '自定义报表',
path: '/subapp-react/reports/custom',
icon: '⚙️',
level: 2,
parentId: 'reports',
},
{
id: 'reports-export',
title: '报表导出',
path: '/subapp-react/reports/export',
icon: '📤',
level: 2,
parentId: 'reports',
},
],
},
];
/**
* 顶部导航菜单配置
* 每个顶部菜单可以关联不同的侧边栏菜单
*/
export const topMenus: TopMenuItem[] = [
{
id: 'home',
title: '首页',
path: '/home',
icon: '🏠',
sidebarMenus: homeMenus,
},
{
id: 'vue-app',
title: '',
path: '/subapp-vue',
icon: '💚',
sidebarMenus: vueAppMenus,
},
{
id: 'react-app',
title: '子应用模板',
path: '/subapp-react',
icon: '⚛️',
sidebarMenus: reactAppMenus,
},
];
/**
* 根据路径获取对应的侧边栏菜单
*/
export function getSidebarMenusByPath(path: string): MenuItem[] {
const topMenu = topMenus.find((menu) => path.startsWith(menu.path));
return topMenu?.sidebarMenus || [];
}
/**
* 递归查找菜单项
*/
export function findMenuItem(menus: MenuItem[], id: string): MenuItem | null {
for (const menu of menus) {
if (menu.id === id) {
return menu;
}
if (menu.children) {
const found = findMenuItem(menu.children, id);
if (found) return found;
}
}
return null;
}
/**
* 获取菜单的所有父级路径
*/
export function getMenuParentPaths(
menus: MenuItem[],
targetPath: string,
): string[] {
const paths: string[] = [];
function findPath(
items: MenuItem[],
target: string,
currentPaths: string[],
): boolean {
for (const item of items) {
const newPaths = [...currentPaths, item.path];
if (item.path === target) {
paths.push(...newPaths);
return true;
}
if (item.children && findPath(item.children, target, newPaths)) {
return true;
}
}
return false;
}
findPath(menus, targetPath, []);
return paths;
}