美文网首页
对axios封装

对axios封装

作者: Augenstern___ | 来源:发表于2018-12-05 11:24 被阅读0次

    首先引入

    import axios from 'axios'
    import qs from 'qs'
    import Vue from 'vue'
    import router from '@/router/index'
    import { Loading } from 'element-ui';
     //配置请求头
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
    

    配置全局地址 也可以写成自己的地址

    axios.defaults.baseURL = process.env.BASE_API;   //代表config配置文件的地址  
    Vue.prototype.path = process.env.BASE_API;
    Vue.prototype.pathImg = process.env.PATH_IMG;
    
     let loading = null;
     let loadingOpenCount = 0;
     axios.interceptors.request.use(function (config) {
     if (config.showLoading != false && config.method != 'OPTIONS') {
     []
    loadingOpenCount++;
     }
     if (config.showLoading != false && loadingOpenCount == 1 && config.method != 'OPTIONS') {
       open();
     }
      if (sessionStorage.getItem('token')) {
      config.headers.common['token'] = sessionStorage.getItem('token');
    }
    if (config.method === 'post') {
      config.data = qs.stringify(config.data);
    }
    if (config.method === 'get') {
      config.url = config.url + "?" + qs.stringify(config.data);
    }
    if (config.method === 'upload') {
     config.method = 'post';
    }
      return config
    }, function (error) {
      return Promise.reject(error)
    });
    axios.interceptors.response.use(
      response => {
         if (response.config.showLoading != false && response.config.showLoading != false && response.config.method != 'OPTIONS') {
      loadingOpenCount--;
     if (loadingOpenCount == 0) {
      setTimeout(function () {
          close();
      }, 0.3 * 1000)
     }
     }
        if(response.headers.authorization){
          sessionStorage.setItem('token',response.headers.authorization) ;
        }
          //判断返回的数据里面code资源
      else if (!response || response.data.code == 401) {
        sessionStorage.clear();
        router.push({
          path: '/login', name: 'login'
          , params: { 'error': response.data.tokenMsg }
        });
       close();
    }
    return response;
    },
      error => {
    close();
        //请求异常处理
                 switch (error.response.status) {
                    case 401:
                       sessionStorage.clear();
                        router.push({path:'/Login',name:'Login'
                        ,params: {'error': 'token失效,请重新登陆'}
                       })
                       }
    if(error.response == undefined){
      return {data: {success: false, msg: '请求异常'}};
    }else{
      return checkStatus(error.response);
    }
    
    if (error.response && error.response.data) {
      return Promise.reject(error.response.data)
    }
    // 返回接口返回的错误信息
    });
     function getConfig(config){
        return config;
     }
     function open() {
       loading = Loading.service({
    lock: false,
     text: '拼命加载中...',
     spinner: 'el-icon-loading',
     background: 'rgba(0, 0, 0, 0.7)'
     });
       }
     function close() {
       if (loading) {
         loading.close();
       }
     }
    
      // 检查状态
      function checkStatus(response) {
        let data = {data: {}};
      data.data['success'] = false;
        switch (response.status) {
        case 400:
          data.data['msg'] = '请求错误'
          break
    
    case 401:
      data.data['msg'] = '未授权,请登录'
      break
    
    case 403:
      data.data['msg'] = '拒绝访问'
      break
    
    case 404:
      data.data['msg'] = '请求地址出错'
      break
    
    case 408:
      data.data['msg'] = '请求超时'
      break
    
    case 500:
      data.data['msg'] = '服务器内部错误'
      break
    
    case 501:
      data.data['msg'] = '服务未实现'
      break
    
    case 502:
      data.data['msg'] = '网关错误'
      break
    
    case 503:
      data.data['msg'] = '服务不可用'
      break
    
    case 504:
      data.data['msg'] = '网关超时'
      break
    
    case 505:
      data.data['msg'] = 'HTTP版本不受支持'
      break
    
    default:
      data.data['msg'] = '异常请求';
      }
      return data;
    }
    

    相关文章

      网友评论

          本文标题:对axios封装

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