首页 > 微信 > 详细

微信小程序怎么区分线上和测试环境

时间:2020-10-29 12:46:36      阅读:157      评论:0      收藏:0      [点我收藏+]

这个问题我找了好久没有找到解决方法,然后自己想了一个办法

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
})

 

微信小程序怎么区分线上和测试环境

原文:https://www.cnblogs.com/binmengxue/p/13895998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!