diff --git a/.env b/.env index 0c3c73f7..d67ad509 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://h5.example.com + +# 小程序 分享链接(小程序使用,多个链接用逗号分隔) +# SHOPRO_MINI_BASE_URL=https://mini-1.example.com,https://mini-2.example.com +SHOPRO_MINI_BASE_URL=https://mini.example.com \ No newline at end of file diff --git a/sheep/config/index.js b/sheep/config/index.js index 9aa4f523..4192b0b5 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',