From a96d9d834229ff351360b768f910d91c49dacda6 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Tue, 19 Aug 2025 17:07:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20=E4=BF=AE=E5=A4=8D=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8B=A6=E6=88=AA=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=B7=AF=E7=94=B1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构登录拦截器逻辑,区分需要登录和不需要登录的情况 优化路径处理逻辑,修复重定向URL的拼接问题 更新登录相关配置,添加isNeedLogin判断标志 关闭eslint的no-useless-return规则 --- eslint.config.mjs | 1 + src/login/config.ts | 4 +++- src/pages/login/login.vue | 12 +++++++---- src/router/interceptor.ts | 45 +++++++++++++++++++++++++++------------ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 7383bed..328c179 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -18,6 +18,7 @@ export default uniHelper({ 'src/service/app/**', ], rules: { + 'no-useless-return': 'off', 'no-console': 'off', 'no-unused-vars': 'off', 'vue/no-unused-refs': 'off', diff --git a/src/login/config.ts b/src/login/config.ts index ba724ac..f79870b 100644 --- a/src/login/config.ts +++ b/src/login/config.ts @@ -4,8 +4,10 @@ export const LOGIN_STRATEGY_MAP = { } // 登录策略,默认使用`无需登录策略`,即默认不需要登录就可以访问 export const LOGIN_STRATEGY = LOGIN_STRATEGY_MAP.DEFAULT_NO_NEED_LOGIN +export const isNeedLogin = LOGIN_STRATEGY === LOGIN_STRATEGY_MAP.DEFAULT_NEED_LOGIN -export const LOGIN_PAGE_LIST = ['/pages/login/login', '/pages/login/register'] +export const LOGIN_PAGE = '/pages/login/login' +export const LOGIN_PAGE_LIST = [LOGIN_PAGE, '/pages/login/register'] // 排除在外的列表,白名单策略指白名单列表,黑名单策略指黑名单列表 export const EXCLUDE_PAGE_LIST = [ diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue index bf16e7e..9facf7a 100644 --- a/src/pages/login/login.vue +++ b/src/pages/login/login.vue @@ -15,7 +15,7 @@ import { isPageTabbar } from '@/tabbar/store' const redirectUrl = ref('') onLoad((options) => { console.log('login options', options) - redirectUrl.value = options.redirectUrl || tabbarList[0].pagePath + redirectUrl.value = options.redirect || tabbarList[0].pagePath }) const userStore = useUserStore() function doLogin() { @@ -26,14 +26,18 @@ function doLogin() { token: 'fake-token', }) console.log(redirectUrl.value) - if (isPageTabbar(redirectUrl.value)) { + let path = redirectUrl.value + if (path.startsWith('/')) { + path = redirectUrl.value.substring(1) + } + if (isPageTabbar(path)) { uni.switchTab({ - url: `/${redirectUrl.value}`, + url: `/${path}`, }) } else { uni.redirectTo({ - url: `/${redirectUrl.value}`, + url: `/${path}`, }) } } diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts index 4dfb4e9..4be0908 100644 --- a/src/router/interceptor.ts +++ b/src/router/interceptor.ts @@ -1,5 +1,5 @@ /** - * by 菲鸽 on 2024-03-06 + * by 菲鸽 on 2025-08-19 * 路由拦截,通常也是登录拦截 * 可以设置路由白名单,或者黑名单,看业务需要选哪一个 * 我这里应为大部分都可以随便进入,所以使用黑名单 @@ -7,7 +7,7 @@ import { useUserStore } from '@/store' import { tabbarStore } from '@/tabbar/store' import { getLastPage } from '@/utils' -import { EXCLUDE_PAGE_LIST, LOGIN_PAGE_LIST } from '../login/config' +import { EXCLUDE_PAGE_LIST, isNeedLogin, LOGIN_PAGE, LOGIN_PAGE_LIST } from '../login/config' // 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录) export const navigateToInterceptor = { @@ -18,6 +18,10 @@ export const navigateToInterceptor = { if (url === undefined) { return } + console.log(getCurrentPages()) + if (getCurrentPages().length === 0) { + return + } let path = url.split('?')[0] // 处理相对路径 @@ -36,21 +40,34 @@ export const navigateToInterceptor = { return } + console.log('拦截器中得到的 path:', path) + const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(path)}` + const userStore = useUserStore() - if (userStore.hasLogin) { - return - } - console.log('拦截器中得到的 path:', path, userStore.hasLogin) - - if (EXCLUDE_PAGE_LIST.includes(path)) { - console.log('111') - uni.navigateTo({ url: path }) - return + // #region 1/2 需要登录的情况 --------------------------- + if (isNeedLogin) { + if (userStore.hasLogin) { + return + } + else { + if (EXCLUDE_PAGE_LIST.includes(path)) { + return + } + else { + uni.navigateTo({ url: redirectUrl }) + } + } } - console.log('222') - const redirectUrl = `/pages/login/login?redirect=${encodeURIComponent(path)}` - uni.navigateTo({ url: redirectUrl }) + // #endregion 1/2 需要登录的情况 --------------------------- + + // #region 2/2 不需要登录的情况 --------------------------- + else { + if (EXCLUDE_PAGE_LIST.includes(path)) { + uni.navigateTo({ url: redirectUrl }) + } + } + // #endregion 2/2 不需要登录的情况 --------------------------- }, }