filter.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import axios from 'axios'
  2. import VueAxios from 'vue-axios'
  3. import Vue from 'vue'
  4. import ElementUI, {
  5. Loading
  6. } from 'element-ui';
  7. let loading //定义loading变量
  8. function startLoading() { //使用Element loading-start 方法
  9. loading = Loading.service({
  10. text: '请稍等...',
  11. background: 'rgba(0, 0, 0, 0.2)'
  12. })
  13. }
  14. function endLoading() { //使用Element loading-close 方法
  15. loading.close()
  16. }
  17. let needLoadingRequestCount = 0
  18. export function showFullScreenLoading() {
  19. if (needLoadingRequestCount === 0) {
  20. startLoading()
  21. }
  22. needLoadingRequestCount++
  23. }
  24. export function tryHideFullScreenLoading() {
  25. if (needLoadingRequestCount <= 0) return
  26. needLoadingRequestCount--
  27. if (needLoadingRequestCount === 0) {
  28. endLoading()
  29. }
  30. }
  31. //axios配置和处理
  32. Vue.use(VueAxios, axios);
  33. axios.defaults.baseURL = ''; //项目的的基础url
  34. axios.defaults.headers.common['Authorization'] = ""; //请求token信息配置
  35. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
  36. axios.defaults.timeout = 30000; //在超时前,所有请求都会等待30秒
  37. axios.defaults.withCredentials = true;
  38. // 添加请求拦截器
  39. axios.interceptors.request.use(function (config) {
  40. // 在发送请求之前处理
  41. // 判断token在前台session中是否存在
  42. config.headers.common['Authorization'] = localStorage.getItem('Authorization');
  43. showFullScreenLoading();
  44. return config;
  45. }, function (error) {
  46. // 对请求错误做处理
  47. return Promise.reject(error);
  48. });
  49. // 添加响应拦截器
  50. axios.interceptors.response.use(function (response) {
  51. // 对响应数据处理
  52. tryHideFullScreenLoading();
  53. if (response && (response.status == 200)) {
  54. if (response.data.exceptionMes) {
  55. ElementUI.MessageBox({
  56. type: 'info',
  57. title: response.data.exceptionMes,
  58. message: '网络错误,请稍后再试!'
  59. });
  60. }
  61. return response.data;
  62. }
  63. return response;
  64. }, function (error) {
  65. tryHideFullScreenLoading();
  66. console.log('网络异常');
  67. ElementUI.Message.error("网络异常");
  68. return Promise.reject(error);
  69. });
  70. Vue.prototype.$axios = axios; //定义调用方式
  71. // 针对上传大文件
  72. const instance = axios.create({
  73. baseURL: '',
  74. });
  75. instance.defaults.headers.common['Authorization'] = ""; //请求token信息配置
  76. instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
  77. instance.defaults.timeout = 1000 * 60 * 60; //在超时前,所有请求都会等待
  78. instance.defaults.withCredentials = true;
  79. instance.interceptors.request.use(function (config) {
  80. // 在发送请求之前处理
  81. // 判断token在前台session中是否存在
  82. config.headers.common['Authorization'] = localStorage.getItem('Authorization');
  83. showFullScreenLoading();
  84. return config;
  85. }, function (error) {
  86. // 对请求错误做处理
  87. return Promise.reject(error);
  88. });
  89. // 添加响应拦截器
  90. instance.interceptors.response.use(function (response) {
  91. // 对响应数据处理
  92. tryHideFullScreenLoading();
  93. if (response && (response.status == 200)) {
  94. if (response.data.exceptionMes) {
  95. ElementUI.MessageBox({
  96. type: 'info',
  97. title: response.data.exceptionMes,
  98. message: '网络错误,请稍后再试!'
  99. });
  100. }
  101. return response.data;
  102. }
  103. return response;
  104. }, function (error) {
  105. tryHideFullScreenLoading();
  106. console.log('网络异常');
  107. ElementUI.Message.error("网络异常");
  108. return Promise.reject(error);
  109. });
  110. Vue.use(VueAxios, instance);
  111. Vue.prototype.$instance = instance; //定义调用方式