let dom="http://192.168.28.206:8027";
function thenCallBack(res, then, cat) {
console.log(res);
if (res.data.code == 200) {
then.call(this, res);
} else {
this.$message({
message: res.data.msg,
type:"error"
});
cat.call(this,res);
}
}
function post(url, data, then, cat) {
if (data instanceof Function) {
cat = then;
then = data;
data=undefined;
}
if (!cat) {
cat = res => {};
}
var url = dom + url;
this.$axios.post(url, data).then(res => {
thenCallBack.call(this,res,then, cat);
})
}
function get(url, data, then, cat) {
if (data instanceof Function) {
cat = then;
then = data;
data=undefined;
}
var url = dom + url;
this.$axios.get(url, data).then(res => {
thenCallBack.call(this,res,then, cat);
})
}
function doms(url) {
return dom + url;
}
//获取token的接口
// function auth(data) {
// return this.$axios({
// url: "/api/Home/Token",
// method: 'post',
// data: data,
// baseURL: dom,
// }).then(res => {
// localStorage.setItem("access_token", res.data.data.access_token)
// })
// }
function authDel(url, data, then, cat){
//获取access_token
var token = localStorage.getItem("keg_token");
if (data instanceof Function) {
cat = then;
then = data;
data=undefined;
}
if (!cat) {
cat = res => {};
}
if (token) {
return this.$axios({
headers: {
'Authorization': 'Bearer ' + token
},
url: url,
method: 'delete',
data: data,
baseURL: 'http://192.168.28.206:8027',
}).then(res => {
thenCallBack.call(this,res,then, cat);
});
} else {
//异常页面
}
}
function authGet(url, params, then, cat) {
//获取access_token
var token = localStorage.getItem("keg_token");
if (params instanceof Function) {
cat = then;
then = params;
params=undefined;
}
if (!cat) {
cat = res => {};
}
if (token) {
return this.$axios({
headers: {
'Authorization': 'Bearer ' + token
},
url: url,
method: 'get',
params: params,
baseURL: dom,
}).then(res => {
thenCallBack.call(this,res,then, cat);
});
} else {
//异常页面
}
}
function authPost(url, data, then, cat) {
//获取access_token
var token = localStorage.getItem("keg_token");
if (data instanceof Function) {
cat = then;
then = data;
data=undefined;
}
if (!cat) {
cat = res => {};
}
if (token) {
return this.$axios({
headers: {
'Authorization': 'Bearer ' + token
},
url: url,
method: 'post',
data: data,
baseURL: dom,
}).then(res => {
thenCallBack.call(this,res,then, cat);
})
} else {
//异常页面
}
}
function authPostForm(url, data, then, cat) {
if (data instanceof Function) {
authPost(url, data, then, cat);
} else {
//获取access_token
var token = localStorage.getItem("keg_token");
if (!cat) {
cat = res => {};
}
url=stringfy(data);
if (token) {
return this.$axios({
headers: {
'Authorization': 'Bearer ' + token
},
url: url,
method: 'post',
//data: value,
baseURL: dom,
}).then(res => {
thenCallBack.call(this,res,then);
}).catch(cat)
} else {
//异常页面
}
}
}
function stringfy(url,data){
var value = "";
if (data instanceof String) {
value = data;
} else {
for (var i in data) {
value += i + "=" + data[i] + "&";
}
value = value && value.substr(0, value.length - 1);
}
url += url.indexOf("?") > -1 ? ("&" + value) : ("?" + value)
return dom+url;
}
export default {
post,
get,
authGet,
authPost,
doms,
authPostForm,
stringfy,
authDel
}
封装http请求
最新推荐文章于 2024-05-11 17:49:41 发布