import axios from 'axios';
import qs from 'qs'
import Vue from 'vue';
//意思是携带cookie信息,保持session的一致性
axios.defaults.withCredentials = true;
//生产环境,不在控制台打印提示信息
Vue.config.productionTip = false;
//默认请求时间3s
axios.defaults.timeout = 3000;
//post 请求
let $post = (url, params, headers) => {
// 设置请求头
let _params = params;//根据请求头 设置参数类型
if (headers === undefined) {
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
_params = qs.stringify(params);
} else {
axios.defaults.headers.post['Content-Type'] = 'application/json; charset=UTF-8';
}
return new Promise((resolve, reject) => {
axios.post(url, _params, {indices: false}).then((res) => {
resolve(res);// 返回请求成功的数据 data
}).catch((err) => {
reject(err);//返回失败的错误信息
});
});
};
//get 请求
let $get = (url, params) => {
return new Promise((resolve, reject) => {
axios.get(url, {params: params}).then((res) => {
resolve(res.data);
}).catch((err) => {
reject(err);
});
});
};
//delete 请求
let $del = (url, params) => {
return new Promise((resolve, reject) => {
axios.delete(url, {params: params}).then((res) => {
resolve(res.data);
}).catch((err) => {
reject(err);
});
});
};
export {
$get,
$post,
$del
}