fixed: 增加错误设置首页的补救措施

This commit is contained in:
piexlMax(奇淼 2026-01-12 15:11:59 +08:00
parent 2242f5d6e1
commit fd2a6f45fd
1 changed files with 17 additions and 18 deletions

View File

@ -75,6 +75,22 @@ function addRouteByChildren(route, segments = [], parentName = null) {
// 还有子节点,继续向下收集路径片段(忽略外链片段) // 还有子节点,继续向下收集路径片段(忽略外链片段)
if (route?.children && route.children.length) { if (route?.children && route.children.length) {
if(!parentName){
const firstChild = route.children[0]
if (firstChild) {
const fullParentPath = [...segments, route.path].filter(Boolean).join('/')
const redirectPath = normalizeRelativePath(
[fullParentPath, firstChild.path].filter(Boolean).join('/')
)
const parentRoute = {
path: normalizeRelativePath(fullParentPath),
name: route.name, // 保留父级名称,以便 defaultRouter 可以指向它
meta: route.meta,
redirect: "/layout/" + redirectPath,
}
router.addRoute('layout', parentRoute)
}
}
const nextSegments = isExternalUrl(route.path) ? segments : [...segments, route.path] const nextSegments = isExternalUrl(route.path) ? segments : [...segments, route.path]
route.children.forEach((child) => addRouteByChildren(child, nextSegments, parentName)) route.children.forEach((child) => addRouteByChildren(child, nextSegments, parentName))
return return
@ -157,13 +173,6 @@ const handleKeepAlive = async (to) => {
} }
} }
// 处理路由重定向
const handleRedirect = (to, userStore) => {
if (router.hasRoute(userStore.userInfo.authority.defaultRouter)) {
return { ...to, replace: true }
}
return { path: '/layout/404' }
}
// 路由守卫 // 路由守卫
router.beforeEach(async (to, from) => { router.beforeEach(async (to, from) => {
@ -176,10 +185,8 @@ router.beforeEach(async (to, from) => {
// 处理元数据和缓存 // 处理元数据和缓存
to.meta.matched = [...to.matched] to.meta.matched = [...to.matched]
await handleKeepAlive(to) await handleKeepAlive(to)
// 设置页面标题 // 设置页面标题
document.title = getPageTitle(to.meta.title, to) document.title = getPageTitle(to.meta.title, to)
if (to.meta.client) { if (to.meta.client) {
return true return true
} }
@ -208,15 +215,7 @@ router.beforeEach(async (to, from) => {
// 处理异步路由 // 处理异步路由
if (!routerStore.asyncRouterFlag && !WHITE_LIST.includes(from.name)) { if (!routerStore.asyncRouterFlag && !WHITE_LIST.includes(from.name)) {
const setupSuccess = await setupRouter(userStore) const setupSuccess = await setupRouter(userStore)
return to
if (setupSuccess && userStore.token) {
return handleRedirect(to, userStore)
}
return {
name: 'Login',
query: { redirect: to.fullPath }
}
} }
return to.matched.length ? true : { path: '/layout/404' } return to.matched.length ? true : { path: '/layout/404' }