What should I do if the WeChat applet network times out?

coldplay.xixi
Release: 2020-07-14 15:08:11
Original
14391 people have browsed it

Solution to the WeChat applet network timeout: 1. If the task times out when running onLaunch, set the error level to 0 and redirect to the error page; 2. If the page request times out, set the error level to 0 2. Can be fixed by retrying.

What should I do if the WeChat applet network times out?

Solution to WeChat applet network timeout:

onLaunchThrough this we can Obtain the user's basic information, or position it for next step processing. If the data cannot be obtained, the entire applet will fail.

So I suggest that errors can be divided into two levels. If the developer server cannot be connected, it can be handled by reloading the page. However, if the data in onLaunch cannot be obtained, the user must exit the small Program, reopen it and try again. The configuration in

app.json is used to set the timeout, the default is 6000 milliseconds, which is 6 seconds

"networkTimeout": {
    "request": 6000,
    "downloadFile": 10000
  }
Copy after login

Related learning recommendations: WeChat Mini program development tutorial

1. If the task times out when running onLaunch, I set the error level to 0 and redirect to the error page

wx.login({
      success(res) {
        if (res.code) {
          //console.log(res.code);
          //发起网络请求
          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: '可能网络不太好,请重试!',
                success: function () {
                  wx.navigateTo({
                    url: '/pages/reload?error=0'
                  });
                }
              });
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      
      }, fail: function () {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '可能网络不太好,请重试!',
          success: function () {
            wx.navigateTo({
              url: '/pages/reload?error=0'
            });
          }
        });
      }
    });
Copy after login

2. If the page request times out, I set the error as 2, which can be fixed by retrying

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: '可能网络不太好,请重试!',
        success: function () {
          wx.navigateTo({
            url: '/pages/reload?error=1'
          });
        }
      });
    }
  })
Copy after login

3. Process the page: Use getCurrentPages() to get the previous page object, Must use wx.navigateToredirect to this page

/**
  * 页面的初始数据
  */
 data: {
  error:0 // 0:需要退出小程序 1:可以重新发起网络请求重试
 },
 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中所带的参数可以查看options
     console.log('options', options);
     //拼接url的参数
     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,
         })
       }
     });
   }
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ error: options.error});
   // this.reLoad(options.error);
  },
Copy after login

The above is the detailed content of What should I do if the WeChat applet network times out?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template