//const API_ADDRESS = ‘xxx’;
const DIY_TOAST_CODES = [“20000”];
var Api = {};
Api.install = function (Vue, options) {
if (!Vue.axios) {
console.log(‘Api需要引入axios插件…’);
return
}
var _this = Vue.prototype;
Vue.axios.interceptors.response.use(function (res) {
// 判断请求是否成功
if (DIY_TOAST_CODES.indexOf(res.data.code) != -1) {
// 未登录不用提示。
return res.data;
}
return res.data
}, function (err) {
console.log(‘获取数据失败’);
return Promise.reject(err)
})
Vue.axios.defaults.headers = {
‘Content-Type’: ‘application/json; charset=UTF-8’
};
// 接口列表
Vue.prototype.Api = {
setHeaders: function (key, value) {
Vue.axios.defaults.headers[key] = value;
},
isSuccess: function (rsp) {
return rsp.code == 0;
},
isSuccessWithErrorToast: function (rsp) {
let success = this.isSuccess(rsp);
if (!success) {
alert(rsp.msg || 获取数据失败
);
}
return success;
},
//接口名称
xxx(rsp) {
return Vue.axios.post(${API_ADDRESS}/common/xxx
, rsp);
},
再main.js import Api from “./service/api”