nebula/plugins/axios.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

import {Message} from 'element-ui'
2020-07-03 14:43:14 +08:00
export default function ({app, $axios, store, redirect}) {
$axios.onRequest(config => {
2022-01-14 10:38:55 +08:00
let fingerprint = store.state.fingerprint;
if (fingerprint) {
config.headers['fingerprint'] = fingerprint
}
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 {
if (response.data.code === 0) {
Message.error(message ? message : '服务异常')
} else if (response.data.code === 401) {
2022-10-27 23:22:46 +08:00
app.$auth.logout()
} else if (response.data.code === 402) {
2022-10-27 23:22:46 +08:00
app.$auth.strategy.token.reset()
} else if (response.data.code === 404) {
Message.error('操作失败,请稍后再试......')
} else if (response.data.code === 500) {
Message.error('服务器正在开小差,请稍后再试......')
} else {
Message.error(response.data.message);
2020-07-03 14:43:14 +08:00
}
}
reject(response);
})
});
2022-10-27 23:22:46 +08:00
2020-07-03 14:43:14 +08:00
$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 {
console.log(error.data);
2020-07-03 14:43:14 +08:00
}
})
}