美文网首页
mpvue中,fly请求拦截token

mpvue中,fly请求拦截token

作者: 苏本的书柜 | 来源:发表于2018-12-10 15:00 被阅读72次
    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

          本文链接:https://www.haomeiwen.com/subject/ywnrhqtx.html