From 168a19fbca1ac02c16e699d942c25909a5bda6e1 Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Mon, 18 Aug 2025 21:24:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=99=BB=E5=BD=95):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=AD=96=E7=95=A5=E5=92=8C=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加登录相关配置文件和页面,包括登录策略配置、注册页面和路由拦截逻辑 移除旧的登录相关代码和配置,使用新的登录策略实现 在用户store中添加hasLogin计算属性用于检查登录状态 优化路由拦截器逻辑,支持白名单和黑名单策略 --- env/.env | 3 --- src/App.vue | 15 +++++------ src/login/README.md | 15 +++++++++++ src/login/config.ts | 13 +++++++++ src/pages.json | 16 +++++++++++ src/pages/login/register.vue | 19 ++++++++++++++ src/router/interceptor.ts | 51 ++++++++++++++---------------------- src/store/user.ts | 1 + src/utils/index.ts | 12 --------- 9 files changed, 91 insertions(+), 54 deletions(-) create mode 100644 src/login/README.md create mode 100644 src/login/config.ts create mode 100644 src/pages/login/register.vue diff --git a/env/.env b/env/.env index 8aa561c..61c2a09 100644 --- a/env/.env +++ b/env/.env @@ -8,9 +8,6 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b' # https://uniapp.dcloud.net.cn/collocation/manifest.html#h5-router VITE_APP_PUBLIC_BASE=/ -# 登录页面 -VITE_LOGIN_URL = '/pages/login/index' - # 后台请求地址 VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' # 后台上传地址 diff --git a/src/App.vue b/src/App.vue index b0d9043..275a071 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,21 +1,20 @@ + + + + diff --git a/src/router/interceptor.ts b/src/router/interceptor.ts index 14c5535..fe73004 100644 --- a/src/router/interceptor.ts +++ b/src/router/interceptor.ts @@ -6,54 +6,43 @@ */ import { useUserStore } from '@/store' import { tabbarStore } from '@/tabbar/store' -import { needLoginPages as _needLoginPages, getLastPage, getNeedLoginPages } from '@/utils' - -// TODO Check -const loginRoute = import.meta.env.VITE_LOGIN_URL - -function isLogined() { - const userStore = useUserStore() - return !!userStore.userInfo.username -} - -const isDev = import.meta.env.DEV +import { getLastPage } from '@/utils' +import { EXCLUDE_LIST, LOGIN_PAGE_LIST } from '../login/config' // 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录) export const navigateToInterceptor = { // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同 // 增加对相对路径的处理,BY 网友 @ideal invoke({ url }: { url: string }) { - // console.log(url) // /pages/route-interceptor/index?name=feige&age=30 + console.log(url) // /pages/route-interceptor/index?name=feige&age=30 let path = url.split('?')[0] // 处理相对路径 if (!path.startsWith('/')) { - const currentPath = getLastPage().route + 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) { - needLoginPages = getNeedLoginPages() - } - else { - needLoginPages = _needLoginPages - } - const isNeedLogin = needLoginPages.includes(path) - if (!isNeedLogin) { - return true - } - const hasLogin = isLogined() - if (hasLogin) { - return true + if (LOGIN_PAGE_LIST.includes(path)) { + console.log('000') + return } + tabbarStore.restorePrevIdx() - const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}` - uni.navigateTo({ url: redirectRoute }) - return false + + console.log('拦截器中得到的 path:', path) + const userStore = useUserStore() + + if (userStore.hasLogin || [...EXCLUDE_LIST, ...LOGIN_PAGE_LIST].includes(path)) { + console.log('111') + uni.navigateTo({ url: path }) + return + } + console.log('222') + const redirectUrl = `/login/login?redirect=${encodeURIComponent(path)}` + uni.navigateTo({ url: redirectUrl }) }, } diff --git a/src/store/user.ts b/src/store/user.ts index cec931a..2616a62 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -103,6 +103,7 @@ export const useUserStore = defineStore( getUserInfo, setUserAvatar, logout, + hasLogin: computed(() => !!userInfo.value.token), } }, { diff --git a/src/utils/index.ts b/src/utils/index.ts index 3aa8e52..00224ae 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -110,18 +110,6 @@ export function getCurrentPageI18nKey() { return currPage.style.navigationBarTitleText } -/** - * 得到所有的需要登录的 pages,包括主包和分包的 - * 只得到 path 数组 - */ -export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map(page => page.path) - -/** - * 得到所有的需要登录的 pages,包括主包和分包的 - * 只得到 path 数组 - */ -export const needLoginPages: string[] = getAllPages('needLogin').map(page => page.path) - /** * 根据微信小程序当前环境,判断应该获取的 baseUrl */