From 602b8f394038581dc445a7a0689589214aef4483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=A8=E6=A0=88=E5=B0=8F=E5=AD=A6=E7=94=9F?= <1518079521@qq.com> Date: Thu, 1 Jun 2023 19:37:35 +0800 Subject: [PATCH] uni-app --- uni-app/.env.production | 4 +- uni-app/App.vue | 5 +- uni-app/locale/zh-Hans/common.json | 88 +++++++++---------- .../locale/zh-Hans/pages.index.develop.json | 10 +++ uni-app/main.js | 2 + uni-app/pages.json | 11 ++- uni-app/pages/index/develop.vue | 62 +++++++++++++ uni-app/stores/system.ts | 20 ++--- uni-app/utils/common.ts | 16 ++++ uni-app/utils/interceptor.ts | 35 ++++++-- uni-app/utils/request.ts | 15 ++-- uni-app/utils/storage.ts | 43 +++++++++ 12 files changed, 237 insertions(+), 74 deletions(-) create mode 100644 uni-app/locale/zh-Hans/pages.index.develop.json create mode 100644 uni-app/pages/index/develop.vue create mode 100644 uni-app/utils/storage.ts diff --git a/uni-app/.env.production b/uni-app/.env.production index f731b4093..448bf6118 100644 --- a/uni-app/.env.production +++ b/uni-app/.env.production @@ -1,4 +1,4 @@ -NODE_ENV = 'development' +NODE_ENV = 'production' # api请求地址 VITE_APP_BASE_URL='' @@ -7,7 +7,7 @@ VITE_APP_BASE_URL='' VITE_IMG_DOMAIN='' # 站点id 仅在编译为小程序时生效 -VITE_SITE_ID = 1 +VITE_SITE_ID = '' # 本地存储时token的参数名 VITE_REQUEST_STORAGE_TOKEN_KEY='wapToken' diff --git a/uni-app/App.vue b/uni-app/App.vue index 334d892e2..1370b5c80 100644 --- a/uni-app/App.vue +++ b/uni-app/App.vue @@ -2,7 +2,7 @@ import { onLaunch, onShow, onHide } from '@dcloudio/uni-app' import manifest from '@/manifest.json' import { redirectInterceptor, launchInterceptor } from '@/utils/interceptor' - import { getToken, isWeixinBrowser, urlDeconstruction } from '@/utils/common' + import { getToken, isWeixinBrowser,getSiteId } from '@/utils/common' import useMemberStore from '@/stores/member' import useConfigStore from '@/stores/config' import useSystemStore from '@/stores/system' @@ -22,6 +22,9 @@ // #ifdef H5 uni.getSystemInfoSync().platform == 'ios' && (uni.setStorageSync('initUrl', location.href)) // #endif + + // 缺少站点id,拦截 + if (getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) === '') return; const configStore = useConfigStore() configStore.getTabbarConfig() diff --git a/uni-app/locale/zh-Hans/common.json b/uni-app/locale/zh-Hans/common.json index d4f8fd5f6..0880b2203 100644 --- a/uni-app/locale/zh-Hans/common.json +++ b/uni-app/locale/zh-Hans/common.json @@ -1,49 +1,49 @@ { - "requestFail": "请求失败", - "notInDomainList": "不在request 合法域名列表中", - "currency": "¥", - "getSmsCode": "获取验证码", - "smsCodeChangeText": "秒后重新获取", - "captchaTitle": "请完成验证", - "confirm": "确认", - "cancel": "取消", - "save": "保存", - "delete": "删除", - "captchaPlaceholder": "请输入验证码", - "mobilePlaceholder": "请输入手机号码", - "mobileError": "请输入正确的手机号", - "codePlaceholder": "请输入手机验证码", - "memberCenter": "个人中心", - "userAgreement": "用户协议", - "privacyAgreement": "隐私协议", - "nickname": "昵称", - "nicknamePlaceholder": "请输入昵称", - "headimg": "头像", - "headimgPlaceholder": "请设置头像", - "getAvatarNickname": "获取您的昵称头像", - "getAvatarNicknameTips": "获取用户头像、昵称完善个人资料,主要用于向用户提供具有辨识度的用户中心界面", - "point": "积分", - "balance": "余额", - "login": "登录", - "register": "注册", - "complete": "完成", - "close": "关闭", - "pay": { - "orderInfo": "订单信息", - "confirmPay": "确认支付", - "payTitle": "确认付款", - "notHavePayType": "没有可用的支付方式", - "notObtainedInfo": "未获取到支付信息", - "paymentDocuments": "该支付单据", - "paySuccess": "支付成功", - "payFail": "支付失败", - "completePay": "已完成支付", - "incompletePay": "未完成支付", - "getting": "获取支付结果中" - }, + "requestFail": "请求失败", + "notInDomainList": "不在request 合法域名列表中", + "currency": "¥", + "getSmsCode": "获取验证码", + "smsCodeChangeText": "秒后重新获取", + "captchaTitle": "请完成验证", + "confirm": "确认", + "cancel": "取消", + "save": "保存", + "delete": "删除", + "captchaPlaceholder": "请输入验证码", + "mobilePlaceholder": "请输入手机号码", + "mobileError": "请输入正确的手机号", + "codePlaceholder": "请输入手机验证码", + "memberCenter": "个人中心", + "userAgreement": "用户协议", + "privacyAgreement": "隐私协议", + "nickname": "昵称", + "nicknamePlaceholder": "请输入昵称", + "headimg": "头像", + "headimgPlaceholder": "请设置头像", + "getAvatarNickname": "获取您的昵称头像", + "getAvatarNicknameTips": "获取用户头像、昵称完善个人资料,主要用于向用户提供具有辨识度的用户中心界面", + "point": "积分", + "balance": "余额", + "login": "登录", + "register": "注册", + "complete": "完成", + "close": "关闭", + "pay": { + "orderInfo": "订单信息", + "confirmPay": "确认支付", + "payTitle": "确认付款", + "notHavePayType": "没有可用的支付方式", + "notObtainedInfo": "未获取到支付信息", + "paymentDocuments": "该支付单据", + "paySuccess": "支付成功", + "payFail": "支付失败", + "completePay": "已完成支付", + "incompletePay": "未完成支付", + "getting": "获取支付结果中" + }, "myBalance": "我的余额", "myPoint": "我的积分", "customerService": "联系客服", - "siteClose": "站点已关闭", - "siteNonexistence": "站点不存在" + "siteClose": "站点已关闭", + "siteNonexistence": "站点不存在" } \ No newline at end of file diff --git a/uni-app/locale/zh-Hans/pages.index.develop.json b/uni-app/locale/zh-Hans/pages.index.develop.json new file mode 100644 index 000000000..9d4d9eed7 --- /dev/null +++ b/uni-app/locale/zh-Hans/pages.index.develop.json @@ -0,0 +1,10 @@ +{ + "developTitle":"开发环境配置", + "baseUrl":"API请求地址", + "imgUrl":"图片服务器地址", + "siteId":"站点ID", + "siteIdPlaceholder": "请输入站点ID", + "pleaseEnterNumber":"请输入数字", + "maximumCannotExceed":"最大不能超过" + +} \ No newline at end of file diff --git a/uni-app/main.js b/uni-app/main.js index 48e6f1f56..b38ff95f7 100644 --- a/uni-app/main.js +++ b/uni-app/main.js @@ -6,12 +6,14 @@ import locale from './locale' import uviewPlus from 'uview-plus' import '@/styles/index.scss' import 'virtual:windi.css' +import { uniStorage } from './utils/storage' export function createApp() { const app = createSSRApp(App) app.use(Pinia.createPinia()) app.use(locale) app.use(uviewPlus) + uniStorage() return { app, Pinia diff --git a/uni-app/pages.json b/uni-app/pages.json index 7c99b6bb2..6329f968d 100644 --- a/uni-app/pages.json +++ b/uni-app/pages.json @@ -259,7 +259,16 @@ // #endif "navigationBarTitleText": "%pages.webview.index%" } - }// {{PAGE}} + }, + { + "path": "pages/index/develop", + "style": { + // #ifdef H5 + "navigationStyle": "custom", + // #endif + "navigationBarTitleText": "%pages.index.develop%" + } + }// {{PAGE}} ], "globalStyle": { "navigationBarTextStyle": "black", diff --git a/uni-app/pages/index/develop.vue b/uni-app/pages/index/develop.vue new file mode 100644 index 000000000..f9654050e --- /dev/null +++ b/uni-app/pages/index/develop.vue @@ -0,0 +1,62 @@ + + + + + \ No newline at end of file diff --git a/uni-app/stores/system.ts b/uni-app/stores/system.ts index c3105f06e..d5ad8ebb3 100644 --- a/uni-app/stores/system.ts +++ b/uni-app/stores/system.ts @@ -3,7 +3,7 @@ import { getSiteInfo } from '@/api/system' import { redirect } from '@/utils/common' interface System { - site: AnyObject | null + site : AnyObject | null } const useSystemStore = defineStore('system', { @@ -14,15 +14,15 @@ const useSystemStore = defineStore('system', { }, actions: { async getSitenfo() { - await getSiteInfo() - .then((res: any) => { - this.site = res.data - if (this.site.status == 3) redirect({ url: '/pages/index/close', mode: 'reLaunch' }) - }) - .catch((err) => { - redirect({ url: '/pages/index/nonexistence', mode: 'reLaunch' }) - }) - } + await getSiteInfo() + .then((res : any) => { + this.site = res.data + if (this.site.status == 3) redirect({ url: '/pages/index/close', mode: 'reLaunch' }) + }) + .catch((err) => { + redirect({ url: '/pages/index/nonexistence', mode: 'reLaunch' }) + }) + } } }) diff --git a/uni-app/utils/common.ts b/uni-app/utils/common.ts index e9e68bca6..0ec62d37e 100644 --- a/uni-app/utils/common.ts +++ b/uni-app/utils/common.ts @@ -1,6 +1,7 @@ import { getTabbarPages } from './pages' import useDiyStore from '@/stores/diy' import useMemberStore from '@/stores/member' +import internal from 'stream' /** * 跳转页面 @@ -214,4 +215,19 @@ export function moneyFormat(money : string) : string { */ export function mobileConceal(mobile : string) : string { return mobile.substring(0, 3) + "****" + mobile.substr(mobile.length - 4); +} + +/** + * 获取站点id + */ +export function getSiteId(siteid : number) { + // #ifdef H5 + const match = location.href.match(/\/s(\d*)\//); + if (match) return match[1] + else return siteid + // #endif + + // #ifndef H5 + return siteid + // #endif } \ No newline at end of file diff --git a/uni-app/utils/interceptor.ts b/uni-app/utils/interceptor.ts index dbfa01e2d..e253db88c 100644 --- a/uni-app/utils/interceptor.ts +++ b/uni-app/utils/interceptor.ts @@ -1,6 +1,6 @@ import { language } from '@/locale' import { checkNeedLogin } from '@/utils/auth' -import { urlDeconstruction, getToken } from '@/utils/common' +import { redirect, urlDeconstruction, getToken, getSiteId } from '@/utils/common' import { memberLog } from '@/api/auth' /** @@ -17,6 +17,15 @@ export const redirectInterceptor = () => { // 加载语言包 language.loadLocaleMessages(route.path, uni.getLocale()) + // 开发模式下,如果未配置站点ID,则跳转到开发环境配置页面 + // #ifdef H5 + if (process.env.NODE_ENV == 'development') { + if ((getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) === '') && route.path != '/pages/index/develop') { + redirect({ url: '/pages/index/develop', mode: 'reLaunch' }) + } + } + // #endif + // 校验是否需要登录 checkNeedLogin(route) @@ -31,9 +40,25 @@ export const redirectInterceptor = () => { * 应用初始化拦截器 */ export const launchInterceptor = () => { - // 加载语言包 const launch = uni.getLaunchOptionsSync() launch.path = `/${launch.path}` + + // 开发模式下,如果未配置站点ID,则跳转到开发环境配置页面 + // #ifdef H5 + if (process.env.NODE_ENV == 'development') { + // 后台DIY装修页面时,获取站点ID + if (location.search.indexOf('?mode=decorate&site_id=') != -1) { + uni.setStorageSync('wap_site_id', location.search.replace('?mode=decorate&site_id=','')); + } + if (getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) === '') { + launch.path = '/pages/index/develop'; + uni.setStorageSync('develop_before_path', launch.path); + redirect({ url: '/pages/index/develop', mode: 'reLaunch' }) + } + } + // #endif + + // 加载语言包 language.loadLocaleMessages(launch.path, uni.getLocale()) // 校验是否需要登录 @@ -44,12 +69,6 @@ export const launchInterceptor = () => { uni.setStorageSync('pid', launch.query.mid) } - // #ifdef H5 - const match = location.href.match(/\/s(\d*)\//); - if (match) uni.setStorageSync('wap_site_id', match[1]) - else uni.removeStorageSync('wap_site_id') - // #endif - // 添加会员访问日志 if (getToken()) memberLog({ route: launch.path, params: JSON.stringify(launch.query || {}), pre_route: '' }) } \ No newline at end of file diff --git a/uni-app/utils/request.ts b/uni-app/utils/request.ts index 4fb55028e..36aee6c9e 100644 --- a/uni-app/utils/request.ts +++ b/uni-app/utils/request.ts @@ -1,6 +1,6 @@ -import { getToken, getAppChannel } from './common' import useMemberStore from '@/stores/member' import { t } from '@/locale' +import { getToken, getAppChannel, getSiteId } from './common' interface RequestConfig { showErrorMessage ?: boolean @@ -23,13 +23,12 @@ class Request { // #ifndef H5 this.baseUrl = import.meta.env.VITE_APP_BASE_URL // #endif - - // #ifdef MP - this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = import.meta.env.VITE_SITE_ID - // #endif - // #ifdef H5 - this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID - // #endif + + if (process.env.NODE_ENV == 'development') { + this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) + } else { + this.config.header[import.meta.env.VITE_REQUEST_HEADER_SITEID_KEY] = getSiteId(import.meta.env.VITE_SITE_ID) + } this.config.header[import.meta.env.VITE_REQUEST_HEADER_CHANNEL_KEY] = getAppChannel() } diff --git a/uni-app/utils/storage.ts b/uni-app/utils/storage.ts new file mode 100644 index 000000000..588378906 --- /dev/null +++ b/uni-app/utils/storage.ts @@ -0,0 +1,43 @@ +import { getSiteId } from './common' + +const handleKey = (key: string) : string => { + const siteid = getSiteId(0) + const storageKey = (siteid ? `site${siteid}.` : '') + key + return storageKey +} + +export function uniStorage(){ + const setStorageSync = uni.setStorageSync + const setStorage = uni.setStorage + const getStorage = uni.getStorage + const getStorageSync = uni.getStorageSync + const removeStorage = uni.removeStorage + const removeStorageSync = uni.removeStorageSync + + uni.setStorage = (options: UniNamespace.SetStorageOptions)=> { + options.key = handleKey(options.key) + setStorage(options) + } + + uni.setStorageSync = (key: string, data: any) => { + setStorageSync(handleKey(key), data) + } + + uni.getStorage = (options: UniNamespace.GetStorageOptions) => { + options.key = handleKey(options.key) + getStorage(options) + } + + uni.getStorageSync = (key: string) => { + return getStorageSync(handleKey(key)) + } + + uni.removeStorage = (options: UniNamespace.RemoveStorageOptions) => { + options.key = handleKey(options.key) + removeStorage(options) + } + + uni.removeStorageSync = (key: string) => { + return removeStorageSync(handleKey(key)) + } +} \ No newline at end of file