隐藏

微信小程序区分开发版、体验版和正式版

发布:2023/7/7 13:50:50作者:管理员 来源:本站 浏览次数:419

核心

__wxConfig.envVersion


代码

let version = __wxConfig.envVersion;


switch(version){

   case: 'develop';

   this.globalData.url = '开发域名'

   break

   case: 'trial';

   this.globalData.url = '体验域名'

   break

   case:'release'

   this.globalData.url = '生产域名'

   break

   default:

   this.globalData.url = '默认域名'

}

动态切换小程序版本

一个小程序只能有一个体验版本,这时,我们可以做一个切换域名的功能,方便测不同的版本,配合上面的环境检测,就可以做到,体验版可以切换环境,生产版本不能切换。




<!--读取-->

const ApiURLPrefix = {

 dev: {

   urlPrefix: 'https://dev.cc/gateway/blood-pressure/',

   commonURLPrefix: 'https://dev.cc/gateway',

 },

 test: {

   urlPrefix: 'https://test-api.cc/blood-pressure/',)

   commonURLPrefix: 'https://test-api.cc',  

 },

}



const config = {

 get commonURLPrefix() {

   const envType = Taro.getStorageSync('env_type') || 'test';

   return ApiURLPrefix[envType]?.commonURLPrefix;

 },

 get urlPrefix() {

   const envType = Taro.getStorageSync('env_type') || 'test';

   return ApiURLPrefix[envType]?.urlPrefix;

 },

}


<!--设置-->

const handleButtonClick = () => {

   Taro.setStorageSync('env_type', envValue);

   onConfirm();

   Taro.showToast({ title: '请重启小程序', icon: 'none', duration: 2000 });

};