2024-08-20 12:11:31 +00:00
|
|
|
/**
|
|
|
|
* Created by Zhang Haijun on 2017/8/24.
|
|
|
|
* axios#request(config)
|
|
|
|
* axios#get(url[, config])
|
|
|
|
* axios#delete(url[, config])
|
|
|
|
* axios#head(url[, config])
|
|
|
|
* axios#options(url[, config])
|
|
|
|
* axios#post(url[, data[, config]])
|
|
|
|
* axios#put(url[, data[, config]])
|
|
|
|
* axios#patch(url[, data[, config]])
|
|
|
|
*/
|
|
|
|
import axios from 'axios'
|
|
|
|
import NProgress from 'nprogress'
|
|
|
|
import qs from 'qs'
|
|
|
|
import 'nprogress/nprogress.css'
|
|
|
|
import { Notification, MessageBox } from 'element-ui'
|
|
|
|
import { getToken } from 'utils/auth'
|
|
|
|
import store from '@cmp/cmp-core/store'
|
|
|
|
import { setToken } from '@cmp/cmp-element'
|
|
|
|
|
|
|
|
const codeMessage = {
|
|
|
|
200: '服务器成功返回请求的数据。',
|
|
|
|
201: '新建或修改数据成功。',
|
|
|
|
202: '一个请求已经进入后台排队(异步任务)。',
|
|
|
|
204: '删除数据成功。',
|
|
|
|
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
|
|
|
|
401: '用户没有权限(令牌、用户名、密码错误)。',
|
|
|
|
403: '用户得到授权,但是访问是被禁止的。',
|
|
|
|
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
|
|
|
|
406: '请求的格式不可得。',
|
|
|
|
410: '请求的资源被永久删除,且不会再得到的。',
|
|
|
|
422: '当创建一个对象时,发生一个验证错误。',
|
|
|
|
500: '服务器发生错误,请检查服务器。',
|
|
|
|
502: '网关错误。',
|
|
|
|
503: '服务不可用,服务器暂时过载或维护。',
|
|
|
|
504: '网关超时。'
|
|
|
|
}
|
|
|
|
const axiosInstance = axios.create({
|
|
|
|
baseURL: '/api',
|
|
|
|
headers: { 'Content-Type': 'application/json', BsmAjaxHeader: true },
|
|
|
|
timeout: 20000,
|
|
|
|
paramsSerializer: params => {
|
|
|
|
return qs.stringify(params, { arrayFormat: 'indices' })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 请求完成回调
|
2024-08-21 01:17:14 +00:00
|
|
|
const finishCallback = function () {
|
2024-08-20 12:11:31 +00:00
|
|
|
NProgress.done()
|
|
|
|
}
|
|
|
|
// 报错处理
|
2024-08-21 01:17:14 +00:00
|
|
|
const handleError = function (response) {
|
2024-08-20 12:11:31 +00:00
|
|
|
if (!response) return // 容错处理
|
|
|
|
let title = `请求错误 ${response.status}: ${response.config.url}`
|
|
|
|
let errorText = codeMessage[response.status] || response.statusText
|
|
|
|
const { data } = response
|
|
|
|
if (typeof data?.message == 'string') {
|
|
|
|
title = data.message
|
|
|
|
errorText = ''
|
|
|
|
}
|
|
|
|
Notification({
|
|
|
|
type: 'error',
|
|
|
|
title,
|
|
|
|
message: errorText
|
|
|
|
})
|
|
|
|
const error = new Error(errorText)
|
|
|
|
error.name = response.status
|
|
|
|
error.response = response
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
axiosInstance.interceptors.request.use(
|
|
|
|
config => {
|
|
|
|
const {
|
|
|
|
headers,
|
|
|
|
headers: { options = {} }
|
|
|
|
} = config
|
|
|
|
NProgress.start()
|
|
|
|
if (config.method === 'get') {
|
|
|
|
// 清除get缓存
|
|
|
|
config.url = `${config.url}?t=${new Date().getTime()}`
|
|
|
|
} else if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
|
|
config.data = qs.stringify(config.data || {})
|
|
|
|
}
|
|
|
|
config.headers.token = getToken()
|
|
|
|
delete config.headers.options
|
2024-08-21 01:17:14 +00:00
|
|
|
config.options = options
|
2024-08-20 12:11:31 +00:00
|
|
|
return config
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
axiosInstance.interceptors.response.use(
|
|
|
|
data => {
|
|
|
|
// const requestKey = getRequestIdentify(data.config);
|
|
|
|
// removePending(requestKey);
|
|
|
|
finishCallback()
|
2024-08-21 01:17:14 +00:00
|
|
|
const {
|
|
|
|
data: responseData,
|
|
|
|
config: { options },
|
|
|
|
headers: { token }
|
|
|
|
} = data
|
|
|
|
if (token) setToken(token)
|
2024-08-20 12:11:31 +00:00
|
|
|
if (!responseData.success) {
|
|
|
|
switch (responseData.status) {
|
|
|
|
case '402':
|
|
|
|
store.dispatch('permission/ResetRoutes', false)
|
|
|
|
location.href = '/license'
|
|
|
|
break
|
|
|
|
case '401':
|
|
|
|
case '509':
|
|
|
|
store.dispatch('permission/ResetRoutes')
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if (!options.ignoreError) {
|
|
|
|
Notification({
|
|
|
|
message: responseData.message || responseData.data,
|
|
|
|
type: 'error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return responseData
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
finishCallback()
|
|
|
|
handleError(error.response)
|
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
export default axiosInstance
|