86 lines
3.9 KiB
JavaScript
86 lines
3.9 KiB
JavaScript
const removedRoots = new Set(['resource', 'test', 'vi', 'is'])
|
||
const removedTitles = new Set(['首页', '资源中心', '测试管理', '语音交互', '触控交互'])
|
||
|
||
/**
|
||
* 按 Vue Router 的绝对子路径语义拼接菜单路径,外链保持原样。
|
||
* @param {string} parentPath 父级完整路径
|
||
* @param {string} routePath 当前菜单的绝对或相对路径
|
||
* @returns {string} 规范化后的完整路径,不重复拼接绝对子路径
|
||
*/
|
||
export function resolveRoutePath(parentPath = '', routePath = '') {
|
||
if (/^(https?:)?\/\//i.test(routePath)) return routePath
|
||
const path = routePath.startsWith('/') ? routePath : `${parentPath}/${routePath}`
|
||
return path.replace(/\/+/g, '/').replace(/\/$/, '') || '/'
|
||
}
|
||
|
||
/**
|
||
* 判断旧首页及被移除业务的访问路径,按完整路径段匹配以免误伤智能巡检等功能。
|
||
* @param {string} path 页面路径,允许包含历史书签的 query 和 hash
|
||
* @returns {boolean} 是否属于已删除业务;不改变后端 API 地址的含义
|
||
*/
|
||
export function isRemovedPath(path = '') {
|
||
const pathname = String(path).split(/[?#]/)[0]
|
||
const parts = pathname.replace(/^\/+/, '').split('/')
|
||
return (parts[0] === 'index' && parts.length === 1) || removedRoots.has(parts[0])
|
||
}
|
||
|
||
/**
|
||
* 在注册路由前裁剪已删除模块,同时维持系统菜单管理的原有隐藏设置。
|
||
* 结合路径、组件标识和标题处理后端历史菜单;复制节点、不修改响应原对象或角色权限。
|
||
* @param {Array} routes 后端返回的路由树
|
||
* @param {boolean} hideMenuManagement 是否隐藏系统管理中的菜单管理入口
|
||
* @param {string} parentPath 父级完整路径,用于识别相对子菜单
|
||
* @returns {Array} 可用于本项目的路由树,已删除内容和空目录不再注册
|
||
*/
|
||
export function filterRetainedRoutes(routes, hideMenuManagement = true, parentPath = '') {
|
||
const retained = []
|
||
for (const route of routes) {
|
||
const fullPath = resolveRoutePath(parentPath, route.path || '')
|
||
const component = typeof route.component === 'string'
|
||
? route.component.replace(/^@\/views\//, '').replace(/^\/+|\.vue$/g, '')
|
||
: ''
|
||
if (isRemovedPath(fullPath) || isRemovedPath(component) || removedTitles.has(route.meta?.title)) continue
|
||
|
||
const menu = { ...route }
|
||
if (route.children?.length) {
|
||
menu.children = filterRetainedRoutes(route.children, hideMenuManagement, fullPath)
|
||
if (!menu.children.length) continue
|
||
}
|
||
if (typeof menu.redirect === 'string' && isRemovedPath(menu.redirect)) delete menu.redirect
|
||
if (hideMenuManagement && (fullPath === '/system/menu' || component === 'system/menu/index')) menu.hidden = true
|
||
retained.push(menu)
|
||
}
|
||
return retained
|
||
}
|
||
|
||
/**
|
||
* 按后端授权菜单顺序寻找可进入的保留页面,跳过隐藏目录、外链及需要参数的详情页。
|
||
* @param {Array} routes 已完成模块裁剪、组件解析的授权菜单树
|
||
* @param {string} parentPath 父级路径
|
||
* @returns {{path: string, query?: object}|null} 首个菜单导航位置;无可用页面返回 null,由守卫转入无权限页
|
||
*/
|
||
export function findFirstMenuRoute(routes, parentPath = '') {
|
||
for (const route of routes) {
|
||
if (route.hidden) continue
|
||
const path = resolveRoutePath(parentPath, route.path || '')
|
||
if (/^(https?:)?\/\//i.test(path) || isRemovedPath(path)) continue
|
||
if (route.children?.length) {
|
||
const child = findFirstMenuRoute(route.children, path)
|
||
if (child) return child
|
||
continue
|
||
}
|
||
if (!route.meta?.title || path === '/' || path.includes(':')) continue
|
||
const target = { path }
|
||
if (route.query) {
|
||
try {
|
||
const query = typeof route.query === 'string' ? JSON.parse(route.query) : route.query
|
||
if (query && typeof query === 'object' && !Array.isArray(query)) target.query = query
|
||
} catch {
|
||
// 后端菜单的可选 query 格式错误不应阻断整个默认导航。
|
||
}
|
||
}
|
||
return target
|
||
}
|
||
return null
|
||
}
|