封装 http Get Post请求

let URL = '固定URL';

httpGet(url, callback) {
    axios.get(URL + url, {
    }).then((data) => {
        if (data.data.status == 1) {
            callback(data.data)
        } else {
            if (data.data.error) {
                message.warning(data.data.error);
            } else {
                message.warning('网络异常,将重新刷新.');
                let t = setTimeout(() => {
                    window.location.reload();
                    clearTimeout(t);
                }, 1000);
            }
        }
    }).catch((err) => {
        callback(false);
        console.error(err);
    });
}

/**
 * post一个请求
 * @param {*} url
 * @param {*} callback
 */
httpPost(url, data, callback) {
    axios.post(URL + url, {
        data: data,
    }).then((data) => {
        if (data.data.status == 1) {
            callback(data.data)
        } else {
            if (data.data.error) {
                message.warning(data.data.error);
            } else {
                message.warning('网络异常,将重新刷新.');
                let t = setTimeout(() => {
                    window.location.reload();
                    clearTimeout(t);
                }, 1000);
            }
        }
    }).catch((err) => {
        callback(0);
        console.error(err);
    });
}

猜你喜欢

转载自blog.csdn.net/weixin_43271750/article/details/84328908