import axios from 'axios'
import { Message } from 'element-ui';
// 设置baseURL
//axios.defaults.baseURL = '/api'
//设置默认请求头
axios.defaults.headers = {
'X-Requested-With': 'XMLHttpRequest'
}
// 设置请求过期时间
axios.defaults.timeout = 10000
//请求拦截器
axios.interceptors.request.use(config => {
config.headers = {
'Content-Type': 'application/json',
'x-token': localStorage.getItem('token') || ''
}
return config
}, error => {
return Promise.reject(error)
})
//响应拦截器即异常处理
axios.interceptors.response.use(response => {
if(response.data.code == 200) {
return response.data
}else{
Message.error('接口' + response.config.url +'数据异常')
return response
}
}, error => {
Message.error(error.message || '服务异常')
return Promise.resolve(error)
})
export default axios