封装http请求

こ雲淡風輕ζ 提交于 2019-12-11 20:32:50
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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!