menus.ts 4.52 KB
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;
}