Browse Source

统一loading

wolyshaw 4 years ago
parent
commit
50b5a097d9
3 changed files with 21 additions and 38 deletions
  1. 19 0
      src/utils/request-loading.js
  2. 1 20
      src/utils/request.js
  3. 1 18
      src/utils/request2.js

+ 19 - 0
src/utils/request-loading.js

@@ -0,0 +1,19 @@
+import load from '@/utils/loading'
+let times = 0
+
+export function showFullScreenLoading () {
+  if (times === 0) {
+    load.startLoading()
+  }
+  times++
+
+}
+
+export function tryHideFullScreenLoading () {
+  times--
+  if (times <= 0) {
+    load.endLoading();
+    times = 0
+  }
+
+}

+ 1 - 20
src/utils/request.js

@@ -5,7 +5,7 @@ import store from '@/store'
 import { getToken } from '@/utils/auth'
 import cleanDeep from 'clean-deep'
 // import { Loading } from 'element-ui'
-import load from '@/utils/loading'
+import { showFullScreenLoading, tryHideFullScreenLoading } from './request-loading'
 import router from '@/router/index'
 import Vue from 'vue'
 const showMessage = Symbol('showMessage')
@@ -57,25 +57,6 @@ let vue = new Vue()
 //那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
 //声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
 //调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
-let needLoadingRequestCount = 0
-function showFullScreenLoading () {
-  if (needLoadingRequestCount === 0) {
-    load.startLoading()
-  }
-  needLoadingRequestCount++
-
-}
-
-function tryHideFullScreenLoading () {
-  if (needLoadingRequestCount <= 0) return
-  needLoadingRequestCount--
-  if (needLoadingRequestCount === 0) {
-    load.endLoading();
-
-  }
-
-}
-
 // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
 
 

+ 1 - 18
src/utils/request2.js

@@ -6,7 +6,7 @@ import qs from 'querystring'
 import { getToken } from '@/utils/auth'
 import cleanDeep from 'clean-deep'
 // import { Loading } from 'element-ui'
-import load from '@/utils/loading'
+import { showFullScreenLoading, tryHideFullScreenLoading } from './request-loading'
 import router from '@/router/index'
 import Vue from 'vue'
 const showMessage = Symbol('showMessage')
@@ -58,24 +58,7 @@ let vue = new Vue()
 //那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
 //声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
 //调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
-let needLoadingRequestCount = 0
-function showFullScreenLoading () {
-  if (needLoadingRequestCount === 0) {
-    load.startLoading()
-  }
-  needLoadingRequestCount++
-
-}
-
-function tryHideFullScreenLoading () {
-  if (needLoadingRequestCount <= 0) return
-  needLoadingRequestCount--
-  if (needLoadingRequestCount === 0) {
-    load.endLoading();
 
-  }
-
-}
 
 // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';