封装http请求

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值