36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
/** 扁平化侧边栏用语义路径,顺序与侧边栏自上而下一致(与 MobileBottomNav 逻辑对齐) */
|
|
|
|
export function flattenNavigableRoutes(routes, parentPath = '') {
|
|
if (!routes || !Array.isArray(routes)) return []
|
|
const result = []
|
|
routes.forEach(route => {
|
|
if (route.hidden) return
|
|
let fullPath = (route.path || '').trim()
|
|
if (parentPath) {
|
|
if (!fullPath.startsWith('/')) {
|
|
const base = parentPath.endsWith('/') ? parentPath.slice(0, -1) : parentPath
|
|
fullPath = `${base}/${fullPath}`.replace(/\/+/g, '/')
|
|
}
|
|
}
|
|
if (fullPath && !fullPath.startsWith('/')) fullPath = '/' + fullPath
|
|
if (route.children && route.children.length > 0) {
|
|
result.push(...flattenNavigableRoutes(route.children, fullPath || parentPath))
|
|
} else if (route.meta && route.meta.title && fullPath && fullPath !== '/') {
|
|
result.push({ path: fullPath, meta: route.meta })
|
|
}
|
|
})
|
|
return result
|
|
}
|
|
|
|
/** 侧边栏路由中首个可打开的叶子路径(用于默认首页);无则 null */
|
|
export function firstLeafPathFromSidebarRoutes(sidebarRoutes) {
|
|
const flat = flattenNavigableRoutes(sidebarRoutes)
|
|
const exclude = ['/redirect', '/login', '/register', '/404', '/401', '/user/profile']
|
|
for (const { path } of flat) {
|
|
if (!path) continue
|
|
if (exclude.some(p => path === p || path.startsWith(p + '/'))) continue
|
|
return path
|
|
}
|
|
return null
|
|
}
|