diff --git a/env/.env b/env/.env index e638295..05c8a58 100644 --- a/env/.env +++ b/env/.env @@ -7,6 +7,9 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b' # h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base VITE_APP_PUBLIC_BASE=/ +# 登录页面 +VITE_LOGIN_URL = '/pages/login/index' + VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' VITE_UPLOAD_BASEURL = 'https://ukw0y1.laf.run/upload' diff --git a/manifest.config.ts b/manifest.config.ts index 3ab58c5..2a8b454 100644 --- a/manifest.config.ts +++ b/manifest.config.ts @@ -113,6 +113,9 @@ export default defineManifestConfig({ appid: VITE_WX_APPID, setting: { urlCheck: false, + // 是否启用 ES6 转 ES5 + es6: true, + minified: true, }, usingComponents: true, // __usePrivacyCheck__: true, diff --git a/package.json b/package.json index d1b0001..1fe7d5e 100644 --- a/package.json +++ b/package.json @@ -73,10 +73,10 @@ "prepare": "git init && husky" }, "lint-staged": { - "**/*.{html,cjs,json,md,scss,css,txt}": [ + "**/*.{vue,html,cjs,json,md,scss,css,txt}": [ "prettier --write --cache" ], - "**/*.{vue,js,ts}": [ + "**/*.{js,ts}": [ "oxlint --fix", "prettier --write --cache" ], diff --git a/src/App.vue b/src/App.vue index 6f5aac7..302878f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,9 +1,61 @@ -import { onLaunch, onShow, onHide } from '@dcloudio/uni-app' import -'abortcontroller-polyfill/dist/abortcontroller-polyfill-only' import { useI18n } from 'vue-i18n' -import { tabBarList, getIsTabbar } from '@/utils/index' onLaunch(() => { console.log('App Launch') -// #ifdef MP-WEIXIN const setTabbarText = () => { if (!getIsTabbar()) { return } const { t } = -useI18n() const tabbarTexts = tabBarList.map((item) => item.text.replace(/(^%|%$)/g, '')) -tabbarTexts.forEach((tabbar: string, index: number) => { console.log('tabbar', tabbar) -uni.setTabBarItem({ index, text: t(tabbar), }) }) } // fix 微信小程序需要手动调用 api -设置一次国际化tabbar text。 setTabbarText() uni.onLocaleChange(setTabbarText) // #endif }) onShow(() -=> { console.log('App Show') }) onHide(() => { console.log('App Hide') }) + + + diff --git a/src/hooks/usePageAuth.ts b/src/hooks/usePageAuth.ts new file mode 100644 index 0000000..5ce7ff3 --- /dev/null +++ b/src/hooks/usePageAuth.ts @@ -0,0 +1,49 @@ +import { onLoad } from '@dcloudio/uni-app' +import { needLoginPages as _needLoginPages, getNeedLoginPages } from '@/utils' +import { useUserStore } from '@/store' + +const loginRoute = import.meta.env.VITE_LOGIN_URL +const isDev = import.meta.env.DEV +const 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/interceptors/route.ts b/src/interceptors/route.ts index 060c6f3..bed2af0 100644 --- a/src/interceptors/route.ts +++ b/src/interceptors/route.ts @@ -8,7 +8,7 @@ import { useUserStore } from '@/store' import { needLoginPages as _needLoginPages, getNeedLoginPages, getLastPage } from '@/utils' // TODO Check -const loginRoute = '/pages/login/index' +const loginRoute = import.meta.env.VITE_LOGIN_URL const isLogined = () => { const userStore = useUserStore()