From f6d2a643e831cf6e1281cc89608eb3d8a870deef Mon Sep 17 00:00:00 2001 From: rjl <98n9@163.com> Date: Tue, 27 Jan 2026 10:01:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(router):=20=E5=A2=9E=E5=8A=A0=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E7=99=BB=E5=BD=95=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 VITE_AUTH_ROUTE_FORCE_LOGIN 环境变量以控制是否强制用户登录 当设置为 Y 时,未登录用户访问非白名单路由将被重定向到登录页 同时更新开发环境配置并添加相应的 TypeScript 类型定义 --- apps/admin/.env | 3 ++ apps/admin/.env.dev | 4 +-- apps/admin/src/router/guard/route.ts | 54 ++++++++++++++++------------ apps/admin/src/typings/vite-env.d.ts | 2 ++ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/apps/admin/.env b/apps/admin/.env index 4790452..1dc05b6 100644 --- a/apps/admin/.env +++ b/apps/admin/.env @@ -24,6 +24,9 @@ VITE_ICON_LOCAL_PREFIX=icon-local # 认证路由模式: static (静态) | dynamic (动态) VITE_AUTH_ROUTE_MODE=static +# 是否强制登录: Y (开启) | N (关闭) +VITE_AUTH_ROUTE_FORCE_LOGIN=N + # 静态认证路由的主页 VITE_ROUTE_HOME=home diff --git a/apps/admin/.env.dev b/apps/admin/.env.dev index e546636..4c4d257 100644 --- a/apps/admin/.env.dev +++ b/apps/admin/.env.dev @@ -3,10 +3,10 @@ # VITE_SERVICE_BASE_URL=http://192.168.5.140:9999 -VITE_SERVICE_BASE_URL=https://api.qyzhjy.com +# VITE_SERVICE_BASE_URL=https://api.qyzhjy.com +VITE_SERVICE_BASE_URL=http://172.16.10.130:5000 -# other backend service base url, test environment VITE_OTHER_SERVICE_BASE_URL= `{ "demo": "http://localhost:9528" }` diff --git a/apps/admin/src/router/guard/route.ts b/apps/admin/src/router/guard/route.ts index bc33dd7..a5f0512 100644 --- a/apps/admin/src/router/guard/route.ts +++ b/apps/admin/src/router/guard/route.ts @@ -12,9 +12,9 @@ import { useRouteStore } from '@/store/modules/route' import { localStg } from '@/utils/storage' /** - * create route guard + * 创建路由守卫 * - * @param router router instance + * @param router 路由实例 */ export function createRouteGuard(router: Router) { router.beforeEach(async (to, from, next) => { @@ -38,39 +38,43 @@ export function createRouteGuard(router: Router) { const hasRole = authStore.userInfo.roles.some(role => routeRoles.includes(role)) const hasAuth = authStore.isStaticSuper || !routeRoles.length || hasRole - // if it is login route when logged in, then switch to the root page + // 如果已登录且访问的是登录页,则跳转到根页面 if (to.name === loginRoute && isLogin) { next({ name: rootRoute }) return } - // if the route does not need login, then it is allowed to access directly + // 如果路由不需要登录,则直接允许访问 if (!needLogin) { handleRouteSwitch(to, from, next) return } - // the route need login but the user is not logged in, then switch to the login page - if (!isLogin) { + // 如果路由需要登录但用户未登录,则跳转到登录页 + const isAuthRouteForceLogin = import.meta.env.VITE_AUTH_ROUTE_FORCE_LOGIN === 'Y' + const whiteList: string[] = ['user'] + const isInWhiteList = whiteList.includes(to.name as string) + + if (isAuthRouteForceLogin && !isLogin && !isInWhiteList) { next({ name: loginRoute, query: { redirect: to.fullPath } }) return } - // if the user is logged in but does not have authorization, then switch to the 403 page + // 如果用户已登录但没有权限,则跳转到 403 页面 if (!hasAuth) { next({ name: noAuthorizationRoute }) return } - // switch route normally + // 正常跳转路由 handleRouteSwitch(to, from, next) }) } /** - * initialize route + * 初始化路由 * - * @param to to route + * @param to 目标路由 */ async function initRoute(to: RouteLocationNormalized): Promise { const routeStore = useRouteStore() @@ -78,12 +82,12 @@ async function initRoute(to: RouteLocationNormalized): Promise