拦截器 api
作者:chrispy
// 添加请求拦截器
axios.defaults.withCredentials = true
axios.defaults.timeout = 10000
axios.interceptors.request.use((config) => {
responseStep = responseStep + 1
const isLoading = config.url.indexOf('&isLoading') > -1
if (!isLoading) {
loadingInstance = Loading.service(store.state.LoadingoOptions)
}
return config
}, (error) => {
responseStep = 0
loadingInstance.close()
store.state.isLoading = false
return Promise.reject(error)
})
// 添加响应拦截器
axios.interceptors.response.use((response) => {
if (response && response.data) {
let status = parseInt(response.data.status)
if (status === 200) {
responseStep = responseStep - 1
if (!responseStep) {
responseStep = 0
loadingInstance.close()
}
return response
}
}
}, (error) => {
if (error && error.response && error.response.status === 504) {
Message.error('错误代码504,您可能断网了。')
}
if (error && error.response && error.response.data) {
let status = parseInt(error.response.data.status)
if (status === 401) {
Message.error('未登录')
}
}
// 对响应错误做点什么
return Promise.reject(error)
})