fix: 路由拦截器在相对路径的情况下异常

This commit is contained in:
ideal 2025-04-25 17:07:12 +08:00 committed by feige996
parent 3737e6496b
commit 32765a789a
2 changed files with 13 additions and 4 deletions

View File

@ -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) {

View File

@ -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但依然报错】