瀏覽代碼

Merge branch 'master' of http://git.ycjcjy.com/ershoufang/mp-agent into master

zlisen 4 年之前
父節點
當前提交
ff66760b15
共有 2 個文件被更改,包括 18 次插入2 次删除
  1. 1
    1
      src/store/models/loading.js
  2. 17
    1
      src/utils/request/index.js

+ 1
- 1
src/store/models/loading.js 查看文件

1
-import { reactive } from "@vue/reactivity"
1
+import { reactive } from "vue"
2
 
2
 
3
 export default () => {
3
 export default () => {
4
   const loading = reactive({})
4
   const loading = reactive({})

+ 17
- 1
src/utils/request/index.js 查看文件

1
 import axios from 'axios'
1
 import axios from 'axios'
2
+import { useModel } from '@zjxpcyc/vue-tiny-store'
2
 
3
 
3
 // request 拦截器
4
 // request 拦截器
4
 const requestInterceptor = config => {
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
   return config
13
   return config
6
 }
14
 }
7
 
15
 
9
 const responseInterceptor = response => {
17
 const responseInterceptor = response => {
10
   const { data, headers } = response
18
   const { data, headers } = response
11
   const contentType = headers['Content-Type'] || headers['content-type'] || ''
19
   const contentType = headers['Content-Type'] || headers['content-type'] || ''
20
+  
21
+  if (response.config.setLoading) {
22
+    response.config.setLoading(false)
23
+  }
12
 
24
 
13
   if (/json/.test(contentType)) {
25
   if (/json/.test(contentType)) {
14
     if ('0' === data.code) {
26
     if ('0' === data.code) {
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
   if (error.response) {
45
   if (error.response) {
30
     console.error(`[${type}]:`)
46
     console.error(`[${type}]:`)
31
     console.error(error.response)    
47
     console.error(error.response)