long
2021-09-13 5f052d4e17b53d4fe07a87d53a9c112bff3dc852
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 这是一个对http请求出现不同的code的处理
//  import router from '../router/index';
import { Loading } from 'element-ui'
//  import { Promise } from 'core-js';
 
let loading
var currentStates = {
  // 当前的状态集合 可以设置那些是在请求完成之后关闭的loading
  200: true, // 可以在请求之后结束loading
  404: false, // 出现404的时候不取消loading
  502: false // 出现502的时候不取消loading
}
var currentStateCode // 当前的状态
//  todo 补全状态码报错
var httpEventCode = {
  code200(data) {
    // 200拦截
    currentStateCode = 200
    if (data.data.status === 1) {
      // router.replace({path:'/login'})
      // console.log(data.data.errMsg,'errMsg')
 
    }
    // console.log(data,200)
  },
  code502() {
    currentStateCode = 502
    loading.close()
    loading = Loading.service({
      lock: true,
      text: '服务器异常502',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    // 404 拦截
    // console.log(data,404)
  },
  code404() {
    // 404 拦截
    currentStateCode = 404
    loading.close()
    loading = Loading.service({
      lock: true,
      text: '请求服务器404',
      background: 'rgba(0, 0, 0, 0.7)'
    })
  },
  code400() {
    // 400拦截
    // console.log(data,400)
  }
 
}
 
function startRequest(httpObj, requestObj) {
  console.log(requestObj)
  if (requestObj && requestObj.udData && requestObj.udData.noLoading) {
    return
  }
  // console.log('请求前')
  loading = Loading.service({
    lock: true,
    text: '加载中……',
    // background: 'rgba(0, 0, 0, 0.7)'
    background: 'rgba(255,255,255,0.6)'
  })
}
 
function endRequest() {
  // console.log('请求后')
}
 
function concurrentRequests() {
  // 检测一次多个请求全部完成之后触发
  if (currentStates[currentStateCode]) {
    // 只有正确的code才可以被取消loading
    loading && loading.close()
  }
}
 
//  function defindFlow(o) {
//      // 自定义流程与返回数据
//      // o.requestConfig 请求参数
//      // o.res 上一次的返回结果
//      // o.Request 总请求方法
//      return new Promise((resolve, reject) => {
//          //  console.log(o)
//      })
//  }
// defindFlow
export { httpEventCode, startRequest, endRequest, concurrentRequests }