import { defineStore } from 'pinia' import { ref } from 'vue' import type { RouteComponent } from 'vue-router' import router from '@/router' import { getMenuTree } from '@/api/menu' import { useAuthStore } from '@/store/auth' export interface MenuItem { id: number parent_id: number name: string type: number // 1目录 2菜单 3按钮 path?: string | null icon?: string | null permission?: string | null component?: string | null sort: number status: number children?: MenuItem[] } // 接口不可用时的兜底菜单(与后端种子数据保持一致的设计) const defaultMenus: MenuItem[] = [ { id: 1, parent_id: 0, name: '仪表盘', type: 2, path: '/dashboard', icon: 'HomeFilled', component: 'views/Home.vue', sort: 100, status: 1 }, { id: 4, parent_id: 0, name: '订单管理', type: 1, icon: 'List', sort: 90, status: 1, children: [ { id: 42, parent_id: 4, name: '创建发货单', type: 2, path: '/order-create', icon: 'Plus', permission: 'orderDelivery:create', component: 'views/Order/Create.vue', sort: 80, status: 1 }, { id: 41, parent_id: 4, name: '订单', type: 2, path: '/order', icon: 'Tickets', permission: 'order:list', component: 'views/Order/Index.vue', sort: 70, status: 1 }, ], }, { id: 3, parent_id: 0, name: '商品管理', type: 1, icon: 'Goods', sort: 80, status: 1, children: [ { id: 31, parent_id: 3, name: '商品', type: 2, path: '/product', icon: 'Box', permission: 'product:list', component: 'views/Product/Index.vue', sort: 80, status: 1 }, { id: 32, parent_id: 3, name: '组合商品', type: 2, path: '/combo-product', icon: 'Files', permission: 'comboProduct:list', component: 'views/ComboProduct/Index.vue', sort: 70, status: 1 }, { id: 52, parent_id: 3, name: '店铺商品', type: 2, path: '/shop-product', icon: 'GoodsFilled', permission: 'shopProduct:list', component: 'views/ShopProduct/Index.vue', sort: 65, status: 1 }, ], }, { id: 6, parent_id: 0, name: '库存管理', type: 1, icon: 'Box', sort: 82, status: 1, children: [ { id: 61, parent_id: 6, name: '入库单', type: 2, path: '/inbound-order', icon: 'Bottom', permission: 'inboundOrder:list', component: 'views/InboundOrder/Index.vue', sort: 75, status: 1 }, { id: 62, parent_id: 6, name: '出库单', type: 2, path: '/outbound-order', icon: 'Top', permission: 'outboundOrder:list', component: 'views/OutboundOrder/Index.vue', sort: 70, status: 1 }, ], }, { id: 5, parent_id: 0, name: '店铺管理', type: 1, icon: 'Shop', sort: 85, status: 1, children: [ { id: 51, parent_id: 5, name: '店铺', type: 2, path: '/shop', icon: 'Shop', permission: 'shop:list', component: 'views/Shop/Index.vue', sort: 80, status: 1 }, ], }, { id: 25, parent_id: 0, name: '外部仓库', type: 2, path: '/system/external-api', icon: 'Link', permission: 'principalExternalApi:list', component: 'views/ExternalApi/Index.vue', sort: 75, status: 1 }, { id: 2, parent_id: 0, name: '系统管理', type: 1, icon: 'Setting', sort: 70, status: 1, children: [ { id: 21, parent_id: 2, name: '我的', type: 2, path: '/system/user', icon: 'User', permission: null, component: 'views/User/Profile.vue', sort: 90, status: 1 }, { id: 22, parent_id: 2, name: '主体管理', type: 2, path: '/system/principal', icon: 'OfficeBuilding', permission: 'principal:list', component: 'views/Principal/Index.vue', sort: 80, status: 1 }, { id: 23, parent_id: 2, name: '账号', type: 2, path: '/system/principal-user', icon: 'Connection', permission: 'principalUser:list', component: 'views/PrincipalUser/Index.vue', sort: 70, status: 1 }, ], }, ] // 预加载所有视图组件,供动态路由按 component 路径解析。 // 使用相对路径(相对于本文件 src/store/),保证 dev/build 下 key 格式一致、可稳定命中。 const viewModules = import.meta.glob('../views/**/*.vue') // 无主体时仅展示的「创建主体」面板菜单 const createPrincipalMenu: MenuItem = { id: 9001, parent_id: 0, name: '创建主体', type: 2, path: '/principal/create', icon: 'OfficeBuilding', component: 'views/Principal/Create.vue', sort: 1, status: 1, } function resolveComponent(component?: string | null): RouteComponent { if (component) { const key = component.startsWith('../') ? component : `../${component}` const loader = viewModules[key] as unknown as RouteComponent | undefined if (loader) return loader } return viewModules['../views/Generic.vue'] as unknown as RouteComponent } function flatten(tree: MenuItem[]): MenuItem[] { return tree.flatMap((m) => [m, ...(m.children ? flatten(m.children) : [])]) } export const useMenuStore = defineStore('menu', () => { const menus = ref([]) const loaded = ref(false) /** 始终注册「创建主体」路由,保证该页面可直接访问 */ function registerCreateRoute() { if (!createPrincipalMenu.path) return router.addRoute('layout', { path: createPrincipalMenu.path.replace(/^\//, ''), name: `menu-${createPrincipalMenu.id}`, component: resolveComponent(createPrincipalMenu.component), meta: { title: createPrincipalMenu.name, icon: createPrincipalMenu.icon, permission: createPrincipalMenu.permission, }, }) } /** 拉取菜单树 → 构建侧边栏 + 动态路由 */ async function fetchMenus() { // 无主体:仅展示「创建主体」面板,隐藏其余菜单 if (!useAuthStore().hasPrincipal) { menus.value = [createPrincipalMenu] } else { let tree: MenuItem[] = [] try { tree = await getMenuTree() } catch { tree = [] } if (!tree.length) tree = JSON.parse(JSON.stringify(defaultMenus)) menus.value = tree } registerRoutes(menus.value) registerCreateRoute() // 创建主体路由始终可用,避免直接访问空白 loaded.value = true } /** 将菜单(type=2)注册为 layout 下的动态子路由 */ function registerRoutes(tree: MenuItem[]) { // 清理上一次注册的动态路由,避免重复添加 router .getRoutes() .filter((r) => typeof r.name === 'string' && r.name.startsWith('menu-')) .forEach((r) => router.removeRoute(r.name as string)) flatten(tree).forEach((item) => { if (item.type !== 2 || !item.path) return router.addRoute('layout', { path: item.path.replace(/^\//, ''), name: `menu-${item.id}`, component: resolveComponent(item.component), meta: { title: item.name, icon: item.icon, permission: item.permission }, }) }) } /** 重置(退出登录时调用) */ function reset() { router .getRoutes() .filter((r) => typeof r.name === 'string' && r.name.startsWith('menu-')) .forEach((r) => router.removeRoute(r.name as string)) menus.value = [] loaded.value = false } return { menus, loaded, fetchMenus, reset } })