nebula/plugins/axios.js

29 lines
924 B
JavaScript
Raw Normal View History

2020-06-22 17:13:42 +08:00
import apiConfig from '~/config/api.config'
2020-06-21 21:56:34 +08:00
export default function ({ app: { $axios, $cookies } }) {
2020-06-22 17:13:42 +08:00
$axios.defaults.baseURL = apiConfig.BASE
2020-06-21 21:56:34 +08:00
$axios.defaults.timeout = 30000
$axios.interceptors.request.use(config => {
config.headers['X-Token'] = $cookies.get('token') || ''
config.headers['X-Device-Id'] = $cookies.get('clientId') || ''
config.headers['X-Uid'] = $cookies.get('userId') || ''
return config
})
$axios.interceptors.response.use(response => {
if (/^[4|5]/.test(response.status)) {
return Promise.reject(response.statusText)
}
2020-06-22 17:13:42 +08:00
let message;
if (typeof(response.data.data) !== 'undefined') {
message = response.data.data.message
} else if (typeof(response.data) !== 'undefined') {
message = response.data.message
}
console.log(message);
if (response.data.success) {
return response.data.data
}
2020-06-21 21:56:34 +08:00
return response.data
})
2020-06-19 17:10:44 +08:00
}