axios

export default {
  get: function (url, params, response) {
    return apiAxios('GET', url, params, response)
  },
  post: function (url, params, response) {
    url = process.env.NODE_ENV != 'production' ? 'pc' + url : url;
    return postOri(url, params, {},response)
  },
  postBlob: function (url, params, response) {
    return postBlob('POST', url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios('PUT', url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios('DELETE', url, params, response)
  },
  posth5: function (url, params, response) {
    return h5Axios('POST', url, params, response)
  },
  getdat: function (url, params, response) {
    return datAxios('GET', url, params, response)
  },
  postdat: function (url, params, response) {
    return datAxios('POST', url, params, response)
  },
  env:process.env.NODE_ENV != 'production' ? 'pc': '',
}



import axios from 'axios'
import qs from 'qs'
import {
  Message
} from 'element-ui';
import Vue from 'vue'
import router from '../router'
axios.interceptors.request.use(
  config => {
      config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
      if(config.method=='post'){
        config.transformRequest = [function (data,type) {
          data = qs.stringify(data)
          return data;}
        ];
      }
      console.log(config,'config')
    return config;
  },
  err => {
    return Promise.reject(err);
});
let http = axios.create({
  withCredentials: true,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    'lan': localStorage.getItem('lanCode') || 'cn'
  },
  transformRequest: [function (data,type) {
    data = qs.stringify(data)
    return data;
  }]
});
let datHttp = axios.create({
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
    'lan': localStorage.getItem('lanCode') || 'cn'
  },
  transformRequest: [function (data,type) {
    return data;
  }]
});
function postOri(url, data = {}, config = {
  "headers": {
   'Content-Type': 'application/json;charset=utf-8',
   'lan': localStorage.getItem('lanCode') || 'cn'
  },
 },response) {
  return new Promise((resolve, reject) => {
   axios.post(url, data, config)
   .then(function (res) {
    let data = res.data;
    if (data.code == 1001) {
      Message.warning(data.message);
      localStorage.clear();
      sessionStorage.clear();
      // Router.push({path:'/login'});
      setTimeout(_ => {
        router.replace({
          path: '/login' //自己修改
        })
      }, 100);
      return;
    }
    if (res.status == 200) {
      response(data);
    }
     
  }).catch(function (err) {
    if (err.response && err.response.status == 500) {
      Message.warning('系统异常');
      response(err);
    }
  })
  })
 }
axios.interceptors.request 请求之前进来拦截
import axios from 'axios'
import qs from 'qs'
import {
Message
} from 'element-ui';
import Vue from 'vue'
import router from '../router'
axios.interceptors.request.use(
config => {
config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
if(config.method=='post'){
config.transformRequest = [function (data,type) {
data = qs.stringify(data)
return data;}
];
}
console.log(config,'config')
return config;
},
err => {
return Promise.reject(err);
});
let http = axios.create({
withCredentials: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'lan': localStorage.getItem('lanCode') || 'cn'
},
transformRequest: [function (data,type) {
data = qs.stringify(data)
return data;
}]
});
原文地址:https://www.cnblogs.com/guidan/p/10489277.html