150 lines
4.2 KiB
TypeScript
150 lines
4.2 KiB
TypeScript
/**
|
|
* 路由工具模块
|
|
*
|
|
* 提供路由处理和菜单路径相关的工具函数
|
|
*
|
|
* ## 主要功能
|
|
*
|
|
* - iframe 路由检测,判断是否为外部嵌入页面
|
|
* - 菜单项有效性验证,过滤隐藏和无效菜单
|
|
* - 路径标准化处理,统一路径格式
|
|
* - 递归查找菜单树中第一个有效路径
|
|
* - 支持多级嵌套菜单的路径解析
|
|
*
|
|
* ## 使用场景
|
|
*
|
|
* - 系统初始化时获取默认跳转路径
|
|
* - 菜单权限过滤后获取首个可访问页面
|
|
* - 路由重定向逻辑处理
|
|
* - iframe 页面特殊处理
|
|
*
|
|
* @module utils/navigation/route
|
|
* @author Art Design Pro Team
|
|
*/
|
|
|
|
import { AppRouteRecord } from '@/types'
|
|
import { filterSidebarRoutes } from '@/utils/router/sidebar-menu-filter'
|
|
|
|
// 检查是否为 iframe 路由
|
|
export function isIframe(url: string): boolean {
|
|
return url.startsWith('/outside/iframe/')
|
|
}
|
|
|
|
const WORKBENCH_PATH_RE = /\/console$|\/dashboard\/console(\/|$)/
|
|
|
|
/** 是否为已废弃/不需展示标签的「工作台」路由 */
|
|
export function isWorkbenchRoute(path?: string): boolean {
|
|
if (!path?.trim()) {
|
|
return false
|
|
}
|
|
const normalized = path.replace(/\/+$/, '') || '/'
|
|
return WORKBENCH_PATH_RE.test(normalized) || normalized === '/console'
|
|
}
|
|
|
|
/** 菜单项是否为工作台(侧栏可保留,标签栏与默认落点需排除) */
|
|
export function isWorkbenchMenuItem(menuItem: AppRouteRecord): boolean {
|
|
if (isWorkbenchRoute(menuItem.path)) {
|
|
return true
|
|
}
|
|
const component = String(menuItem.component ?? '')
|
|
if (component.includes('/dashboard/console')) {
|
|
return true
|
|
}
|
|
if (menuItem.meta?.menuCode === 'dashboard:console') {
|
|
return true
|
|
}
|
|
const name = String(menuItem.name ?? '')
|
|
if (name === 'Console' || name === 'console') {
|
|
return true
|
|
}
|
|
const title = String(menuItem.meta?.title ?? '')
|
|
return title === '工作台' || title === 'menus.dashboard.console'
|
|
}
|
|
|
|
/**
|
|
* 判断菜单项是否可作为默认导航落点
|
|
* 隐藏的全屏页面虽然不展示在菜单中,但仍然可能是合法首页。
|
|
*/
|
|
export const isNavigableMenuItem = (menuItem: AppRouteRecord): boolean => {
|
|
if (!menuItem.path || !menuItem.path.trim()) {
|
|
return false
|
|
}
|
|
|
|
if (!menuItem.meta?.isHide) {
|
|
return true
|
|
}
|
|
|
|
return menuItem.meta?.isFullPage === true
|
|
}
|
|
|
|
/**
|
|
* 标准化路径格式
|
|
* @param path 路径
|
|
* @returns 标准化后的路径
|
|
*/
|
|
const normalizePath = (path: string): string => {
|
|
return path.startsWith('/') ? path : `/${path}`
|
|
}
|
|
|
|
/**
|
|
* 递归获取菜单的第一个有效路径
|
|
* @param menuList 菜单列表
|
|
* @param parentPath 父级路径前缀(内部递归使用)
|
|
* @returns 第一个有效路径,如果没有找到则返回空字符串
|
|
*/
|
|
export const getFirstMenuPath = (menuList: AppRouteRecord[], parentPath = ''): string => {
|
|
if (!Array.isArray(menuList) || menuList.length === 0) {
|
|
return ''
|
|
}
|
|
|
|
for (const menuItem of menuList) {
|
|
if (!isNavigableMenuItem(menuItem) || isWorkbenchMenuItem(menuItem)) {
|
|
continue
|
|
}
|
|
|
|
const currentFullPath = menuItem.path!.startsWith('/')
|
|
? menuItem.path!
|
|
: parentPath
|
|
? `${parentPath}/${menuItem.path!}`
|
|
: normalizePath(menuItem.path!)
|
|
|
|
if (menuItem.children?.length) {
|
|
const childPath = getFirstMenuPath(menuItem.children, currentFullPath)
|
|
if (childPath) {
|
|
return childPath
|
|
}
|
|
}
|
|
|
|
return currentFullPath.startsWith('/') ? currentFullPath : `/${currentFullPath}`
|
|
}
|
|
|
|
return ''
|
|
}
|
|
|
|
/**
|
|
* 获取侧栏第一个二级菜单对应的路由路径(与 SidebarSubmenu level=1 一致)
|
|
* 若不存在二级结构则回退到第一个可导航菜单路径
|
|
*/
|
|
export const getFirstSecondLevelMenuPath = (menuList: AppRouteRecord[]): string => {
|
|
const filtered = filterSidebarRoutes(menuList)
|
|
if (!filtered.length) {
|
|
return ''
|
|
}
|
|
|
|
for (const topItem of filtered) {
|
|
const children = topItem.children ? filterSidebarRoutes(topItem.children) : []
|
|
if (!children.length) {
|
|
continue
|
|
}
|
|
|
|
const topPath = topItem.path!.startsWith('/') ? topItem.path! : normalizePath(topItem.path!)
|
|
|
|
const childPath = getFirstMenuPath(children, topPath)
|
|
if (childPath) {
|
|
return childPath
|
|
}
|
|
}
|
|
|
|
return getFirstMenuPath(filtered)
|
|
}
|