From a539112a0f53183ee073d4eb9034ef48209fe30c Mon Sep 17 00:00:00 2001 From: Soybean Date: Wed, 21 Sep 2022 12:45:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(projects):=20support=20constant=20route=20?= =?UTF-8?q?without=20login=20status[=E6=94=AF=E6=8C=81=E6=9C=AA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=8A=B6=E6=80=81=E4=B8=8B=E8=AE=BF=E9=97=AE=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E5=9B=BA=E5=AE=9A=E8=B7=AF=E7=94=B1?= =?UTF-8?q?]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/guard/dynamic.ts | 3 ++- src/store/modules/route/index.ts | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/router/guard/dynamic.ts b/src/router/guard/dynamic.ts index 6d9c2c5d3..965c4914c 100644 --- a/src/router/guard/dynamic.ts +++ b/src/router/guard/dynamic.ts @@ -18,7 +18,8 @@ export async function createDynamicRouteGuard( if (!route.isInitAuthRoute) { // 未登录情况下直接回到登录页,登录成功后再加载权限路由 if (!isLogin) { - if (to.name === routeName('login')) { + const toName = to.name as AuthRoute.RouteKey; + if (route.isValidConstantRoute(toName) && !to.meta.requiresAuth) { next(); } else { const redirect = to.fullPath; diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 85c4a460b..33c3a521e 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -53,14 +53,30 @@ export const useRouteStore = defineStore('route-store', { /** 重置路由数据,保留固定路由 */ resetRoutes() { const routes = router.getRoutes(); - const constantRouteNames = getConstantRouteNames(constantRoutes); routes.forEach(route => { const name: AuthRoute.RouteKey = (route.name || 'root') as AuthRoute.RouteKey; - if (!constantRouteNames.includes(name)) { + if (!this.isConstantRoute(name)) { router.removeRoute(name); } }); }, + /** + * 是否是固定路由 + * @param name 路由名称 + */ + isConstantRoute(name: AuthRoute.RouteKey) { + const constantRouteNames = getConstantRouteNames(constantRoutes); + return constantRouteNames.includes(name); + }, + /** + * 是否是有效的固定路由 + * @param name 路由名称 + */ + isValidConstantRoute(name: AuthRoute.RouteKey) { + const NOT_FOUND_PAGE_NAME: AuthRoute.RouteKey = 'not-found-page'; + const constantRouteNames = getConstantRouteNames(constantRoutes); + return constantRouteNames.includes(name) && name !== NOT_FOUND_PAGE_NAME; + }, /** * 处理权限路由 * @param routes - 权限路由