From 32765a789a8ac4562ca97c5bccb33c7ede5b5867 Mon Sep 17 00:00:00 2001 From: ideal Date: Fri, 25 Apr 2025 17:07:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=E5=9C=A8=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interceptors/route.ts | 15 ++++++++++++--- src/utils/index.ts | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/interceptors/route.ts b/src/interceptors/route.ts index ddd6c98..d550c97 100644 --- a/src/interceptors/route.ts +++ b/src/interceptors/route.ts @@ -5,7 +5,7 @@ * 我这里应为大部分都可以随便进入,所以使用黑名单 */ import { useUserStore } from '@/store' -import { needLoginPages as _needLoginPages, getNeedLoginPages } from '@/utils' +import { needLoginPages as _needLoginPages, getNeedLoginPages, getLastPage } from '@/utils' // TODO Check const loginRoute = '/pages/login/index' @@ -19,10 +19,19 @@ const isDev = import.meta.env.DEV // 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录) const navigateToInterceptor = { - // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同 + // 注意,这里的url是 '/' 开头的(也有可能是相对路径),如 '/pages/index/index',跟 'pages.json' 里面的 path 不同 invoke({ url }: { url: string }) { // console.log(url) // /pages/route-interceptor/index?name=feige&age=30 - const path = url.split('?')[0] + let path = url.split('?')[0] + + // 处理相对路径 + if (!path.startsWith('/')) { + const currentPath = getLastPage().route + const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}` + const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/')) + path = `${baseDir}/${path}` + } + let needLoginPages: string[] = [] // 为了防止开发时出现BUG,这里每次都获取一下。生产环境可以移到函数外,性能更好 if (isDev) { diff --git a/src/utils/index.ts b/src/utils/index.ts index a60e713..e358e1b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,7 @@ import { pages, subPackages, tabBar } from '@/pages.json' import { isMpWeixin } from './platform' -const getLastPage = () => { +export const getLastPage = () => { // getCurrentPages() 至少有1个元素,所以不再额外判断 // const lastPage = getCurrentPages().at(-1) // 上面那个在低版本安卓中打包会报错,所以改用下面这个【虽然我加了 src/interceptions/prototype.ts,但依然报错】