WeChat Mini Program Interface Package

function baseRequest ({url, method, header, data}, resolve, reject) { 
  wx.request ({ 
    url, 
    method, 
    header, 
    data, 
    success: function (res) { 
      // Need to judge the server code to use this paragraph 
      // Return 0 means real success, other codes indicate various error codes 
      if (res.data.code === 0) { 
        resolve (res) 
      } else { 
        reject (res) 
      } 
      resolve (res) 
    }, 
    fail: function (res ) { 
      reject (res) 
    } 
  }) 
}

  Use promise

function requestPromise(options) {
  let req = new Promise((resolve, reject) => {
    baseRequest(options, resolve, reject)
  })
  return req
}

  get method

function get(options) {
  options.method = 'GET'
  return requestPromise(options)
}

  post method

function post(options) {
  options.method = 'POST'
  if (!options.header) {
    options.header = {}
  }
  options.header["Content-Type"] = "application/json"
  return requestPromise(options)
}

  put method

function put(options) {
  options.method = 'PUT'
  if (!options.header) {
    options.header = {}
  }
  options.header["Content-Type"] = "application/json"
  return requestPromise(options)
}

  

Guess you like

Origin www.cnblogs.com/feifan1/p/12743493.html