小程序設(shè)置請求超時(shí)
const BaseUrl = "https://www.cn"
const post = {
post ( url, data ) {
// 數(shù)據(jù)請求出來前顯示的加載動畫
wx.showLoading({
title: '正在加載中...',
mask:true//請求時(shí)是否開啟白色蒙版,以防點(diǎn)擊穿透
})
return new Promise((resolve, reject) => {
wx.request({
url: BaseUrl + url, //將傳入的網(wǎng)址與baseurl拼接
data:data || {}, //有data就傳入data盲链,沒有設(shè)置為空
method:"POST", //請求的類型
header: {//設(shè)置請求頭
'content-Type': 'application/json',
'Cookie':wx.getStorageSync("key"),//讀取本地存儲的token
},
success (res) {//成功時(shí)的回調(diào)
if(res.data == "{code:9006}"){
wx.showModal({
title: '提示',
showCancel: false,
content: '登入失效,請重新登入',
success(res) {
if (res.confirm) {//confirm為true時(shí)代表用戶點(diǎn)擊了按鈕
wx.reLaunch({
url: '/pages/log/log'
});
}
}
})
}else if(res.data == "{code:1903}"){
wx.showToast({
title: '請求超時(shí),請重新嘗試',
icon:"none",
mask:true,
})
}
resolve(res)
},
fail () {//失敗時(shí)的回調(diào)
wx.hideLoading();
wx.showToast({
title: '請求超時(shí),請重試',
icon:"none"
})
},
reject(){
},
complete () {//接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功夕晓、失敗都會執(zhí)行)
//取消加載動畫
wx.hideLoading({
mask:false
})
}
})
})
},
}
export default post