代码拉取完成,页面将自动刷新
import axios from 'axios'
import qs from 'qs'
/**
* Created by 白雨浓 on 17-8-10 上午11:29.
*
* ajax axios 封装
*/
const SUCCESS_CODE = 1;
axios.defaults.baseURL = "/api";
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
const Ajax = {
errHandler (Vue, err) {
if (err.response !== undefined)
Vue.prototype.$message.error(`请求错误: ${JSON.stringify(err.response.status)}`);
else
Vue.prototype.$message.error(err);
},
install(Vue){
const $$get = (path, params) => {
if (params !== undefined) {
let b = true;
for (const key in params) {
if (b) {
path += `?${key}=${params[key]}`;
b = !b;
}
else
path += `&${key}=${params[key]}`
}
}
return new Promise((resolve, reject) => {
axios.get(path).then(res => {
resolve([res.data, res]);
}).catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$post = (path, params) => {
return new Promise((resolve, reject) => {
axios.post(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$put = (path, params) => {
return new Promise((resolve, reject) => {
axios.put(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$patch = (path, params) => {
return new Promise((resolve, reject) => {
axios.patch(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$delete = (path, params) => {
if (params !== undefined) {
let b = true;
for (const key in params) {
if (b) {
path += `?${key}=${params[key]}`;
b = !b;
}
else
path += `&${key}=${params[key]}`
}
}
return new Promise((resolve, reject) => {
axios.delete(path).then(res => {
resolve([res.data, res]);
}).catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$postj = (path, json) => {
return new Promise((resolve, reject) => {
axios.post(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$putj = (path, params) => {
return new Promise((resolve, json) => {
axios.put(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$patchj = (path, json) => {
return new Promise((resolve, reject) => {
axios.patch(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
Vue.prototype.$$ajax = axios;
Vue.prototype.$$get = $$get;
Vue.prototype.$$post = $$post;
Vue.prototype.$$put = $$put;
Vue.prototype.$$patch = $$patch;
Vue.prototype.$$delete = $$delete;
Vue.prototype.$$postj = $$postj;
Vue.prototype.$$putj = $$putj;
Vue.prototype.$$patchj = $$patchj;
}
};
export default Ajax;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。