// 展示進度條的網絡請求
// url:網絡請求的url
// params:請求參數
// message:進度條的提示信息
// success:成功的回調函數
// fail:失敗的回調
function requestLoading(url, params, message, success, fail, method = 'get') {
// console.log(params)
wx.showNavigationBarLoading();
if (message != "") {
wx.showLoading({
title: message,
})
}
var sessionid = wx.getStorageSync('wx_outin') || wx.getStorageSync('wx');
if (sessionid != "" && sessionid != null) {
var header = {'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'wx=' + sessionid}
} else {
var header = {'content-type': 'application/x-www-form-urlencoded'}
};
wx.request({
url: url,
data: params,
header: header,
method: method,
success: function (res) {
var headerStr = JSON.stringify(res.header);
// 判斷響應頭cookie里是否有wx,若有則存儲到wxCookie里
if (sessionid == "" || sessionid == null){
if (headerStr.indexOf("wx=") > -1) {
var reg = /wx=(\w+)[;|"]/;
var wxCookie = reg.exec(headerStr)[1];
wx.setStorageSync('wx', wxCookie);
}
}
// console.log(wx.getStorageSync('wx'))
wx.hideNavigationBarLoading()
if (message != "") {
wx.hideLoading()
}
if (res.statusCode == 200) {
console.log(res.data.stat)
if (res.data.stat==2){
wx.showModal({
title: '提示',
content: '賬號已退出,請重新登錄',
showCancel: false,
success: function (res) {
wx.redirectTo({
url: '../regist/index'
});
}
})
}
success(res.data)
} else {
typeof fail == "function" && fail()
}
},
fail: function (res) {
console.log(res);
if (res.errMsg == 'request:fail ') {
wx.showModal({
title: '提示',
content: '當前網絡狀態差,請重試',
success: function(res) {
if (res.confirm) {
setTimeout(function () {
requestLoading(url, params, message, success, fail, method);
}, 1000);
} else if (res.cancel) {
console.log('用戶點擊取消');
wx.redirectTo({
url:'../regist/index'
});
}
}
})
}
wx.hideNavigationBarLoading();
if (message != "") {
wx.hideLoading()
}
typeof fail == "function" && fail()
},
complete: function (res) {
},
})
}