filter.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import axios from 'axios'
  2. import VueAxios from 'vue-axios'
  3. import Vue from 'vue'
  4. import router from '../router'
  5. import ElementUI, {
  6. Loading
  7. } from 'element-ui';
  8. let loading //定义loading变量
  9. function startLoading() { //使用Element loading-start 方法
  10. loading = Loading.service({
  11. text: '请稍等...',
  12. background: 'rgba(0, 0, 0, 0.2)'
  13. })
  14. }
  15. function endLoading() { //使用Element loading-close 方法
  16. loading.close()
  17. }
  18. let needLoadingRequestCount = 0
  19. export function showFullScreenLoading() {
  20. if (needLoadingRequestCount === 0) {
  21. startLoading()
  22. }
  23. needLoadingRequestCount++
  24. }
  25. export function tryHideFullScreenLoading() {
  26. if (needLoadingRequestCount <= 0) return
  27. needLoadingRequestCount--
  28. if (needLoadingRequestCount === 0) {
  29. endLoading()
  30. }
  31. }
  32. //axios配置和处理
  33. Vue.use(VueAxios, axios);
  34. axios.defaults.baseURL = ''; //项目的的基础url
  35. axios.defaults.headers.common['Authorization'] = ""; //请求token信息配置
  36. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
  37. axios.defaults.timeout = 30000; //在超时前,所有请求都会等待30秒
  38. axios.defaults.withCredentials = true;
  39. let isInvalid1 = false //用于控制登录信息过期后弹窗时间的截流,截流时间为5s
  40. let isInvalid2 = false
  41. // 添加请求拦截器
  42. axios.interceptors.request.use(function (config) {
  43. // 在发送请求之前处理
  44. // 判断token在前台session中是否存在
  45. config.headers.common['Authorization'] = localStorage.getItem('Authorization');
  46. if (config.noLoading === true) { //如果noLoading为true,不加载loading画面
  47. console.log("noLoading");
  48. } else {
  49. showFullScreenLoading();
  50. }
  51. return config;
  52. }, function (error) {
  53. // 对请求错误做处理
  54. return Promise.reject(error);
  55. });
  56. // 添加响应拦截器
  57. axios.interceptors.response.use(function (response) {
  58. // 对响应数据处理
  59. tryHideFullScreenLoading();
  60. if (response && (response.status == 200)) {
  61. if (response.data.exceptionMes) {
  62. ElementUI.MessageBox({
  63. type: 'info',
  64. title: response.data.exceptionMes,
  65. message: '网络错误,请稍后再试!'
  66. });
  67. }
  68. return response.data;
  69. }
  70. return response;
  71. }, function (error) {
  72. tryHideFullScreenLoading();
  73. if (error.response.status == 401) { //token失效,弹框提示确认返回login
  74. //ElementUI.Message.error("用户信息过期,请重新登录");
  75. if (isInvalid1 == false) {
  76. isInvalid1 = true
  77. ElementUI.MessageBox.alert('用户信息过期,请重新登陆', '提示', {
  78. confirmButtonText: '确定',
  79. callback: action => {
  80. // let loginUrl = window.location.origin + '/login'
  81. // window.location.href = loginUrl
  82. router.push({
  83. path: "/login"
  84. })
  85. }
  86. })
  87. setTimeout(() => {
  88. isInvalid1 = false
  89. }, 5000)
  90. }
  91. return Promise.reject(error);
  92. } else {
  93. ElementUI.Message.error("网络异常");
  94. return Promise.reject(error);
  95. }
  96. });
  97. Vue.prototype.$axios = axios; //定义调用方式
  98. // 针对上传大文件
  99. const instance = axios.create({
  100. baseURL: '',
  101. });
  102. instance.defaults.headers.common['Authorization'] = ""; //请求token信息配置
  103. instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
  104. instance.defaults.timeout = 1000 * 60 * 3; //在超时前,所有请求都会等待
  105. instance.defaults.withCredentials = true;
  106. instance.interceptors.request.use(function (config) {
  107. // 在发送请求之前处理
  108. // 判断token在前台session中是否存在
  109. config.headers.common['Authorization'] = localStorage.getItem('Authorization');
  110. if (config.noLoading === true) { //如果noLoading为true,不加载loading画面
  111. console.log("noLoading");
  112. } else {
  113. showFullScreenLoading();
  114. }
  115. return config;
  116. }, function (error) {
  117. // 对请求错误做处理
  118. return Promise.reject(error);
  119. });
  120. // 添加响应拦截器
  121. instance.interceptors.response.use(function (response) {
  122. // 对响应数据处理
  123. tryHideFullScreenLoading();
  124. if (response && (response.status == 200)) {
  125. if (response.data.exceptionMes) {
  126. ElementUI.MessageBox({
  127. type: 'info',
  128. title: response.data.exceptionMes,
  129. message: '网络错误,请稍后再试!'
  130. });
  131. }
  132. return response.data;
  133. }
  134. return response;
  135. }, function (error) {
  136. tryHideFullScreenLoading();
  137. if (error.response.status == 401) { //token失效,弹框提示确认返回login
  138. if (isInvalid2 == false) {
  139. isInvalid2 = true
  140. ElementUI.MessageBox.alert('用户信息过期,请重新登陆', '提示', {
  141. confirmButtonText: '确定',
  142. callback: action => {
  143. // let loginUrl = window.location.origin + '/login'
  144. // window.location.href = loginUrl
  145. router.push({
  146. path: "/login"
  147. })
  148. }
  149. })
  150. setTimeout(() => {
  151. isInvalid2 = false
  152. }, 5000)
  153. }
  154. return Promise.reject(error);
  155. } else {
  156. ElementUI.Message.error("网络异常");
  157. return Promise.reject(error);
  158. }
  159. });
  160. Vue.use(VueAxios, instance);
  161. Vue.prototype.$instance = instance; //定义调用方式
  162. //针对不需要loading画面的实例