luobisheng
2022-11-22 35c30ecf074b292e955d96df7a713a2166daff0e
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
import axios from 'axios'
import {handleRequest, handleResponseFail, handleResponseSuccess} from './interceptor-handler'
import {showFullScreenLoading} from '@/utils/loading'
 
// URL 记录器
import urlRecorder from './url-recorder'
 
const axiosInstance = axios.create({
  timeout: 1000 * 60 * 5
})
 
axiosInstance.interceptors.request.use(handleRequest)
axiosInstance.interceptors.response.use(handleResponseSuccess, handleResponseFail)
/**
 * 在 get 请求的 url 后面加个随机参数,以防浏览器缓存请求
 * @param {string} url
 * @return {string}
 */
function addVersion(url) {
  return url.includes('?') ? `${url}&v=${Date.now()}` : url;
}
 
export default {
  // 默认的异常处理方法,会传入完整的data对象,可以在这里弹提示框
  defaultErrorHandler: null,
  /**
   * 通过get发送并接收json格式的数据(get发的本来就是json格式)。
   * 并统一处理常见的错误
   * @param {string} url
   * @param {object?} params={}
   * @param {boolean?} throwError 是否不使用默认的异常处理方法,而把异常抛出来
   * @param {int?} timeout 超时时间,默认10秒
   * @return {Promise} 返回一个promise对象。其中then方法传递回包中的data数据;catch事件则传递整个回包,其参数为{data:{},status{code:123,message:'xxx'}}
   */
  get(url, params = {}, throwError, timeout) {
    const config = {
      method: 'GET',
      url: addVersion(url),
      params,
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: this.withCredentials,
      timeout: timeout
    }
    if (params.showLoading === undefined) {
      showFullScreenLoading()
    }
    return axiosInstance(config)
  },
 
  /**
   * 通过post发送数据,使后端直接收到json格式的数据。并统一处理常见的错误
   * @param {string} url
   * @param {object?} data={}
   * @param {object} params={}
   * @param {boolean?} throwError 是否不使用默认的异常处理方法,而把异常抛出来
   * @return {Promise} 返回一个promise对象。其中then方法传递回包中的data数据;catch事件则传递整个回包,其参数为{data:{},status{code:123,message:'xxx'}}
   */
  post(url, data = {}, params= {}, throwError) {
    const config = {
      method: 'POST',
      url,
      params,
      data: JSON.stringify(data),
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: this.withCredentials
    }
    urlRecorder.add(config)
    if (data.showLoading === undefined) {
      showFullScreenLoading()
    }
    return axiosInstance(config)
  },
  // PUT更新数据
  put(url, data = {}, throwError) {
    const config = {
      method: 'PUT',
      url,
      data: JSON.stringify(data),
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: this.withCredentials
    }
    urlRecorder.add(config)
    if (data.showLoading === undefined) {
      showFullScreenLoading()
    }
    return axiosInstance(config)
  },
  // DELETE更新数据
  delete(url, params = {}, data = {}, throwError) {
    const config = {
      method: 'delete',
      params,
      url,
      data: JSON.stringify(data),
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: this.withCredentials
    }
    urlRecorder.add(config)
    if (data.showLoading === undefined) {
      showFullScreenLoading()
    }
    return axiosInstance(config)
  },
  /**
   * 通过表单下载文件。并统一处理常见的错误
   * @param {string} url
   * @param params 传参
   * @param {Object?} data 上传进度回调,参数为event
   * @param {Function?} throwError 是否不使用默认的异常处理方法,而把异常抛出来
   * @return {Promise} 返回一个promise对象
   */
  downloadFile(url, params= {}, data = {}, throwError) {
    const config = {
      method: 'POST',
      url,
      params,
      data: JSON.stringify(data),
      responseType: 'arraybuffer',
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: this.withCredentials
    }
    urlRecorder.add(config)
    if (data.showLoading === undefined) {
      showFullScreenLoading()
    }
    return axiosInstance(config)
  },
  /**
   * 通过表单post上传文件并接收json格式的数据。并统一处理常见的错误
   * @param {string} url
   * @param {FormData|object} formElem FormData对象,或form Dom元素,其中需要含有一个name为files的选择文件的input元素
   * @param {Function?} onUploadProgress 上传进度回调,参数为event
   * @param {boolean?} throwError 是否不使用默认的异常处理方法,而把异常抛出来
   * @param {int?} timeout 超时时间,默认50秒
   * @return {Promise} 返回一个promise对象。其中then方法传递回包中的data数据;catch事件则传递整个回包,其参数为{data:{},status{code:123,message:'xxx'}}
   */
  uploadFile(url, formElem, onUploadProgress, throwError) {
    return axiosInstance({
      method: 'POST',
      url,
      data: formElem instanceof FormData ? formElem : new FormData(formElem),
      onUploadProgress,
      errorHandler: (!throwError && this.defaultErrorHandler) || null,
      headers: {
        'Content-Type': 'multipart/form-data'
      },
    })
  }
}