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