request.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import axios from "axios";
  2. import store from "../store";
  3. import router from "../router";
  4. import qs from "qs"
  5. // 创建axios实例
  6. const service = axios.create({
  7. baseURL: "/api",//process.env.VUE_APP_URL, // api 的 VUE_APP_URL
  8. timeout: 30000 // 请求超时时间
  9. });
  10. // request拦截器,在请求之前做一些处理
  11. service.interceptors.request.use(
  12. config => {
  13. if(config.url == "/report/record") {
  14. config.headers["Content-Type"] = "multipart/form-data";
  15. } else if(config.url != "/report/editReport" && config.url != "/project/editProject" && config.url != "/common/uploadFile" && config.url != "/project/editProject") {
  16. config.data = qs.stringify(config.data);
  17. config.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
  18. } else {
  19. config.dataType = "json";
  20. config.processData= false;
  21. config.contentType= false;
  22. }
  23. if (localStorage.userInfo) {
  24. config.headers["Token"] = JSON.parse(localStorage.userInfo).id;
  25. }
  26. return config;
  27. },
  28. error => {
  29. return Promise.reject(error);
  30. }
  31. );
  32. // response 拦截器,数据返回后进行一些处理
  33. service.interceptors.response.use(
  34. response => {
  35. if(response.status == 200) {
  36. const res = response.data;
  37. return res;
  38. } else {
  39. return Promise.reject("网络异常");
  40. }
  41. },
  42. error => {
  43. return Promise.reject("网络异常");
  44. }
  45. );
  46. export default service;