From 746d3c97c3c57e5750a571b6c7762a598d59d7ab Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 7 Mar 2025 18:05:31 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=9B=86=E6=88=90vben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/certd-client/src/router/guard.ts | 60 +++++++------------- 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/packages/ui/certd-client/src/router/guard.ts b/packages/ui/certd-client/src/router/guard.ts index 76b9f202..58a9320d 100644 --- a/packages/ui/certd-client/src/router/guard.ts +++ b/packages/ui/certd-client/src/router/guard.ts @@ -71,31 +71,11 @@ export function setupCommonGuard(router: Router) { */ function setupAccessGuard(router: Router) { router.beforeEach(async (to, from) => { - // 基本路由,这些路由不需要进入权限拦截 - const needAuth = to.matched.some((r) => { - return r.meta?.auth || r.meta?.permission; - }); - const accessStore = useAccessStore(); - if (needAuth) { - if (!accessStore.accessToken) { - // 没有访问权限,跳转登录页面 - if (to.fullPath !== LOGIN_PATH) { - return { - path: LOGIN_PATH, - // 如不需要,直接删除 query - query: to.fullPath === DEFAULT_HOME_PATH ? {} : { redirect: encodeURIComponent(to.fullPath) }, - // 携带当前跳转的页面,登录后重新跳转该页面 - replace: true - }; - } - return true; - } - } - // 是否已经生成过动态路由 if (!accessStore.isAccessChecked) { if (accessStore.accessToken) { + //如果已登录 const permissionStore = usePermissionStore(); await permissionStore.loadFromRemote(); const userStore = useUserStore(); @@ -109,25 +89,27 @@ function setupAccessGuard(router: Router) { accessStore.setIsAccessChecked(true); } - // 生成菜单和路由 - // const { accessibleMenus, accessibleRoutes } = await generateAccess({ - // roles: [], - // router, - // // 则会在菜单中显示,但是访问会被重定向到403 - // routes: accessRoutes - // }); - // - // // 保存菜单信息和路由信息 - // accessStore.setAccessMenus(accessibleMenus); - // accessStore.setAccessRoutes(accessibleRoutes); + // 基本路由,这些路由不需要进入权限拦截 + const needAuth = to.matched.some((r) => { + return r.meta?.auth || r.meta?.permission; + }); - // const redirectPath = (from.query.redirect ?? (to.path === DEFAULT_HOME_PATH ? DEFAULT_HOME_PATH : to.fullPath)) as string; - // - // return { - // ...router.resolve(decodeURIComponent(redirectPath)), - // replace: true - // }; - return true; + if (!needAuth) { + return true; + } + if (!accessStore.accessToken) { + // 没有访问权限,跳转登录页面 + if (to.fullPath !== LOGIN_PATH) { + return { + path: LOGIN_PATH, + // 如不需要,直接删除 query + query: to.fullPath === DEFAULT_HOME_PATH ? {} : { redirect: encodeURIComponent(to.fullPath) }, + // 携带当前跳转的页面,登录后重新跳转该页面 + replace: true + }; + } + return true; + } }); }