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 */