Home  >  Article  >  WeChat Applet  >  Solution to disconnection and reloading during WeChat development

Solution to disconnection and reloading during WeChat development

Y2J
Y2JOriginal
2017-05-05 11:16:036034browse

Analysis

微信小程序目前没有提供刷新API,所以要自己去记录当前操作,点击刷新重新执行一遍

Effect

Solution to disconnection and reloading during WeChat development

##Click 'Reload' to reload the page

Implementation

Since when working on a project, page rendering is inseparable from

Interface requests, so I encapsulated wx.request to determine whether the network is disconnected, and record this time after the network is disconnected Request

    /**
     * obj  request请求参数
     * cb   requrst请求成功回掉
     * page 当前page实例
    **/

    function wxRequest (obj, cb, page, type) {
        var isOne = true
        var cachFn = function () {
            wx.request({
                  url: obj.url,
                  data: obj.data || {},
                  method: obj.method || 'GET',
                  success: function (res) {
                    cb.call(page, res)
                    if (!page.data.isNet) {
                        page.setData({
                            isNet: true
                        })
                    }
                  },
                  // fail执行时当断网处理
                  fail: function () {
                      // 防止fail 有时会执行两次,影响渲染
                      if (!isOne) {
                          return
                      }
                    page.setData({
                        isNet: false,
                        isRequested: false
                    })
                    // 记录本次请求,加载时,执行page实例的reloadFn即可
                      page.reloadFn = wxRequest(obj, cb, page, 1)
                      isOne = false
                  }
            })
        }
    
        if (type) {
            page.isRequested = true
        }
    
        return type ? cachFn : cachFn()
    }

Application

 let data = {
      url: '',
      data: {},
      method: ''
    }
    wxTools.wxRequest(data, (res) => {
        // 数据渲染
        this.setData({})
    }, this)

When the network is disconnected, executing this.reloadFn will return to the last request, isNet and determine whether the network is disconnected

[Related recommendations]

1.

Complete source code of WeChat Mini Program

2.

WeChat Mini Program demo: Yangtao

The above is the detailed content of Solution to disconnection and reloading during WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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