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