You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

76 lines
1.8 KiB

import axios from 'axios'
import { Notification } from 'element-ui'
import store from '../store'
import { showLoading, hideLoading } from './apiHelper'
// 创建axios实例
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // api的base_url
timeout: 40000, // 请求超时时间2
withCredentials: true
})
// request拦截器
service.interceptors.request.use(config => {
showLoading()
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = token
}
return config
}, error => {
// Do something with request error
hideLoading()
console.error(error) // for debug
Promise.reject(error)
})
// respone拦截器
service.interceptors.response.use(
response => {
hideLoading()
const res = response.data
if (res.code === 1) {
return res.data
} else if (res.code === 20001) {
Notification({
showClose: true,
message: res.message,
type: 'error',
duration: 500,
onClose: () => {
store.dispatch('FedLogOut').then(() => {
location.reload()// 为了重新实例化vue-router对象 避免bug
})
}
})
return Promise.reject('未登录')
} else {
let msg = ''
if (res.message) {
msg = res.message
} else if (res.msg) {
msg = res.msg
} else {
msg = res.returnMsg
}
Notification({
message: msg,
type: 'error',
duration: 3 * 1000
})
return Promise.reject(res)
}
},
error => {
hideLoading()
console.error('error', error.response)// for debug
Notification({
message: error.response.data && error.response.data.message ? error.response.data.message : error.message,
type: 'error',
duration: 3 * 1000
})
return Promise.reject(error)
}
)
export default service