!181 feat: 增加体验版后端接口地址判断

Merge pull request !181 from 背起行囊/master
pull/177/MERGE
芋道源码 2026-05-31 02:31:24 +00:00 committed by Gitee
commit 2c489b2cab
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 41 additions and 2 deletions

5
.env
View File

@ -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

View File

@ -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配置文件是否存在');

21
sheep/helper/env.js Normal file
View File

@ -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;
}