微信小程序網(wǎng)絡(luò)超時怎么辦?

微信小程序網(wǎng)絡(luò)超時的解決辦法:1、在onLaunch運行任務(wù)如果超時,就把錯誤級別定為0,并轉(zhuǎn)向錯誤頁面;2、如果是頁面請求超時,就把錯誤定為2,可以通過重試來修復(fù)。

邢臺網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)公司從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司

微信小程序網(wǎng)絡(luò)超時的解決辦法:

onLaunch通過這個我們可以獲取用戶的基本信息,或者定位用來做下一步處理,如果這個無法獲取數(shù)據(jù),會導(dǎo)致整個小程序的失敗。

所以我建議可以把錯誤分兩個級別,假如是開發(fā)者服務(wù)器連接不上,可以通過重載頁面來處理,但是如果是onLaunch中的數(shù)據(jù)也無法獲取就必須讓用戶退出小程序,重新打開再試了。

app.json中配置用來設(shè)置超時時間,默認(rèn)為6000毫秒,也就是6秒

"networkTimeout": {
    "request": 6000,
    "downloadFile": 10000
  }

相關(guān)學(xué)習(xí)推薦:微信小程序開發(fā)教程

一、在onLaunch運行任務(wù)如果超時,我把錯誤級別定為0,并轉(zhuǎn)向錯誤頁面

wx.login({
      success(res) {
        if (res.code) {
          //console.log(res.code);
          //發(fā)起網(wǎng)絡(luò)請求
          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ò)不太好,請重試!',
                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ò)不太好,請重試!',
          success: function () {
            wx.navigateTo({
              url: '/pages/reload?error=0'
            });
          }
        });
      }
    });

二、如果是頁面請求超時,我把錯誤定為2,可以通過重試來修復(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ò)不太好,請重試!',
        success: function () {
          wx.navigateTo({
            url: '/pages/reload?error=1'
          });
        }
      });
    }
  })

三、處理頁面:要使用getCurrentPages()獲取上一頁對象,必須使用wx.navigateTo轉(zhuǎn)向此頁

/**
  * 頁面的初始數(shù)據(jù)
  */
 data: {
  error:0 // 0:需要退出小程序 1:可以重新發(fā)起網(wǎng)絡(luò)請求重試
 },
 reLoad:function(error)
 {
   var pages = getCurrentPages();//獲取頁面棧
   if (pages.length > 1) {
     //上一個頁面實例對象
     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)聽頁面加載
   */
  onLoad: function (options) {
    this.setData({ error: options.error});
   // this.reLoad(options.error);
  },

網(wǎng)站名稱:微信小程序網(wǎng)絡(luò)超時怎么辦?
鏈接地址:http://muchs.cn/article24/chseje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈、小程序開發(fā)、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)