import axios from 'axios'
import state from '../store/state'
import { Message } from 'element-ui'
// 服务器的域名/ip
const domain = 'http://192.168.0.108/'
const $http = axios.create({
baseURL: domain,
timeout: 5000
})
// request请求拦截器
$http.interceptors.request.use(
config => {
// Loading.service({text: '加载中...'}) 这是全局的loading
if (state.cjtzAuthorization) {
Object.assign(config.headers, { 'Authorization': state.cjtzAuthorization })
}
return config
},
error => {
Promise.reject(error)
}
)
// respone响应拦截器
$http.interceptors.response.use(
response => {
// Loading.service().close() 响应成功,关闭全局的loading
return response
},
error => {
// Loading.service().close() 响应错误,关闭全局的loading
Message({
message: '服务器繁忙,请稍后重试',
type: 'error',
duration: 4 * 1000
})
return Promise.reject(error)
}
)
export default $http
网友评论