45 lines
1007 B
JavaScript
45 lines
1007 B
JavaScript
function request(url, method, data) {
|
|
let promise = new Promise((resolve, reject) => {
|
|
wx.showLoading({
|
|
title: '加载中'
|
|
})
|
|
wx.request({
|
|
url: getApp().globalData.host + url,
|
|
method: method,
|
|
data: data,
|
|
header:{
|
|
'Authorization': 'JWT ' + getApp().globalData.token
|
|
},
|
|
success: (res => {
|
|
wx.hideLoading();
|
|
if (res.data.code >= 200 && res.data.code < 400) {
|
|
resolve(res.data);
|
|
}else if(res.data.code == 401){
|
|
getApp().onLaunch()
|
|
}
|
|
else {
|
|
wx.showToast({
|
|
title: JSON.stringify(res.data.msg),
|
|
icon: 'none',
|
|
duration: 1000
|
|
})
|
|
}
|
|
|
|
}),
|
|
fail: (res => {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: '网络出错',
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
reject('网络出错');
|
|
})
|
|
})
|
|
})
|
|
return promise;
|
|
}
|
|
|
|
module.exports = {
|
|
request: request
|
|
} |