From e8478c9d437e1e14201c3edc1fa1eaa40fb3ea72 Mon Sep 17 00:00:00 2001
From: feige996 <1020102647@qq.com>
Date: Mon, 18 Aug 2025 21:52:32 +0800
Subject: [PATCH] =?UTF-8?q?feat(=E7=99=BB=E5=BD=95):=20=E5=AE=9E=E7=8E=B0H?=
=?UTF-8?q?5=E6=A8=A1=E6=8B=9F=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E8=B7=AF=E7=94=B1=E6=8B=A6=E6=88=AA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在user store中添加setUserInfo方法
- 新增tabbar页面判断工具函数isPageTabbar
- 重构路由拦截逻辑,简化登录判断
- 实现H5模拟登录页面及跳转逻辑
- 移除不再使用的usePageAuth钩子
---
src/hooks/usePageAuth.ts | 50 ------------------------------------
src/pages/login/login.h5.vue | 33 +++++++++++++++++++++++-
src/router/interceptor.ts | 13 ++++++----
src/store/user.ts | 1 +
src/tabbar/store.ts | 4 +++
5 files changed, 45 insertions(+), 56 deletions(-)
delete mode 100644 src/hooks/usePageAuth.ts
diff --git a/src/hooks/usePageAuth.ts b/src/hooks/usePageAuth.ts
deleted file mode 100644
index fd006c8..0000000
--- a/src/hooks/usePageAuth.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import { onLoad } from '@dcloudio/uni-app'
-import { useUserStore } from '@/store'
-import { needLoginPages as _needLoginPages, getNeedLoginPages } from '@/utils'
-
-const loginRoute = import.meta.env.VITE_LOGIN_URL
-const isDev = import.meta.env.DEV
-function isLogined() {
- const userStore = useUserStore()
- return !!userStore.userInfo.username
-}
-// 检查当前页面是否需要登录
-export function usePageAuth() {
- onLoad((options) => {
- // 获取当前页面路径
- const pages = getCurrentPages()
- const currentPage = pages[pages.length - 1]
- const currentPath = `/${currentPage.route}`
-
- // 获取需要登录的页面列表
- let needLoginPages: string[] = []
- if (isDev) {
- needLoginPages = getNeedLoginPages()
- }
- else {
- needLoginPages = _needLoginPages
- }
-
- // 检查当前页面是否需要登录
- const isNeedLogin = needLoginPages.includes(currentPath)
- if (!isNeedLogin) {
- return
- }
-
- const hasLogin = isLogined()
- if (hasLogin) {
- return true
- }
-
- // 构建重定向URL
- const queryString = Object.entries(options || {})
- .map(([key, value]) => `${key}=${encodeURIComponent(String(value))}`)
- .join('&')
-
- const currentFullPath = queryString ? `${currentPath}?${queryString}` : currentPath
- const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(currentFullPath)}`
-
- // 重定向到登录页
- uni.redirectTo({ url: redirectRoute })
- })
-}
diff --git a/src/pages/login/login.h5.vue b/src/pages/login/login.h5.vue
index 9966cdf..83d5c1d 100644
--- a/src/pages/login/login.h5.vue
+++ b/src/pages/login/login.h5.vue
@@ -8,7 +8,35 @@
@@ -17,6 +45,9 @@
+
diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts
index 87dd783..3291fde 100644
--- a/src/router/interceptor.ts
+++ b/src/router/interceptor.ts
@@ -5,7 +5,6 @@
* 我这里应为大部分都可以随便进入,所以使用黑名单
*/
import { useUserStore } from '@/store'
-import { tabbarStore } from '@/tabbar/store'
import { getLastPage } from '@/utils'
import { EXCLUDE_LIST, LOGIN_PAGE_LIST } from '../login/config'
@@ -30,12 +29,16 @@ export const navigateToInterceptor = {
return
}
- tabbarStore.restorePrevIdx()
-
- console.log('拦截器中得到的 path:', path)
const userStore = useUserStore()
+ if (userStore.hasLogin) {
+ return
+ }
- if (userStore.hasLogin || [...EXCLUDE_LIST, ...LOGIN_PAGE_LIST].includes(path)) {
+ // tabbarStore.restorePrevIdx()
+
+ console.log('拦截器中得到的 path:', path, userStore.hasLogin)
+
+ if ([...EXCLUDE_LIST, ...LOGIN_PAGE_LIST].includes(path)) {
console.log('111')
uni.navigateTo({ url: path })
return
diff --git a/src/store/user.ts b/src/store/user.ts
index 2616a62..16a9d8b 100644
--- a/src/store/user.ts
+++ b/src/store/user.ts
@@ -100,6 +100,7 @@ export const useUserStore = defineStore(
userInfo,
login,
wxLogin,
+ setUserInfo,
getUserInfo,
setUserAvatar,
logout,
diff --git a/src/tabbar/store.ts b/src/tabbar/store.ts
index 2feeea7..7225118 100644
--- a/src/tabbar/store.ts
+++ b/src/tabbar/store.ts
@@ -16,6 +16,10 @@ if (BULGE_ENABLE) {
export { tabbarList }
+export function isPageTabbar(path: string) {
+ return tabbarList.some(item => item.pagePath === path)
+}
+
/**
* 自定义 tabbar 的状态管理,原生 tabbar 无需关注本文件
* tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面