您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何解決微信小程序網(wǎng)絡(luò)超時(shí)的問(wèn)題的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
微信小程序網(wǎng)絡(luò)超時(shí)的解決辦法:1、在onLaunch運(yùn)行任務(wù)如果超時(shí),就把錯(cuò)誤級(jí)別定為0,并轉(zhuǎn)向錯(cuò)誤頁(yè)面;2、如果是頁(yè)面請(qǐng)求超時(shí),就把錯(cuò)誤定為2,可以通過(guò)重試來(lái)修復(fù)。
微信小程序網(wǎng)絡(luò)超時(shí)的解決辦法:
onLaunch
通過(guò)這個(gè)我們可以獲取用戶的基本信息,或者定位用來(lái)做下一步處理,如果這個(gè)無(wú)法獲取數(shù)據(jù),會(huì)導(dǎo)致整個(gè)小程序的失敗。
所以我建議可以把錯(cuò)誤分兩個(gè)級(jí)別,假如是開(kāi)發(fā)者服務(wù)器連接不上,可以通過(guò)重載頁(yè)面來(lái)處理,但是如果是onLaunch中的數(shù)據(jù)也無(wú)法獲取就必須讓用戶退出小程序,重新打開(kāi)再試了。
app.json
中配置用來(lái)設(shè)置超時(shí)時(shí)間,默認(rèn)為6000毫秒,也就是6秒
"networkTimeout": { "request": 6000, "downloadFile": 10000 }
相關(guān)學(xué)習(xí)推薦:微信小程序開(kāi)發(fā)教程
一、在onLaunch
運(yùn)行任務(wù)如果超時(shí),我把錯(cuò)誤級(jí)別定為0,并轉(zhuǎn)向錯(cuò)誤頁(yè)面
wx.login({ success(res) { if (res.code) { //console.log(res.code); //發(fā)起網(wǎng)絡(luò)請(qǐng)求 wx.request({ url: 'https://**/index/zz/getuserinfo', data: { code: res.code }, success: res => { wx.setStorageSync('open_id', res.data.openid); wx.setStorageSync('session_id', res.data.session_id); wx.setStorageSync('session_key', res.data.session_key); that.globalData.isSessionkey=true; //console.log(res.data); if (that.sessionCallback) { that.sessionCallback(res); } },fail:f=> { wx.showModal({ title: '提示', showCancel: false, content: '可能網(wǎng)絡(luò)不太好,請(qǐng)重試!', success: function () { wx.navigateTo({ url: '/pages/reload?error=0' }); } }); } }) } else { console.log('登錄失?。?#39; + res.errMsg) } }, fail: function () { wx.showModal({ title: '提示', showCancel: false, content: '可能網(wǎng)絡(luò)不太好,請(qǐng)重試!', success: function () { wx.navigateTo({ url: '/pages/reload?error=0' }); } }); } });
二、如果是頁(yè)面請(qǐng)求超時(shí),我把錯(cuò)誤定為2,可以通過(guò)重試來(lái)修復(fù)
wx.request({ url: webUrl + model.url, data: model.param, method: model.method, success: function (res) { }, fail: function (res) { wx.hideLoading(); wx.showModal({ title: '提示', showCancel: false, content: '可能網(wǎng)絡(luò)不太好,請(qǐng)重試!', success: function () { wx.navigateTo({ url: '/pages/reload?error=1' }); } }); } })
三、處理頁(yè)面:要使用getCurrentPages()
獲取上一頁(yè)對(duì)象,必須使用wx.navigateTo
轉(zhuǎn)向此頁(yè)
/** * 頁(yè)面的初始數(shù)據(jù) */ data: { error:0 // 0:需要退出小程序 1:可以重新發(fā)起網(wǎng)絡(luò)請(qǐng)求重試 }, reLoad:function(error) { var pages = getCurrentPages();//獲取頁(yè)面棧 if (pages.length > 1) { //上一個(gè)頁(yè)面實(shí)例對(duì)象 var prePage = pages[pages.length - 2]; let url=prePage.route; var options = prePage.options //如果要獲取url中所帶的參數(shù)可以查看options console.log('options', options); //拼接url的參數(shù) var urlWithArgs = url + '?' for (var key in options) { var value = options[key] urlWithArgs += key + '=' + value + '&' } urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1) wx.reLaunch({ url: '/' + urlWithArgs, fail:function(e) { wx.switchTab({ url: '/' + prePage.route, }) } }); } }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 */ onLoad: function (options) { this.setData({ error: options.error}); // this.reLoad(options.error); },
感謝各位的閱讀!關(guān)于“如何解決微信小程序網(wǎng)絡(luò)超時(shí)的問(wèn)題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。