vue 封装get 和post请求
Vue.prototype.post = function (url, param, callback) {
return new Promise((resolve, reject) => {
var sessionid = localStorage.getItem('sessionid');
Vue.http.post(url, param, { headers: { Authorization: sessionid }, emulateJSON: true }).then((res) => {
if (res.body.success) {
resolve(res.body);
}
if (res.body.code == '403') {
localStorage.clear();
this.$cookiesStore.delCookie('access_token');
this.$router.push('/');
if (typeof (callback) == 'function') {
callback(res)
}
return;
}
if (res.body.code != '200') {
this.$loading(false);
this.$toast(res.body.msg, 2000, 'error');
}
if (typeof (callback) == 'function') {
callback(res)
}
}).catch((res) => {
reject(res.body);
});
});
};
Vue.prototype.get = function (url, param) {
return new Promise((resolve, reject) => {
var sessionid = localStorage.getItem('sessionid');
// url = param ? `${url}?${qsStringify(param)}` : url;
Vue.http.get(url, {
params: param,
headers: {
Authorization: sessionid,
// Accept: 'application/json',
// 'Content-Type': 'application/octet-stream;charset=UTF-8',
},
}).then((res) => {
if (res.status == '200') {
resolve(res.body);
}
}).catch((res) => {
reject(res.body);
});
});
};