マイクロ信ウィジェットネットワーク層パッケージ(promise,ログインロック)

4677 ワード

一、ウィジェットのrequestのパッケージ
          ,   request         ,       ,       ,           ,    。
         ,      promise。
function baseRequest({ url, method, header, data, complete }, resolve, reject) {
  wx.request({
    url,
    method,
    header,
    data,
    success: function (res) {
      //        code     
      //        0       ,  code       
      // if (res.data.code === 0) {
      //   resolve(res)
      // } else {
      //   reject(res)
      // }
      resolve(res)
    },
    fail: function (res) {
      reject(res)
    },
    complete: function (res) {
      complete(res)
    }
  })
}

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

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

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

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


module.exports = {
    get,
    post,
    put
}
    :
1.                 ,     network.js。
2.            wx     。 wx.Network = require('./network.js')。( app.js    )
3.     
wx.Network.get({
    url: 'xxx.xxx.xxx', //     
    data: {
        a: "a"    //     
    }
  }).then(res => {
      console.log("    ")
  }).catch(res => {
      console.log("    ")  
  })

二、ログインロック
   request       ,         ,  
1.          ,      token,      token       token         (        )
2.                    token   ,           (    ing...)
3.                   ,          ,        ,             (        ),    ,         loading,         ,             ,          
4.               ,          loading,    “   ”   
5.           token    ,   ,           
6.          token,          ,      ,          (200ms),    ,      ,    ,    ,      

           : (       wait   ,  get,post,put        ,      token )
function baseRequest({ url, method, header, data, complete }, resolve, reject) {
  wx.request({
    url,
    method,
    header,
    data,
    success: function (res) {
      //        code     
      //        0       ,  code       
      // if (res.data.code === 0) {
      //   resolve(res)
      // } else {
      //   reject(res)
      // }
      resolve(res)
    },
    fail: function (res) {
      reject(res)
    },
    complete: function (res) {
      complete(res)
    }
  })
}

function wait(options, needToken, resolve, reject) {
  if (needToken) {
    var token = wx.getStorageSync('token')
    if (!token) {
      setTimeout(() => {
        wait.apply(null, arguments)
      }, 200)
    } else {
      if (!options.header) {
        options.header = {}
      }
      options.header["token"] = token
      baseRequest(options, resolve, reject)
    }
  } else {
    baseRequest(options, resolve, reject)
  }
}

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

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

function post(options, needToken) {
  options.method = 'POST'
  if (!options.header) {
    options.header = {}
  }
  options.header["Content-Type"] = "application/x-www-form-urlencoded"
  return requestPromise(options, needToken)
}

function put(options, needToken) {
  options.method = 'PUT'
  if (!options.header) {
    options.header = {}
  }
  options.header["Content-Type"] = "application/x-www-form-urlencoded"
  return requestPromise(options, needToken)
}


module.exports = {
    get,
    post,
    put
}
    :(         ,            )
1.                 ,     network.js。
2.            wx     。 wx.Network = require('./network.js')。( app.js    )
3.     
wx.Network.get({
    url: 'xxx.xxx.xxx', //     
    data: {
        a: "a"    //     
    }
  }, true  //        ,true    token, false     
  ).then(res => {
      console.log("    ")
  }).catch(res => {
      console.log("    ")  
  })