From 4e924c62eb5093cd62efbad1251dacbf0958b6af Mon Sep 17 00:00:00 2001 From: hwj Date: Mon, 2 Mar 2026 15:38:04 +0800 Subject: [PATCH] =?UTF-8?q?style=EF=BC=9A=E8=B7=AF=E7=94=B1=E3=80=81?= =?UTF-8?q?=E6=9D=83=E9=99=90=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/UserInfo/src/UserInfo.vue | 2 +- src/permission.ts | 39 +++++++++++- src/router/index.ts | 2 +- src/router/modules/remaining.ts | 60 ++++++++++++------- src/views/Empty/Index.vue | 12 ++++ src/views/iot/device/index.vue | 4 +- 6 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 src/views/Empty/Index.vue diff --git a/src/layout/components/UserInfo/src/UserInfo.vue b/src/layout/components/UserInfo/src/UserInfo.vue index 355aabc8..c10a50ed 100644 --- a/src/layout/components/UserInfo/src/UserInfo.vue +++ b/src/layout/components/UserInfo/src/UserInfo.vue @@ -43,7 +43,7 @@ const loginOut = async () => { }) await userStore.loginOut() tagsViewStore.delAllViews() - replace('/login?redirect=/index') + replace('/login?redirect=/') } catch {} } const toProfile = async () => { diff --git a/src/permission.ts b/src/permission.ts index d538303b..dfd774d7 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -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() } } diff --git a/src/router/index.ts b/src/router/index.ts index b818421c..96c7f414 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -12,7 +12,7 @@ const router = createRouter({ }) export const resetRouter = (): void => { - const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root'] + const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root', 'EmptyRoot', 'EmptyPage'] router.getRoutes().forEach((route) => { const { name } = route if (name && !resetWhiteNameList.includes(name as string)) { diff --git a/src/router/modules/remaining.ts b/src/router/modules/remaining.ts index dddf2046..659eafb2 100644 --- a/src/router/modules/remaining.ts +++ b/src/router/modules/remaining.ts @@ -50,26 +50,26 @@ const remainingRouter: AppRouteRecordRaw[] = [ noTagsView: true } }, - { - path: '/', - component: Layout, - redirect: '/index', - name: 'Home', - meta: {}, - children: [ - { - path: 'index', - component: () => import('@/views/Home/Index.vue'), - name: 'Index', - meta: { - title: t('router.home'), - icon: 'ep:home-filled', - noCache: false, - affix: true - } - } - ] - }, + // { + // path: '/', + // component: Layout, + // redirect: '/index', + // name: 'Home', + // meta: {}, + // children: [ + // { + // path: 'index', + // component: () => import('@/views/Home/Index.vue'), + // name: 'Index', + // meta: { + // title: t('router.home'), + // icon: 'ep:home-filled', + // noCache: false, + // affix: true + // } + // } + // ] + // }, { path: '/user', component: Layout, @@ -234,6 +234,26 @@ const remainingRouter: AppRouteRecordRaw[] = [ noTagsView: true } }, + { + path: '/empty', + component: Layout, + name: 'EmptyRoot', + meta: { + hidden: true, + noTagsView: true + }, + children: [ + { + path: '', + component: () => import('@/views/Empty/Index.vue'), + name: 'EmptyPage', + meta: { + hidden: true, + noTagsView: true + } + } + ] + }, { path: '/bpm', component: Layout, diff --git a/src/views/Empty/Index.vue b/src/views/Empty/Index.vue new file mode 100644 index 00000000..47c1ef85 --- /dev/null +++ b/src/views/Empty/Index.vue @@ -0,0 +1,12 @@ + + + + + diff --git a/src/views/iot/device/index.vue b/src/views/iot/device/index.vue index f962ee68..82348824 100644 --- a/src/views/iot/device/index.vue +++ b/src/views/iot/device/index.vue @@ -96,11 +96,11 @@ - +