import { computed } from "vue"; import { useRoute } from "vue-router"; import { useMenuStore } from "@/stores/menu"; import { pickDefaultChild, isWorkbenchMenu } from "@/router/menuDefaults"; const fixedCmsMenu = { id: -200, path: "/apps/cms", title: "内容管理", icon: "Document", sort: -200, order: -200, children: [ { id: -2001, path: "/apps/cms/analytics/content", title: "内容统计", icon: "DataAnalysis", sort: 0, order: 0, children: [], }, { id: -201, path: "/apps/cms/article", title: "文章管理", icon: "Document", sort: 1, order: 1, children: [], }, { id: -202, path: "/apps/cms/article/type", title: "文章分类", icon: "Folder", sort: 2, order: 2, children: [], }, { id: -203, path: "/apps/cms/products", title: "产品管理", icon: "Goods", sort: 3, order: 3, children: [], }, { id: -204, path: "/apps/cms/products/types", title: "产品分类", icon: "Folder", sort: 4, order: 4, children: [], }, { id: -205, path: "/apps/cms/solutions", title: "解决方案管理", icon: "Promotion", sort: 5, order: 5, children: [], }, { id: -206, path: "/apps/cms/solutions/types", title: "解决方案分类", icon: "Folder", sort: 6, order: 6, children: [], }, ], }; function processMenus(menus) { return (menus || []) .filter((menu) => { if (menu.is_visible !== undefined && Number(menu.is_visible) === 0) { return false; } if (menu.path && String(menu.path).trim() !== "") return true; return !!(menu.children && menu.children.length > 0); }) .map((menu) => { const sort = Number(menu.sort) || 0; return { id: menu.id, pid: menu.pid || 0, path: menu.path, icon: menu.icon || "Grid", title: menu.title, component_path: menu.component_path || "", type: menu.type, locked: !!menu.locked, sort, order: sort, children: menu.children ? processMenus(menu.children) : [], }; }); } function mergeFixedCmsMenu(menus) { return menus.some((menu) => menu.path === fixedCmsMenu.path) ? menus : [fixedCmsMenu, ...menus]; } const ERP_ROOT = "/apps/erp"; const ERP_WORKBENCH_DIR = "/apps/erp/workbench"; const ERP_DASHBOARD_PAGE = "/apps/erp/dashboard"; const ERP_APPROVAL_PAGE = "/apps/erp/workbench/approval"; function normalizeErpWorkbench(menus) { return (menus || []).map((menu) => { if (menu.path !== ERP_ROOT) { return menu.children?.length ? { ...menu, children: normalizeErpWorkbench(menu.children) } : menu; } const children = [...(menu.children || [])]; const dir = children.find((m) => m.path === ERP_WORKBENCH_DIR && m.children?.length); const dashboard = children.find((m) => m.path === ERP_DASHBOARD_PAGE); const pages = dir ? [...(dir.children || [])] : []; const hasDashboard = pages.some( (m) => m.path === ERP_DASHBOARD_PAGE || String(m.title || "") === "工作台" ); if (!hasDashboard) { pages.push( dashboard ? { ...dashboard, title: "工作台", sort: 1, order: 1 } : { id: -3001, path: ERP_DASHBOARD_PAGE, title: "工作台", icon: "DataBoard", component_path: "/apps/erp/dashboard/index.vue", type: 2, sort: 1, order: 1, children: [], } ); } if (!pages.some((m) => m.path === ERP_APPROVAL_PAGE)) { pages.push({ id: -3002, path: ERP_APPROVAL_PAGE, title: "审批列表", icon: "Tickets", component_path: "/apps/erp/workbench/approvalList.vue", type: 2, sort: 2, order: 2, children: [], }); } const workbenchDir = dir ? { ...dir, type: 1, component_path: "", children: pages } : { id: -300, pid: menu.id, path: ERP_WORKBENCH_DIR, title: "工作台", icon: "DataBoard", component_path: "", type: 1, sort: 1, order: 1, children: pages, }; const rest = children.filter( (m) => m.path !== ERP_WORKBENCH_DIR && m.path !== ERP_DASHBOARD_PAGE ); return { ...menu, children: [workbenchDir, ...rest] }; }); } function sortMenusRecursively(menus) { (menus || []).forEach((menu) => { if (menu.children && menu.children.length > 0) { menu.children.sort((a, b) => { if (a.order !== b.order) return a.order - b.order; return (Number(a.id) || 0) - (Number(b.id) || 0); }); sortMenusRecursively(menu.children); } }); return menus; } function findMenuItem(menus, targetIndex) { for (const menu of menus || []) { if (menu.path === targetIndex) return menu; if (menu.children && menu.children.length > 0) { const found = findMenuItem(menu.children, targetIndex); if (found) return found; } } return null; } function isPathInSubtree(base, currentPath) { if (!base) return false; const normalized = base.replace(/\/+$/, ""); return currentPath === normalized || currentPath.startsWith(`${normalized}/`); } function findParentModule(menus, currentPath) { for (const menu of menus) { if (!menu.path || menu.path === "/home") { const found = findParentModule(menu.children || [], currentPath); if (found) return found; continue; } if (isPathInSubtree(menu.path, currentPath)) return menu; for (const child of menu.children || []) { if (isPathInSubtree(child.path, currentPath)) return menu; for (const grandchild of child.children || []) { if (isPathInSubtree(grandchild.path, currentPath)) return menu; } } } return null; } function findActiveTopMenu(topMenus, currentPath) { let best = null; for (const top of topMenus) { const stack = [top]; while (stack.length) { const node = stack.pop(); if (node.path && isPathInSubtree(node.path, currentPath)) { const length = node.path.length; if (!best || length > best.length) { best = { menu: top, length }; } } if (node.children && node.children.length) { stack.push(...node.children); } } } return best?.menu || null; } function resolveLandingMenu(menu) { if (!menu) return null; if (menu.locked) return menu.path ? menu : null; let current = menu; const visited = new Set(); while (current.children && current.children.length > 0 && !visited.has(current.id)) { visited.add(current.id); if (current.component_path) break; const next = pickDefaultChild(current.children); if (!next) break; current = next; } return current.path ? current : menu.path ? menu : null; } function flattenMenus(menus, parentTitle = "") { const result = []; for (const menu of menus || []) { // 只把「可直接打开的页面」放进搜索结果,纯目录交给路由 redirect, // 否则会同时产生「目录」和「落地页」两个标签页 const isPage = !menu.children?.length || !!menu.component_path; if (menu.path && menu.path !== "/home" && isPage) { result.push({ id: menu.id, path: menu.path, title: menu.title, icon: menu.icon, locked: menu.locked, parentTitle: parentTitle || menu.title, }); } if (menu.children && menu.children.length) { result.push(...flattenMenus(menu.children, menu.title || parentTitle)); } } return result; } function findWorkbenchPage(menus) { const stack = [...menus]; const visited = new Set(); while (stack.length) { const node = stack.shift(); if (!node || visited.has(node.id)) continue; visited.add(node.id); if ( node.path && isWorkbenchMenu(node) && (!node.children?.length || !!node.component_path) ) { return node; } if (node.children?.length) stack.push(...node.children); } return null; } export function useAdminNavigation() { const route = useRoute(); const menuStore = useMenuStore(); const list = computed(() => sortMenusRecursively( processMenus(normalizeErpWorkbench(mergeFixedCmsMenu(menuStore.menus || []))) ), ); const currentModule = computed(() => { const path = route.path; if (!path || path === "/" || path === "/home") return null; return findParentModule(list.value, path); }); const topMenus = computed(() => currentModule.value?.children || []); const activeTopMenu = computed(() => { const menus = topMenus.value; if (!menus.length) return null; return ( findActiveTopMenu(menus, route.path) || pickDefaultChild(menus) || menus[0] || null ); }); const sideMenus = computed(() => { const active = activeTopMenu.value; if (!active) return []; return active.children && active.children.length ? active.children : [active]; }); const sideTitle = computed( () => activeTopMenu.value?.title || currentModule.value?.title || "功能菜单", ); const searchableMenus = computed(() => flattenMenus(list.value)); const defaultTab = computed(() => { const module = currentModule.value; if (!module) return null; const target = findWorkbenchPage(module.children || []) || resolveLandingMenu(module); return target?.path ? target : null; }); return { list, currentModule, topMenus, activeTopMenu, sideMenus, sideTitle, defaultTab, searchableMenus, findMenuItem, resolveLandingMenu, }; }