|
|
|
|
@ -46,6 +46,32 @@ const parseURL = (
|
|
|
|
|
return { basePath, paramsObject }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resolveChildPath = (parentPath: string, childPath: string) => {
|
|
|
|
|
if (!childPath) return parentPath
|
|
|
|
|
if (childPath.startsWith('/')) return childPath
|
|
|
|
|
const p = parentPath.endsWith('/') ? parentPath.slice(0, -1) : parentPath
|
|
|
|
|
return `${p}/${childPath}`.replace(/\/+/g, '/')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resolveFirstMenuPath = (routes: any[], parentPath = ''): string | undefined => {
|
|
|
|
|
for (const r of routes ?? []) {
|
|
|
|
|
if (!r) continue
|
|
|
|
|
if (typeof r?.path === 'string' && r.path.includes(':path(.*)*')) continue
|
|
|
|
|
if (r?.meta?.hidden === true) continue
|
|
|
|
|
|
|
|
|
|
if (typeof r.redirect === 'string' && r.redirect.trim()) {
|
|
|
|
|
const redirect = r.redirect.trim()
|
|
|
|
|
if (redirect !== '/') return redirect
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentPath = parentPath ? resolveChildPath(parentPath, String(r.path ?? '')) : String(r.path ?? '')
|
|
|
|
|
const childPath = resolveFirstMenuPath(r.children, currentPath)
|
|
|
|
|
if (childPath) return childPath
|
|
|
|
|
if (currentPath && currentPath !== '/') return currentPath
|
|
|
|
|
}
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 路由不重定向白名单
|
|
|
|
|
const whiteList = [
|
|
|
|
|
'/login',
|
|
|
|
|
@ -81,12 +107,21 @@ router.beforeEach(async (to, from, next) => {
|
|
|
|
|
router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
|
|
|
|
|
})
|
|
|
|
|
const redirectPath = from.query.redirect || to.path
|
|
|
|
|
// 修复跳转时不带参数的问题
|
|
|
|
|
const redirect = decodeURIComponent(redirectPath as string)
|
|
|
|
|
const { basePath, paramsObject: query } = parseURL(redirect)
|
|
|
|
|
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
|
|
|
|
|
let targetPath = redirect
|
|
|
|
|
let targetQuery = query
|
|
|
|
|
if (basePath === '/' || basePath === '') {
|
|
|
|
|
targetPath = resolveFirstMenuPath(permissionStore.addRouters) ?? '/empty'
|
|
|
|
|
targetQuery = {}
|
|
|
|
|
}
|
|
|
|
|
const nextData = to.path === targetPath ? { ...to, replace: true } : { path: targetPath, query: targetQuery }
|
|
|
|
|
next(nextData)
|
|
|
|
|
} else {
|
|
|
|
|
if (to.path === '/') {
|
|
|
|
|
next({ path: resolveFirstMenuPath(permissionStore.addRouters) ?? '/empty', replace: true })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|