menus.ts
6.13 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
import type { MenuItem, TopMenuItem } from "@/types/menu";
/**
* 侧边栏菜单配置示例
* 支持一级、二级、三级菜单结构
* 注意:这些是默认菜单配置,实际菜单将从 API 动态获取并存储在 menu store 中
*/
// 首页侧边栏菜单
export const homeMenus: MenuItem[] = [];
// Vue 微应用侧边栏菜单(多级嵌套示例)
// 支持一级、二级、三级菜单嵌套结构
// 注意:此配置仅作为后备方案,实际菜单将从 API 动态获取
export const vueAppMenus: MenuItem[] = [];
/**
* 默认顶部导航菜单配置
* 每个顶部菜单可以关联不同的侧边栏菜单
* 注意:实际菜单将从 API 动态获取,此配置仅作为后备方案
*/
export const defaultTopMenus: TopMenuItem[] = [];
/**
* 根据路径获取对应的侧边栏菜单
* 注意:此函数已废弃,请使用 menu store 中的 getSidebarMenusByPath 方法
* @deprecated 请使用 useMenuStore().getSidebarMenusByPath()
*/
export function getSidebarMenusByPath(path: string): MenuItem[] {
const topMenu = defaultTopMenus.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;
}
/**
* 检查路径是否匹配(支持动态路由参数)
* @param menuPath 菜单配置的路径(可能包含 :id, :param 等参数)
* @param targetPath 实际路由路径
* @returns 是否匹配
*/
export function isPathMatch(menuPath: string, targetPath: string): boolean {
if (!menuPath || !targetPath) {
return false;
}
// 去掉末尾多余的 /,方便匹配像 /edit/ 与 /edit/2 这类场景
const normalizedMenuPath = menuPath.replace(/\/+$/, "");
const normalizedTargetPath = targetPath.replace(/\/+$/, "");
// 精确匹配
if (normalizedMenuPath === normalizedTargetPath) {
return true;
}
// 如果菜单路径包含参数占位符(如 :id),转换为正则表达式匹配
if (normalizedMenuPath.includes(":")) {
// 将路径中的 :id, :param 等替换为正则表达式的非斜杠字符匹配
// 例如: /assisant/assistant/edit/:id -> /assisant/assistant/edit/[^/]+
const pattern = normalizedMenuPath
.replaceAll(/:[^/]+/g, "[^/]+")
.replaceAll("/", String.raw`\/`);
// 匹配完整路径,例如: /assisant/assistant/edit/123 应该匹配 /assisant/assistant/edit/:id
const regex = new RegExp(`^${pattern}$`);
return regex.test(normalizedTargetPath);
}
// 如果目标路径以菜单路径开头,也认为是匹配(用于处理动态路由参数)
// 例如:菜单路径是 /assisant/assistant/edit,实际路径是 /assisant/assistant/edit/123
// 检查菜单路径是否是目标路径的前缀,且目标路径的下一个字符是 / 或结束
if (normalizedTargetPath.startsWith(normalizedMenuPath)) {
const nextChar = normalizedTargetPath[normalizedMenuPath.length];
// 如果下一个字符是 / 或者是 undefined(路径结束),则匹配
if (nextChar === "/" || nextChar === undefined) {
return true;
}
}
return false;
}
/**
* 获取菜单的所有父级路径(包括自身)
* 支持动态路由参数匹配(如 /edit/:id 可以匹配 /edit/123)
* 返回的路径数组按照菜单层级顺序排列(从一级到当前级)
*
* @param menus 菜单列表
* @param targetPath 目标路径
* @returns 路径数组,按层级顺序排列
*/
export function getMenuParentPaths(
menus: MenuItem[],
targetPath: string,
): string[] {
const paths: string[] = [];
const normalizedTargetPath = targetPath.replace(/\/+$/, "");
/**
* 递归查找路径链
* @param items 当前层级的菜单项
* @param target 目标路径
* @param currentPathChain 当前已构建的路径链
* @returns 是否找到匹配项
*/
function findPathChain(
items: MenuItem[],
target: string,
currentPathChain: string[],
): boolean {
for (const item of items) {
// 构建新的路径链(包含当前项)
const newPathChain = [ ...currentPathChain, item.path ];
// 使用路径匹配检查当前项是否匹配目标路径
if (isPathMatch(item.path, target)) {
// 找到匹配项,将完整的路径链添加到结果中
paths.push(...newPathChain);
return true;
}
// 递归查找子菜单
if (item.children && item.children.length > 0 && findPathChain(item.children, target, newPathChain)) {
// 在子菜单中找到匹配项,路径链已经添加到 paths 中
return true;
}
}
return false;
}
// 开始查找路径链
findPathChain(menus, normalizedTargetPath, []);
// 返回去重后的路径数组(虽然理论上不应该有重复,但为了安全起见)
return Array.from(new Set(paths));
}
/**
* 查找菜单项及其所有父级菜单项
* 返回的数组按照菜单层级顺序排列(从一级到当前级)
*
* @param menus 菜单列表
* @param targetPath 目标路径
* @returns 菜单项数组,按层级顺序排列
*/
export function getMenuChain(
menus: MenuItem[],
targetPath: string,
): MenuItem[] {
const menuChain: MenuItem[] = [];
const normalizedTargetPath = targetPath.replace(/\/+$/, "");
/**
* 递归查找菜单链
*/
function findMenuChain(
items: MenuItem[],
target: string,
currentChain: MenuItem[],
): boolean {
for (const item of items) {
// 构建新的菜单链(包含当前项)
const newChain = [ ...currentChain, item ];
// 使用路径匹配检查当前项是否匹配目标路径
if (isPathMatch(item.path, target)) {
// 找到匹配项,将完整的菜单链添加到结果中
menuChain.push(...newChain);
return true;
}
// 递归查找子菜单
if (item.children && item.children.length > 0 && findMenuChain(item.children, target, newChain)) {
// 在子菜单中找到匹配项,菜单链已经添加到 menuChain 中
return true;
}
}
return false;
}
// 开始查找菜单链
findMenuChain(menus, normalizedTargetPath, []);
return menuChain;
}