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 @@ 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 页面