diff --git a/.env b/.env index 03dd89b7..b19e0dff 100644 --- a/.env +++ b/.env @@ -1,9 +1,12 @@ # 版本号 SHOPRO_VERSION=v2.4.1 -# 后端接口 - 正式环境(通过 process.env.NODE_ENV 非 development) +# 后端接口 - 正式环境(通过 process.env.NODE_ENV 非 development -生产环境-release) SHOPRO_BASE_URL=http://api-dashboard.yudao.iocoder.cn +# 后端接口 - 体验环境(通过 process.env.NODE_ENV 非 development -体验环境-trial) +SHOPRO_TRIAL_BASE_URL=http://api-dashboard.yudao.iocoder.cn/trial + # 后端接口 - 测试环境(通过 process.env.NODE_ENV = development) SHOPRO_DEV_BASE_URL=http://127.0.0.1:48080 ### SHOPRO_DEV_BASE_URL=http://10.171.1.188:48080 diff --git a/sheep/config/index.js b/sheep/config/index.js index 803558c7..b00e4635 100644 --- a/sheep/config/index.js +++ b/sheep/config/index.js @@ -1,4 +1,5 @@ import packageInfo from '@/package.json'; +import { getWxEnvVersion } from '@/sheep/helper/env'; const { version } = packageInfo; @@ -7,7 +8,21 @@ export let baseUrl; if (process.env.NODE_ENV === 'development') { baseUrl = import.meta.env.SHOPRO_DEV_BASE_URL; } else { - baseUrl = import.meta.env.SHOPRO_BASE_URL; + // 非本地 dev 模式开发环境,判断是体验版还是正式版 + const wxEnvVersion = getWxEnvVersion(); + + if (wxEnvVersion === 'trial') { + // 体验版使用体验版服务器地址 + baseUrl = import.meta.env.SHOPRO_TRIAL_BASE_URL || import.meta.env.SHOPRO_BASE_URL; + console.log('当前运行环境:体验版'); + } else if (wxEnvVersion === 'release') { + // 正式版使用生产服务器地址 + baseUrl = import.meta.env.SHOPRO_BASE_URL; + console.log('当前运行环境:正式版'); + } else { + // 其他平台或获取失败,使用默认地址 + baseUrl = import.meta.env.SHOPRO_BASE_URL; + } } if (typeof baseUrl === 'undefined') { console.error('请检查.env配置文件是否存在'); diff --git a/sheep/helper/env.js b/sheep/helper/env.js new file mode 100644 index 00000000..6e53918a --- /dev/null +++ b/sheep/helper/env.js @@ -0,0 +1,21 @@ +/** + * 微信小程序运行环境:develop / trial / release + * 其它平台默认 release,整个 App 生命周期内不会变,缓存一次 + */ +let cachedEnvVersion; + +export function getWxEnvVersion() { + if (cachedEnvVersion) return cachedEnvVersion; + // #ifdef MP-WEIXIN + try { + cachedEnvVersion = wx.getAccountInfoSync().miniProgram.envVersion; + } catch (e) { + console.log('获取微信小程序环境失败', e); + cachedEnvVersion = 'release'; + } + // #endif + // #ifndef MP-WEIXIN + cachedEnvVersion = 'release'; + // #endif + return cachedEnvVersion; +}