From e7c40e46ec838d1a0cee3c5dac2e82c35e4cf3ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E7=BB=B4?= <908280968@qq.com>
Date: Mon, 29 Sep 2025 16:44:28 +0800
Subject: [PATCH] =?UTF-8?q?feat(router):=20=E6=B7=BB=E5=8A=A0404=E9=A1=B5?=
=?UTF-8?q?=E9=9D=A2=E5=B9=B6=E5=A4=84=E7=90=86=E4=B8=8D=E5=AD=98=E5=9C=A8?=
=?UTF-8?q?=E7=9A=84=E8=B7=AF=E7=94=B1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
在路由配置中添加404页面常量,并创建对应的404页面组件。修改路由拦截器,当访问不存在的路由时自动跳转到404页面。同时优化路由拦截器的返回逻辑,明确区分阻止和允许路由继续执行的情况。
---
src/pages/404/index.vue | 30 ++++++++++++++++++++++++++++++
src/router/config.ts | 1 +
src/router/interceptor.ts | 11 +++++++++--
3 files changed, 40 insertions(+), 2 deletions(-)
create mode 100644 src/pages/404/index.vue
diff --git a/src/pages/404/index.vue b/src/pages/404/index.vue
new file mode 100644
index 0000000..533e259
--- /dev/null
+++ b/src/pages/404/index.vue
@@ -0,0 +1,30 @@
+
+
+
+
+ 404
+ 页面不存在
+
+
+
+
+
diff --git a/src/router/config.ts b/src/router/config.ts
index c5ccf1d..fcb7ea8 100644
--- a/src/router/config.ts
+++ b/src/router/config.ts
@@ -10,6 +10,7 @@ export const isNeedLoginMode = LOGIN_STRATEGY === LOGIN_STRATEGY_MAP.DEFAULT_NEE
export const LOGIN_PAGE = '/pages/login/login'
export const REGISTER_PAGE = '/pages/login/register'
+export const NOT_FOUND_PAGE = '/pages/404/index'
export const LOGIN_PAGE_LIST = [LOGIN_PAGE, REGISTER_PAGE]
diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts
index d3cd88d..2670d14 100644
--- a/src/router/interceptor.ts
+++ b/src/router/interceptor.ts
@@ -7,7 +7,7 @@ import { isMp } from '@uni-helper/uni-env'
import { useTokenStore } from '@/store/token'
import { isPageTabbar, tabbarStore } from '@/tabbar/store'
import { getAllPages, getLastPage, HOME_PAGE, parseUrlToObj } from '@/utils/index'
-import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP } from './config'
+import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP, NOT_FOUND_PAGE } from './config'
export const FG_LOG_ENABLE = false
export function judgeIsExcludePath(path: string) {
@@ -43,6 +43,13 @@ export const navigateToInterceptor = {
path = `${baseDir}/${path}`
}
+ // 处理路由不存在的情况
+ if (getAllPages().every(page => page.path !== path)) {
+ console.warn('路由不存在:', path)
+ uni.navigateTo({ url: NOT_FOUND_PAGE })
+ return false // 明确表示阻止原路由继续执行
+ }
+
// 处理直接进入路由非首页时,tabbarIndex 不正确的问题
tabbarStore.setAutoCurIdx(path)
@@ -104,9 +111,9 @@ export const navigateToInterceptor = {
uni.navigateTo({ url: redirectUrl })
return false // 修改为false,阻止原路由继续执行
}
+ return true // 明确表示允许路由继续执行
}
// #endregion 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
- return true // 明确表示允许路由继续执行
},
}