diff --git a/kinit-admin/src/permission.ts b/kinit-admin/src/permission.ts index 18a747a..b14a408 100644 --- a/kinit-admin/src/permission.ts +++ b/kinit-admin/src/permission.ts @@ -1,3 +1,4 @@ +import { hasRoute } from './router' import router from './router' import type { RouteRecordRaw } from 'vue-router' import { useTitle } from '@/hooks/web/useTitle' @@ -18,6 +19,7 @@ router.beforeEach(async (to, from, next) => { loadStart() const permissionStore = usePermissionStoreWithOut() const authStore = useAuthStoreWithOut() + if (authStore.getToken) { if (to.path === '/login') { next({ path: '/' }) @@ -28,6 +30,9 @@ router.beforeEach(async (to, from, next) => { await authStore.setUserInfo() } if (permissionStore.getIsAddRouters) { + if (!hasRoute(to.path)) { + authStore.logout('认证已过期,请重新登录!') + } next() return } diff --git a/kinit-admin/src/router/index.ts b/kinit-admin/src/router/index.ts index b8572be..b84b124 100644 --- a/kinit-admin/src/router/index.ts +++ b/kinit-admin/src/router/index.ts @@ -124,6 +124,12 @@ export const resetRouter = (): void => { }) } +// 判断是否已经有某个路径的路由配置 +export const hasRoute = (path: string): boolean => { + const resolvedRoute = router.resolve(path) + return resolvedRoute.matched.length > 0 +} + export const setupRouter = (app: App) => { app.use(router) }