wsj
5 days ago b09eb09acd24d885bbd610fd96c2114b4711e99b
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
import type { MenuProps } from 'antd';
import type { AppRouteItem } from '@/router/routeConfig';
 
type MenuItem = Required<MenuProps>['items'][number];
 
/** 把路由树转换成 antd Menu 需要的结构。 */
export const buildMenuItems = (routes: AppRouteItem[]): MenuItem[] => {
  const items: MenuItem[] = [];
 
  for (const route of routes) {
    if (route.hideInMenu) continue;
 
    const children = route.children ? buildMenuItems(route.children) : [];
 
    items.push({
      key: route.path,
      icon: route.icon,
      label: route.title,
      ...(children.length > 0 ? { children } : {}),
    } as MenuItem);
  }
 
  return items;
};
 
/** 根据当前地址推导需要展开的父级菜单 key。 */
export const getOpenKeys = (pathname: string): string[] => {
  const segments = pathname.split('/').filter(Boolean);
  return segments.slice(0, -1).map((_, index) => `/${segments.slice(0, index + 1).join('/')}`);
};