这个问题我找了好久没有找到解决方法,然后自己想了一个办法
1.首页需要你在远程服务器添加一个JSON文件,里面只添加了debug判断是线上还是测试环境
{ "debug":true}
2.在app.js中添加globalData添加全局isDebug,host
globalData: { host: \'XXX\', isDebug:false, }
3.然后index.js中添加如下代码,index.js是我服装的所有接口文件,promise解决异步问题
index.js中代码
let DEBUG = false; let baseUrl = \'\' var promise =new Promise((resolve, reject) => { wx.request({ url: \'https://www.zhiyunyi.net//host.json\',//json数据地址 headers: { \'Content-Type\': \'application/json\' }, success: function (res) { console.log(res) let obj=res.data; getApp().globalData.isDebug=obj.debug if(obj.debug){ getApp().globalData.host=\'https://courtdev.zhiyunyi.net\'//这是测试环境的host resolve(getApp().globalData.host); // 这里是关键 }else{ getApp().globalData.host=\'https://court.zhiyunyi.net\'//这是线上环境的host resolve(getApp().globalData.host); // 这里是关键 } } }) }) promise.then(res=>{ baseUrl=res })