派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-05-30 a312e0dd96d8f7e96fb3341f1a55561b12394405
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Vue from 'vue'
import axios from 'axios'
import * as $CONST from '@/utils/constant'
import * as $T from '@/utils/tools'
// import $store from '@/store'
 
/**
 * 定义传入参数数据格式
 *
 * === 删除数据 ===
 * {
 *    key:['dataId1','dataId2']
 * }
 *
 */
 
/**
 * 定义返回的json数据格式
 *
 * === 分页数据 ===
 * {
 *    code:200,            --- 目前框架认为0和200都是成功状态
 *    message:'按业务自定义',
 *    data:{
 *      records:[{...},{...}],   --- 可按自己业务替换属性
 *      total:xx,          --- 可按自己业务替换属性
 *      size:xx,           --- 可按自己业务替换属性
 *      current:xx,        --- 可按自己业务替换属性
 *      pages:xx           --- 可按自己业务替换属性
 *    },
 * }
 *
 * === 列表数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data:[{..},{..}...]
 * }
 *
 * === 一条数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data:{...}
 * }
 *
 * === 无返回数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data: null
 * }
 *
 * === 树形结构 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义'
 *    data:[{
 *      prop:xx,
 *      ...
 *      children: [
 *         prop:xx,
 *         ...
 *         children:[...]
 *      ]
 *    },
 *    {
 *      prop:xx,
 *      ...
 *      children: [
 *         prop:xx,
 *         ...
 *         children:[...]
 *      ]
 *    }]
 * }
 *
 */
 
// 创建axios实例
const Service = axios.create({
  timeout: 1000 * 30
})
 
const CancelToken = axios.CancelToken
 
/*
 function showDebugInfo(config) {
 console.group('%cMethod::' + config.method + '::Url::' + config.url, 'color:red;font-size:12px;')
 console.log('%c保存/更新/查看场合,传入的参数::', 'font-size:12px;')
 console.log(config.hasOwnProperty('params') ? JSON.stringify(config.params) : JSON.stringify(config.data))
 console.groupEnd()
 }
 
 function filterOauthData(config) {
 let url = config.url
 if (config.method === 'post' && $t.isExistOauthApi(url)) {
 // post请求的场合下,追加角色名称
 }
 
 if (config.method === 'get' && $t.isExistOauthApi(url)) {
 // get请求的场合下,追加角色名称
 if (config.hasOwnProperty('params')) {
 config.params['userCode'] = ''
 } else {
 config['params'] = {}
 config.params['userCode'] = ''
 }
 }
 return config
 } */
 
// request拦截器
Service.interceptors.request.use(
  config => {
    // 临时追加
    //   if ($store.state.user.account) {
    //     config.headers.account = $store.state.user.account
    //   }
    const copyConfig = Object.assign({}, config)
    // copyConfig = filterOauthData(copyConfig)
    // showDebugInfo(copyConfig)
    copyConfig.cancelToken = new CancelToken((c) => {
      // 页面失效场景,取消padding中请求
      Vue.prototype.$cancels.push(c)
      // todo 快速双击取消重复请求
      // 重复请求场景,取消掉上一个未padding中请求
    })
    return copyConfig
  },
  error => {
    Promise.reject(error)
  })
 
// respone拦截器
Service.interceptors.response.use(
  response => {
    const res = response.data
    /* if (Number(res.code) !== 200 && Number(res.code) !== 0) {
      $T.warning(res.message)
      return Promise.reject(res.message)
    } else {
      return res
    } */
    return res
  },
  error => {
    if (error.message && error.message.includes('timeout')) {
      $T.fail($CONST.MSG_SYS_TIME_OUT)
      return Promise.reject(error)
    }
 
    if (!error.response) {
      $T.fail($CONST.MSG_SYS_ERR)
      return Promise.reject(error)
    }
 
    switch (error.response.status) {
      // http status handler
      case 404:
        $T.fail($CONST.MSG_SYS_404)
        break
      case 500:
        $T.fail($CONST.MSG_SYS_ERR)
        break
      case 503:
        $T.fail($CONST.MSG_SYS_503)
        break
    }
 
    return Promise.reject(error)
  }
)
 
const $http = {}
$http.$service = Service
$http.$axios = axios
$http.get = function (url, params = {}) {
  if ($T.isInlineParams(url)) {
    // 严格遵循restful标准的url的场景
    url = $T.replaceUrlParams(url, params)
    params = {}
  }
  return Service({
    url: url,
    method: 'get',
    params: $T.filterParams(params)
  })
}
$http.post = function (url, data = {}, setHeaders) {
  return Service({
    url: url,
    method: 'post',
    data: data,
    headers: setHeaders || { 'Content-Type': 'application/json; charset=UTF-8' }
  })
}
$http.put = function (url, data = {}) {
  return Service({
    url: url,
    method: 'put',
    data: data
  })
}
$http.delete = function (url, data = {}) {
  return Service({
    url: url,
    method: 'delete',
    data: data
  })
}
export default $http