2020-12-07 23:56:33 +08:00
|
|
|
|
import {Message} from 'element-ui'
|
|
|
|
|
|
2021-02-01 18:12:35 +08:00
|
|
|
|
const Cookie = process.client ? require('js-cookie') : undefined
|
2020-07-03 14:43:14 +08:00
|
|
|
|
export default function ({app, $axios, store, redirect}) {
|
|
|
|
|
$axios.onRequest(config => {
|
2020-08-01 00:41:54 +08:00
|
|
|
|
let token = store.state.oauth?.accessToken;
|
|
|
|
|
if (token) {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
// if (!(config.url.indexOf('console') > -1 || config.url.indexOf('comments') > -1)) {
|
|
|
|
|
// }
|
|
|
|
|
config.headers['Authorization'] = token
|
2020-07-03 14:43:14 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
$axios.onResponse(response => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
//返回数据逻辑处理 比如:error_code错误处理
|
|
|
|
|
let message;
|
|
|
|
|
if (typeof (response.data.data) !== 'undefined') {
|
|
|
|
|
message = response.data.data.message
|
|
|
|
|
} else if (typeof (response.data) !== 'undefined') {
|
|
|
|
|
message = response.data.message
|
|
|
|
|
}
|
|
|
|
|
if (response.data.success) {
|
|
|
|
|
resolve(response.data);
|
|
|
|
|
} else {
|
2021-02-03 09:15:23 +08:00
|
|
|
|
if (response.data.code === '0') {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error(message ? message : '服务异常')
|
2021-02-03 09:15:23 +08:00
|
|
|
|
} else if (response.data.code === '401') {
|
2021-02-01 18:12:35 +08:00
|
|
|
|
Cookie.remove('auth');
|
2020-08-01 00:41:54 +08:00
|
|
|
|
store.commit('setAuth', null);
|
2021-02-03 09:15:23 +08:00
|
|
|
|
window.location.reload()
|
|
|
|
|
} else if (response.data.code === '402') {
|
2021-02-01 18:12:35 +08:00
|
|
|
|
Cookie.remove('auth');
|
2020-08-01 00:41:54 +08:00
|
|
|
|
store.commit('setAuth', null);
|
2021-02-03 09:15:23 +08:00
|
|
|
|
window.location.reload()
|
|
|
|
|
} else if (response.data.code === '404') {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error('操作失败,请稍后再试......')
|
2021-02-03 09:15:23 +08:00
|
|
|
|
} else if (response.data.code === '500') {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error('服务器正在开小差,请稍后再试......')
|
2021-02-01 18:12:35 +08:00
|
|
|
|
} else {
|
|
|
|
|
Message.error(response.data.message);
|
2020-07-03 14:43:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reject(response);
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
$axios.onError(error => {
|
|
|
|
|
const code = parseInt(error.response && error.response.status)
|
|
|
|
|
if (code === 400) {
|
2020-08-01 00:41:54 +08:00
|
|
|
|
redirect('/400');
|
2020-07-03 14:43:14 +08:00
|
|
|
|
} else {
|
2020-08-02 19:24:48 +08:00
|
|
|
|
console.log(error.data);
|
2020-07-03 14:43:14 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|