Small program wx.request () encapsulation, to support the operation and the common public in response to operation of front

Most read online tutorials, all with Promise to achieve, I use the article is relatively clear odd way, it can be said, the original format of pollution.

Directly on the code:

app.js method defined in a request. In this way, you can directly app.request global call.

Request (A) { 
    // here may be pre-operation 
    a.url = + a.url the baseUrl                   // add base address api 
    a.header = a.header || {} 
    a.data = a.data || {} 
    a.data.id = . 1                             // this can add global ID 
    a.header.authorization = "Authorization" // Add Authorization 

    // Success callback 
    the let = Success a.success
     IF (Success) { 
      a.success = function (RES) { 
        console.log ( "this is the public callback operation after the success, you can do something, below empathy" ) 
        success (RES)  
      }
    }
    // fail 回调
    let fail = a.fail
    if (fail) {
      a.fail = function (res) { 
        fail(res)
       }
    }
    // complete 回调
    let complete = a.complete
    if (complete) {
      a.complete = function (res) {
        complete(res)
      }
    }
    wx.request(a)
  }

 

Code is well understood, plainly, is to rewrite the parameters of the object to wx.request. In this way, a direct reference wx.request document can be in.

 

Guess you like

Origin www.cnblogs.com/alpiny/p/12632183.html