From 0632985305d7ea2b78be33618c7c7fd69dbfbce7 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Mon, 1 Sep 2025 15:50:58 +0800 Subject: [PATCH] =?UTF-8?q?docs(router):=20=E6=9B=B4=E6=96=B0README?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新router模块的README文档,添加excludeLoginPath配置说明 优化judgeIsExcludePath函数,将isDev判断移到函数内部 --- src/router/README.md | 18 ++++++++++++++++++ src/router/interceptor.ts | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/router/README.md b/src/router/README.md index 3854a85..82dbdb3 100644 --- a/src/router/README.md +++ b/src/router/README.md @@ -22,6 +22,24 @@ 在 `默认需要登录策略: DEFAULT_NEED_LOGIN` 中,只有路由在 `EXCLUDE_LOGIN_PATH_LIST` 中,才不需要登录,相当于白名单。 +### excludeLoginPath +definePage 中可以通过 `excludeLoginPath` 来配置路由是否需要登录。(类似过去的 needLogin 的功能) + +```ts +definePage({ + style: { + navigationBarTitleText: '关于', + }, + // 登录授权(可选):跟以前的 needLogin 类似功能,但是同时支持黑白名单,详情请见 arc/router 文件夹 + excludeLoginPath: true, + // 角色授权(可选):如果需要根据角色授权,就配置这个 + roleAuth: { + field: 'role', + value: 'admin', + redirect: '/pages/auth/403', + }, +}) +``` ## 登录注册页路由 diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts index 249b084..4291fa5 100644 --- a/src/router/interceptor.ts +++ b/src/router/interceptor.ts @@ -8,16 +8,16 @@ import { isPageTabbar, tabbarStore } from '@/tabbar/store' import { getAllPages, getLastPage, parseUrlToObj } from '@/utils/index' import { EXCLUDE_LOGIN_PATH_LIST, HOME_PAGE, isNeedLoginMode, LOGIN_PAGE } from './config' -const isDev = import.meta.env.DEV export const FG_LOG_ENABLE = false export function judgeIsExcludePath(path: string) { + const isDev = import.meta.env.DEV if (!isDev) { return EXCLUDE_LOGIN_PATH_LIST.includes(path) } const allExcludeLoginPages = getAllPages('excludeLoginPath') // dev 环境下,需要每次都重新获取,否则新配置就不会生效 return EXCLUDE_LOGIN_PATH_LIST.includes(path) || (isDev && allExcludeLoginPages.some(page => page.path === path)) } -// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录) + export const navigateToInterceptor = { // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同 // 增加对相对路径的处理,BY 网友 @ideal