interceptor.js
根
/
site-m /
main /
interceptor.js
const interceptor = {
//请求拦截
request(options) {
options.headers.token = localStorage.getItem('token');
},
//响应拦截
response(response, options) {
let obj = { isErr: false, data: '' };
if (response.status == '200') {
var json = response.data;
if (json.isErr) {
//如果显示错误信息,弹出错误,否则继续执行
if (options.showMsg) {
options.onError(this, '错误', json.errMsg);
this.$vue.$Notice.error({
title: '错误',
desc: json.errMsg
});
console.error(json.errMsg);
obj.isErr = true;
obj.data = json;
return obj;
} else {
//继续执行,内部处理错误
obj.isErr = false;
obj.data = json;
return obj;
}
} else {
//当现实返回消息时
if (options.showMsg) {
if (json.msg) {
// options.onMessage(this, '消息', json.msg);
this.$vue.$Notice.success({
title: json.msg,
// desc: nodesc ? '' : 'Here is the notification description. Here is the notification description. '
});
}
obj.isErr = false;
obj.data = json.data;
return obj;
}
//当不现实返回消息时
else {
obj.isErr = false;
obj.data = json;
return obj;
}
}
} else {
console.error(response);
if (options.showMsg) {
options.onError(this, '网络错误', '请联系管理员解决');
}
obj.isErr = true;
obj.data = '网络错误';
return obj;
}
return obj;
}
};
export default interceptor;