封装的微信小程序网络请求及token取不到的问题

request.js

var app = getApp();
const BASEURL = 'http://test.com'
app.globalData.baseurl = BASEURL
var token;
let that = this
//以下代码是为了处理token存入storage中 可能出现的第一次取token为undefined
var header = {
  "content-type": "application/json",
  "X-Access-Token": token
}

function changToken(requetsToken) {
  token = requetsToken
  header['X-Access-Token'] = requetsToken
}
//获取code码
function getCode() {
  wx.login({
    success: res => {
      if (res.code) {
        app.globalData.code = res.code
      }
    }
  })
}
//GET请求
function GET(url, params) {
  return new Promise((resolve, reject) => {
    let res = request('GET', url, params)
    resolve(res)
  })
}
//POST请求
function POST(url, params) {
  return new Promise((resolve, reject) => {
    let res = request('POST', url, params)
    resolve(res)
  })
}
//POST请求
function PUT(url, params) {
  return new Promise((resolve, reject) => {
    let res = request('PUT', url, params)
    resolve(res)
  })
}

//DELETE请求
function DELETE(url, params) {
  return new Promise((resolve, reject) => {
    let res = request('DELETE', url, params)
    resolve(res)
  })
}

function request(method, url, params) {
  wx.showLoading({
    title: '请求中...',
    mask: true
  });
  url = BASEURL + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: params,
      method: method,
      header: header,
      success: function (res) {
        wx.hideLoading()
        if (res.data.status == 500) {
          wx.showToast({
            title: '请重新登录',
            icon: 'none',
            image: '',
            duration: 2000,
            mask: false,
            success: (result) => {
              wx.reLaunch({
                url: '../pages/login/login',
                success: (result) => {},
                fail: () => {
                
                },
                complete: () => {}
              });
            },
            fail: () => {},
            complete: () => {}
          });
        } else {

          resolve(res)
        }
      },
      fail: function (res) {
        // console.log('网络错误', res)
        wx.showToast({
          title: '网络错误,请检查您的网络',
          icon: 'none',
          image: '',
          duration: 2000,
          mask: false,
          success: (result) => {},
          fail: () => {},
          complete: () => {}
        });
        wx.hideLoading()
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  })
}

module.exports = {
  GET: GET,
  POST: POST,
  PUT: PUT,
  DELETE: DELETE,
  getCode: getCode,
  request: request,
  changToken: changToken
}
api.js

const {
  GET,
  POST,
  PUT
} = require('./request.js')

const wxlogin = (params) => POST("/mobile/sys/wxlogin", params);

module.exports = {
  wxlogin :wxlogin 
}

使用

import {
  wxlogin
} from '../api'

login() {
 let params = {
      "password": md5.md5(this.data.password),
      "username": this.data.username
    }
    
    wxlogin(params).then(res => {
       
    })
}

你可能感兴趣的:(封装的微信小程序网络请求及token取不到的问题)