67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
|
|
function request(url, method, data, type="application/json", re=0) {
|
|
let promise = new Promise((resolve, reject) => {
|
|
wx.showNavigationBarLoading();
|
|
wx.request({
|
|
url: getApp().globalData.serverUrl + url,
|
|
method: method,
|
|
data: data,
|
|
header: {
|
|
'content-type': type, // 默认值
|
|
'Cookie': getApp().globalData.sessionId,
|
|
},
|
|
success: (res => {
|
|
if(res.statusCode >= 500){
|
|
wx.showToast({
|
|
title: '服务器错误',
|
|
icon: 'none'
|
|
})
|
|
}else{
|
|
if(re==1){
|
|
resolve(res)
|
|
}
|
|
resolve(res.data);
|
|
}
|
|
|
|
// if (res.data.code == 1) {
|
|
// resolve(res.data);
|
|
// }else if(res.data.code == -1){
|
|
// getApp().onLaunch()
|
|
// wx.switchTab({
|
|
// url: '/pages/main/main',
|
|
// })
|
|
// }
|
|
// else {
|
|
// var msg = ''
|
|
// if(res.data){
|
|
// msg = res.data
|
|
// }
|
|
// wx.showToast({
|
|
// title: msg,
|
|
// icon: 'none',
|
|
// duration: 1000
|
|
// })
|
|
// }
|
|
|
|
}),
|
|
fail: (res => {
|
|
wx.showToast({
|
|
title: '请求出错',
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
console.log(res)
|
|
reject('网络出错');
|
|
}),
|
|
complete: function () {
|
|
wx.hideNavigationBarLoading();
|
|
}
|
|
|
|
})
|
|
})
|
|
return promise;
|
|
}
|
|
|
|
module.exports = {
|
|
request: request
|
|
} |