微信小程序——区分生产和开发环境

在封装网络请求时,对于不同的环境要调用接口的baseUrl是不相同的,在小程序中,我是这样设置的:

// request.js 

const envVersion = __wxConfig.envVersion;
let baseUrl;
if(envVersion === 'develop'){
  baseUrl = 'https://xxx.xxx.cn/;
}else{
  baseUrl = 'https://xxx.xxx..com/';
}

我们可以在这里根据当前的环境进行相应接口的调用。
开发版:envVersion: “develop”
体验版:envVersion:“trial”
正式版:envVersion: “release”

同时,我们也可以

console.log(__wxConfig)

打印出结果为:

{
accountInfo:
	appId: "xxxxxxxxxxxxx"
	icon: "http://wx.qlogo.cn/mmhead/Q3auHgzwzM52opext62OxLevNrx3DsDxXcyZicicNNfDGVzz5gcqLfNQ/0"
	nickname: "XXX"
	__proto__: Object
appLaunchInfo:
	path: "pages/index/index"
	query: {}
	referrerInfo: {}
	scene: 1001
	__public: {path: "pages/index/index", query: {…}, scene: 1001, shareTicket: undefined, referrerInfo: {…}}
	__proto__: Object
appType: 0
brand: "devtools"
cloud: false
customClose: false
debug: false
entryPagePath: "pages/index/index.html"
env: {USER_DATA_PATH: "http://usr"}
envVersion: "develop"
ext: {}
extAppid: ""
global: {window: {…}}
language: "zh"
mainPlugins: {}
model: "iPhone 6/7/8"
networkTimeout: {request: 60000, uploadFile: 60000, connectSocket: 60000, downloadFile: 60000}
onReady: ƒ (t)
page: {pages/index/index.html: {…}, pages/auth/auth.html: {…}, pages/logs/logs.html: {…}}
pages: (3) ["pages/index/index", "pages/auth/auth", "pages/logs/logs"]
permission: {scope.userLocation: {…}}
pixelRatio: 2
platform: "devtools"
resizable: false
screenHeight: 667
screenWidth: 375
tabBar: {list: Array(0)}
urlCheck: true
useExtendedLib: {}
version: "7.0.4"
widgets: []
workers: ""
wxAppInfo: {maxRequestConcurrent: 10, maxUploadConcurrent: 10, maxDownloadConcurrent: 10, maxWorkerConcurrent: 1}
__readyHandler: ƒ ()
__warning__: ""
}

留存记录,希望也可以给你带来提示,加油,又是晴朗的一天~

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值