This commit is contained in:
oneao committed 2026-10-09 22:05:02 +08:00
1 parent 0be0b0767a
commit 5f28a10d89
852 files changed
+8962 -111755

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import airRoutes from './modules/air'
import containerRoutes from './modules/container'
import crmRoutes from './modules/crm'
import financeRoutes from './modules/finance'
import oceanRoutes from './modules/ocean'
import systemRoutes from './modules/system'
import { APP_TITLE } from '@/constants/common'
import { menuItems } from '@/constants/menu'
import type { MenuItem } from '@/types/menu'
NProgress.configure({ showSpinner: false })
/** 由菜单常量派生首页重定向目标,保证「菜单唯一数据源」不被打破。 */
const firstMenuPath = menuItems[0]?.path ?? '/ocean'
const routes: RouteRecordRaw[] = [
{
path: '/',
component: () => import('@/layouts/DefaultLayout.vue'),
redirect: firstMenuPath,
children: [
...oceanRoutes,
...airRoutes,
...containerRoutes,
...financeRoutes,
...crmRoutes,
...systemRoutes,
],
},
{
path: '/:pathMatch(.*)*',
name: 'not-found',
component: () => import('@/views/error/NotFound.vue'),
meta: { title: '页面不存在' },
},
]
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes,
scrollBehavior: () => ({ top: 0 }),
})
router.beforeEach((to, _from, next) => {
NProgress.start()
const title = to.meta.title as string | undefined
document.title = title ? `${title} - ${APP_TITLE}` : APP_TITLE
// 登录校验待用户模块落地后接入;当前直接放行。
next()
})
router.afterEach(() => {
NProgress.done()
})
/**
* 在菜单树中按 path 查找菜单项(含子级)。
*/
export function findMenuItemByPath(path: string): MenuItem | undefined {
const walk = (items: MenuItem[]): MenuItem | undefined => {
for (const item of items) {
if (item.path === path) return item
if (item.children?.length) {
const found = walk(item.children)
if (found) return found
}
}
return undefined
}
return walk(menuItems)
}
/**
* 由当前路径反查它的所有祖先分组 key(从顶级到最近一级)。
* 支持多级嵌套与详情页:/ocean/booking/123 会命中「海运」「订舱业务」。
* 返回数组保证侧栏能逐层展开所有祖先分组。
*/
export function findParentKeysByPath(path: string): string[] {
const walk = (items: MenuItem[], parents: string[]): string[] | null => {
for (const item of items) {
// 命中叶子(无 children)或其自身 path:记录所有祖先
if (item.path === path || path.startsWith(`${item.path}/`)) {
return parents
}
// 有子级:分组,继续下钻
if (item.children?.length) {
const found = walk(item.children, [...parents, item.key])
if (found) return found
}
}
return null
}
return walk(menuItems, []) ?? []
}
/**
* 由当前路径反查所属的顶级分组 key(向后兼容,供需要单一顶级 key 的场景使用)。
*/
export function findParentKeyByPath(path: string): string | undefined {
return findParentKeysByPath(path)[0]
}
export default router