封装request api


const baseURL = 'url' 

function request(method, url, data,OK,ERR=(err)=>{console.log(err)},OTHER=(res)=>{}) {

    return new Promise(function(resolve, reject) {

        let header = {

            'content-type': 'application/json',

            'token': wx.getStorageSync("Token") || '',

        }

        if(url !== '/share_park/v1.0.1/vxowner/data'){

          header['plat'] = 'daanju'

        }     

        wx.request({

            url:url.substr(0,4) =='http' ? url : baseURL + url,

            method: method,

            data: data,

            header: header,

            success(res) {

                if (res.data.status=='OK') {   

                    OK(res.data)

                } else {               

                    OK(res.data)

                  if(res.data.code == 5000 ){

                    wx.login({   

                        success: res => {

                          API.Login({

                            user_info: {},

                            code:res.code

                          },(res)=>{

                            wx.setStorage({

                            key:"Token",

                            data:res.data.xvowner.token

                          })

                          request(method, url, data,OK)

                        })                   

                        }

                      })

                  }else{

                    OTHER(res.data)

                  }

                }

            },

            fail(err) { 

                console.log(url.substr(0,4) =='http' ? url : baseURL + url,"请求没有发送出去") 

            }

        })

    })

}

const API = {

  Login: (data,OK) => request('POST','/share_park/v1.0.1/vxowner/data',data,OK)

}

module.exports = {

  API: API

}

你可能感兴趣的:(封装request api)