index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Vue from 'vue';
  2. import axios from 'axios';
  3. import VueAxios from 'vue-axios';
  4. // axios.defaults.headers.post['Content-Type'] = 'application/json';
  5. axios.defaults.transformRequest = [function (data) {
  6. if (data instanceof Object && !(data instanceof FormData)) {
  7. data = JSON.stringify(data)
  8. }
  9. return data;
  10. }],
  11. // 添加请求拦截器
  12. axios.interceptors.request.use(function (config) {
  13. let localhost = ''
  14. if (process.env.NODE_ENV == "production") {
  15. localhost = window.staticHost ? window.staticHost : "/spfm/"
  16. }
  17. config.url = localhost + config.url
  18. let url = localhost + config.url
  19. if (url.indexOf('/sysmgr/sys/login/auth/captcha') == -1 && url.indexOf('/sysmgr/sys/login/auth/token') == -1) {
  20. let token = window.sessionStorage.agileauthtoken;
  21. if (token != undefined && token != 'undefined') {
  22. token = JSON.parse(token);
  23. config.headers['agileauthtoken'] = token
  24. } else {
  25. global.$vm.$notify.error({
  26. title: '提示',
  27. message: '登录超时'
  28. })
  29. global.$vm.$router.push('/login')
  30. }
  31. }
  32. // 在发送请求之前做些什么
  33. return config;
  34. }, function (error) {
  35. // 对请求错误做些什么
  36. return Promise.reject(error);
  37. });
  38. // // 添加响应拦截器
  39. // axios.interceptors.response.use(function(response) {
  40. // if(response.data.ret != 0){
  41. // global.$vm.$notify.error({
  42. // title:'提示',
  43. // message:response.data.msg
  44. // })
  45. // }
  46. // // 对响应数据做点什么
  47. // return response;
  48. // }, function(error) {
  49. // try{
  50. // if(error.response.status==401){
  51. // global.$vm.$notify.error({
  52. // title:'提示',
  53. // message:'登录超时'
  54. // })
  55. // global.$vm.$router.push('/login')
  56. // }else if(error.response.status==404){
  57. // global.$vm.$notify.error({
  58. // title:'提示',
  59. // message:'请求接口不存在'
  60. // })
  61. // }else{
  62. // global.$vm.$notify.error({
  63. // title:'提示',
  64. // message:'服务器异常'
  65. // })
  66. // }
  67. // }catch{
  68. // //TODO handle the exception
  69. // }
  70. // // 对响应错误做点什么
  71. // return {
  72. // data:{
  73. // ret:-1,
  74. // msg:error.message,
  75. // data:error.response.data
  76. // },
  77. // status: error.response.status
  78. // };
  79. // });
  80. Vue.use(VueAxios, axios)
  81. export default axios;