feat: enhance menu and routing data handling

This commit is contained in:
lix
2025-07-18 10:13:27 +08:00
parent bc07c6a472
commit 8461126a55
7 changed files with 52 additions and 14 deletions

View File

@ -16,10 +16,16 @@ export interface CurrentUser {
unreadCount?: number;
}
export interface PathKeyMap {
name: string;
icon?: string;
routeName?: string;
}
export interface UserModelState {
currentUser?: CurrentUser;
menuData?: any[];
pathKeyMap?: Map<string, {name: string, icon?: string}>;
pathKeyMap?: Map<string, PathKeyMap>;
}
export interface UserModelType {
@ -42,7 +48,7 @@ const UserModel: UserModelType = {
state: {
currentUser: {},
menuData: [],
pathKeyMap: new Map<string, {name: string, icon?: string}>(),
pathKeyMap: new Map<string, PathKeyMap>(),
},
effects: {
@ -70,8 +76,9 @@ const UserModel: UserModelType = {
};
},
saveMenuData(state, action) {
const menuData = action.payload || [];
const pathKeyMap = new Map<string, {name: string, icon?: string}>();
const menuData = action.payload.menuData || [];
const routeData = action.payload.routeData || [];
const pathKeyMap = new Map<string, PathKeyMap>();
// 将menuData转换为pathKeyMap 递归处理
const convertToPathKeyMap = (menuData: any[]) => {
menuData.forEach((item: any) => {
@ -83,7 +90,24 @@ const UserModel: UserModelType = {
});
};
convertToPathKeyMap(menuData);
pathKeyMap.set('/dashboard', { name: '首页' });
let i = 1;
while (routeData[i]) {
const item = routeData[i];
if (item.name) {
const pathname = item.path.toLowerCase();
if (pathKeyMap.has(pathname)) {
const target = pathKeyMap.get(pathname)!;
const newItem = {
...target,
routeName: item.name,
}
pathKeyMap.set(pathname, newItem);
} else {
pathKeyMap.set(pathname, { name: item.name, icon: item.icon });
}
}
i++;
}
return {
...state,
menuData: action.payload || [],