12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- const header = {
- }
- window.ajaxTimeout = 20000
- export default async (
- url = '',
- data = {},
- type = 'GET',
- params = {},
- headers = header,
- onUploadProgress
- ) => {
- type = type.toLowerCase()
- const obj = {
- method: type,
- baseURL: '',
- url: url,
- data,
- params,
- timeout: window.ajaxTimeout,
- headers,
- onUploadProgress
- }
- if (onUploadProgress && typeof onUploadProgress === 'function') {
- obj.onUploadProgress = onUploadProgress
- }
- // 请求拦截器
- axios.interceptors.request.use(
- config => {
- return config
- },
- error => {
- // 错误抛到业务代码
- return Promise.reject(new Error('服务器异常,请联系管理员!'))
- }
- )
- // 添加响应拦截器
- axios.interceptors.response.use(
- response => {
- return response
- },
- error => {
- return Promise.reject(error.response.data)
- }
- )
- return new Promise((resolve, reject) => {
- axios(obj)
- .then(response => {
- resolve(response.data)
- })
- .catch(res => {
- reject(res)
- })
- })
- }
|