config.js
let a = wx.getAccountInfoSync()
let env = a.miniProgram.envVersion
const config = {
dev_url:'http://10.10.1.107:88/mini/', //测试环境地址
release_url:'https://qaqn.xingo.cn/mini/',//生产和体验版使用
env,
}
export { config }
request.js
// 使用proxy转换为异步化的wx.request方法
import {
config
} from '../config.js';
const wxHttp = (params) => {
return new Promise((resolve, reason) => {
console.log(config)
let url = ''
if (config.env === "develop") {
url = config.dev_url + params.url
} else {
url = config.release_url + params.url
}
wx.showLoading({
title: '加载中',
mask: true
})
wx.request({
url,
data: params.data,
method: params.method || "GET",
header: {
'content-type': 'application/json',
'token': wx.getStorageSync('token')
},
success: function (res) {
let {
status,
msg,
code,
message
} = res.data
wx.hideLoading()
if (code) {
if(code === -5){
wx.showToast({
title: message,
icon: "none"
})
setTimeout(() => {
wx.switchTab({
url: '/pages/personal/personal',
})
}, 1000)
return false
}
if(code === -1){
wx.showToast({
title: message,
icon: 'none'
})
reason(res)
}
}
if (status === "success") {
resolve(res.data)
} else {
wx.showToast({
title: msg,
icon: 'none'
})
reason(res)
}
},
fail: function (err) {
wx.hideLoading()
reason(err)
}
});
})
}
export default wxHttp