mpvue中,fly请求拦截token

const Fly = require("flyio/dist/npm/wx")
const fly = new Fly
Vue.prototype.$http = fly;
 
fly.interceptors.request.use((request) => {
  //给所有请求添加自定义header
  if (api.Get('token')) {
    request.timeout = 30000,
      request.headers = {
        "content-type": "application/json",
        "cld.stats.page_entry": api.Get('scene'),
        "version": store.state.version,
        "token": api.Get('token')
      }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    return request;
  } else {
    fly.lock();//锁住请求
    return Public.Load().then(res => {
      request.timeout = 30000,
        request.headers = {
          "content-type": "application/json",
          "cld.stats.page_entry": api.Get('scene'),
          "version": store.state.version,
          "token": api.Get('token')
        }
      wx.showLoading({
        title: "加载中",
        mask: true,
      });
        //等待token返回之后在解锁,
      fly.unlock();
      return request;//继续之前的请求,
    })
  }
})
 
fly.interceptors.response.use(
  (response) => {
    wx.hideLoading();
    return response
  },
  (err) => {
    wx.hideLoading();
    if (err.status == 0) {
      return "网络连接异常"
    } else if (err.status == 1) {
      return "网络连接超时"
    } else if (err.status == 401) {
      return "用户未登录"
    } else {
      if (err.response.data.message) {
        return err.response.data.message
      } else {
        return '请求数据失败,请稍后再试'
      }
    };
    // Do something with response error
  }
)

你可能感兴趣的:(mpvue中,fly请求拦截token)