From f41ea5aeb429beb17e388a3a460722252ebaffdc Mon Sep 17 00:00:00 2001 From: lifetime have you <121815889@qq.com> Date: Sat, 25 Jan 2025 21:14:36 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=B0=86h5=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E4=BA=A4=E7=94=B1.env=E9=85=8D=E7=BD=AE=202?= =?UTF-8?q?=E3=80=81=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=86=85=E5=A4=8D=E5=88=B6=E5=88=86=E4=BA=AB=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E7=8B=AC=E7=AB=8B=E9=85=8D=E7=BD=AE=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=203=E3=80=81=E6=94=AF=E6=8C=81=E5=88=86=E4=BA=AB=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E5=A4=9A=E5=9F=9F=E5=90=8D=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E5=9F=9F=E5=90=8D=E9=9A=8F=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 8 ++++++++ sheep/config/index.js | 40 ++++++++++++++++++++++++++++++++++++++++ sheep/store/app.js | 3 ++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 0c3c73f7..f24fc458 100644 --- a/.env +++ b/.env @@ -29,3 +29,11 @@ SHOPRO_MPLIVE_ON=0 # 租户ID 默认 1 SHOPRO_TENANT_ID=1 + +# H5 分享链接(普通浏览器使用,多个链接用逗号分隔) +# SHOPRO_H5SHARE_URL=https://h5-1.example.com,https://h5-2.example.com +SHOPRO_H5SHARE_URL=https://java.qiye.ekcc.vip + +# 小程序 分享链接(多个链接用逗号分隔) +# SHOPRO_MINI_BASE_URL=https://mini-1.example.com,https://mini-2.example.com +SHOPRO_MINI_BASE_URL=https://java.qiye.ekcc.vip \ No newline at end of file diff --git a/sheep/config/index.js b/sheep/config/index.js index 9aa4f523..91cf8405 100644 --- a/sheep/config/index.js +++ b/sheep/config/index.js @@ -14,10 +14,50 @@ export const staticUrl = import.meta.env.SHOPRO_STATIC_URL; export const tenantId = import.meta.env.SHOPRO_TENANT_ID; export const websocketPath = import.meta.env.SHOPRO_WEBSOCKET_PATH; +// 判断是否在小程序环境中 +const isMiniProgram = typeof wx !== 'undefined' && typeof wx.getSystemInfoSync === 'function' && typeof window === 'undefined'; + +// 解析逗号分隔的域名 +const parseCommaSeparatedUrls = (urls) => { + if (!urls) { + console.warn('未配置分享链接'); + return null; + } + const urlList = urls + .split(',') // 按逗号分割 + .map(url => url.trim()) // 去除空白 + .filter(url => url); // 过滤空字符串 + if (urlList.length === 0) { + console.warn('分享链接配置为空或无效'); + return null; + } + return urlList[Math.floor(Math.random() * urlList.length)]; // 随机选择一个域名 +}; + +// 获取 H5 分享链接 +// 普通浏览器:使用 SHOPRO_H5SHARE_URL +// 小程序环境:使用 SHOPRO_MINI_BASE_URL +export const h5shareUrl = isMiniProgram + ? parseCommaSeparatedUrls(import.meta.env.SHOPRO_MINI_BASE_URL) + : parseCommaSeparatedUrls(import.meta.env.SHOPRO_H5SHARE_URL); + +// 确保 h5shareUrl 是完整的 URL +if (h5shareUrl && !h5shareUrl.startsWith('http')) { + console.warn('h5shareUrl 必须是完整的 URL,不能是路径'); + h5shareUrl = null; // 如果配置错误,设置为 null +} + +// 调试信息 +console.log('wx:', wx); +console.log('wx.getSystemInfoSync:', wx && wx.getSystemInfoSync); +console.log('isMiniProgram:', isMiniProgram); +console.log('h5shareUrl:', h5shareUrl); + export default { baseUrl, apiPath, staticUrl, tenantId, websocketPath, + h5shareUrl, }; diff --git a/sheep/store/app.js b/sheep/store/app.js index 36f767f1..1f6bece8 100644 --- a/sheep/store/app.js +++ b/sheep/store/app.js @@ -4,6 +4,7 @@ import $platform from '@/sheep/platform'; import $router from '@/sheep/router'; import user from './user'; import sys from './sys'; +import { h5shareUrl } from '@/sheep/config'; const app = defineStore({ id: 'app', @@ -72,7 +73,7 @@ const app = defineStore({ this.platform = { share: { methods: ['forward', 'poster', 'link'], - linkAddress: 'http://127.0.0.1:3000', // TODO 芋艿:可以考虑改到 .env 那 + linkAddress: h5shareUrl, // 已交由.env接管 posterInfo: { user_bg: '/static/img/shop/config/user-poster-bg.png', goods_bg: '/static/img/shop/config/goods-poster-bg.png',