axios 拦截器
作者:chrispy
import axios from 'axios'
import store from '../index'
import { Loading, Message } from 'element-ui'
// let origin = window.location.origin
let responseStep = 0
let loadingInstance = null
// 添加请求拦截器
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
} else if (status === 400) {
Message({
type: 'error',
dangerouslyUseHTMLString: true,
message: `错误请求 — 请求中有语法问题,或不能满足请求。<br /><br /> 报错接口 — ${response.config.url}`
})
responseStep = 0
loadingInstance.close()
} else if (status === 500) {
Message({
type: 'error',
dangerouslyUseHTMLString: true,
message: `内部错误 — 因为意外情况,服务器不能完成请求。<br /><br /> 报错接口 — ${response.config.url}`
})
responseStep = 0
loadingInstance.close()
}
}
}, (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)
})
let base = '/iCloud-rest'
export const projectsProject = params => { return axios.get(`${base}/projects/project?tenantId=${params}`, {isLoading: true}).then(res => res.data) }
// 查询新闻标题和id,筛选条件使用
export const getArticleParams = params => { return axios.get(`/iCloud-rest/fission-admin/api/getArticleParams/${params}`).then(res => res.data) }
// 线索趋势 折线图
export const postSalesClueStatistics = params => { return axios.post(`/iCloud-rest/fission-admin/api/getSalesClueStatistics`, params).then(res => res.data) }