import request from 'axios'
import store from '@/store'
import router from '@/router'
export const baseURL = ''
const axios = request.create({
baseURL: baseURL
})
axios.interceptors.request.use(
config => {
const token = store.state.user.profile.token
if (token) config.headers.Authorization = `Bearer ${token}`
return config
},
error => {
return Promise.reject(error)
}
)
axios.interceptors.response.use(
res => {
return res.data
},
error => {
if (error.response && error.response.status === 401) return router.push('/login')
return Promise.reject(error)
}
)
export default options => {
return axios({
method: options.method || 'GET',
url: options.url,
[options.method.toUpperCase() === 'GET' ? 'params' : 'data']: options.data
})
}
|