张延森 4 лет назад
Родитель
Сommit
4bab6a6f5b
1 измененных файлов: 17 добавлений и 1 удалений
  1. 17
    1
      src/utils/request/index.js

+ 17
- 1
src/utils/request/index.js Просмотреть файл

@@ -1,7 +1,15 @@
1 1
 import axios from 'axios'
2
+import { useModel } from '@zjxpcyc/vue-tiny-store'
2 3
 
3 4
 // request 拦截器
4 5
 const requestInterceptor = config => {
6
+  if (config.loadingId) {
7
+    const { setLoading } = useModel('loading')
8
+    
9
+    config.setLoading = loading => setLoading(config.loadingId, loading)
10
+    config.setLoading(true)
11
+  }
12
+
5 13
   return config
6 14
 }
7 15
 
@@ -9,6 +17,10 @@ const requestInterceptor = config => {
9 17
 const responseInterceptor = response => {
10 18
   const { data, headers } = response
11 19
   const contentType = headers['Content-Type'] || headers['content-type'] || ''
20
+  
21
+  if (response.config.setLoading) {
22
+    response.config.setLoading(false)
23
+  }
12 24
 
13 25
   if (/json/.test(contentType)) {
14 26
     if ('0' === data.code) {
@@ -25,7 +37,11 @@ const responseInterceptor = response => {
25 37
 }
26 38
 
27 39
 // 错误处理
28
-const handleError = type => error => {
40
+const handleError = type => error => {  
41
+  if (error.config.setLoading) {
42
+    error.config.setLoading(false)
43
+  }
44
+
29 45
   if (error.response) {
30 46
     console.error(`[${type}]:`)
31 47
     console.error(error.response)