请求 - Vue

针对请求进行了简易处理,初识 Vue 还望大家多多指教  :)

其中引用的 Toast 是一个基于 Vant 的组件,用于 loading 和提示语。

import axios from "axios";
import {Toast} from "vant";

export default {
    GetHttpRequestDataWithDefault(url, isFlag, isJSON, parameter) {
        /*
        * Get 请求(书城业务模块)
        *
        * @param url        接口链接
        * @param parameter  请求参数
        * @param isFlag     发起请求时是否加载等待样式提示框(默认显示,显示:true & 不显示:false)
        * @param isJSON     发起请求时数据格式是否转换为 JSon(默认不转JSON,转换:true & 不转换:false)
        * */
        isFlag = isFlag || true;
        if (isFlag) {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 0
            });
        }

        parameter = parameter || {};
        isJSON = isJSON || false;
        if (isJSON) {
            JSON.stringify(parameter);
        }

        axios.defaults.timeout = 15000;
        axios.get(url, parameter).then((res) => {
            let result = null;

            console.log(url);
            if (res.data) {
                result = res.data;
                console.log(result);

                parameter.success && parameter.success(result);
                Toast.clear();
            }
            else {
                Toast('网络异常');
            }
        }).catch(function (error) {
            if (parameter.catch) {
                parameter.catch && parameter.catch(error);
                Toast.clear();
            }
            else {
                console.log(error);
                Toast(error);
            }
        });
    },
    GetHttpRequestDataWithDefaultCheck(url, isFlag, isJSON, parameter) {
        /*
        * Get 请求
        *
        * @param parameter 请求参数
        * @param isFlag     发起请求时是否加载等待样式提示框(默认显示,显示:true & 不显示:false)
        * @param isJSON     发起请求时数据格式是否转换为 JSon(默认不转JSON,转换:true & 不转换:false)
        * */
        isFlag = isFlag || true;
        if (isFlag) {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 0
            });
        }

        parameter = parameter || {};

        isJSON = isJSON || false;
        if (isJSON) {
            JSON.stringify(parameter),
                {headers:{'Content-Type':'application/json'}}
        }

        axios.defaults.timeout = 15000;
        axios.get(url, parameter).then((res) => {
            let result = null;

            if (res.data) {
                result = res.data;
                // result = JSON.parse(res.data);// JSON 处理
                console.log(url);
                console.log(result);

                if ('0' === result.state) {
                    parameter.success && parameter.success(result);
                    Toast.clear();
                }
                else {
                    if (result.errorMessage == null) {
                        Toast('服务异常');
                    }
                    else {
                        Toast(result.errorMessage);
                    }
                }
            }
            else {
                Toast('网络异常');
            }
        }).catch(
            error => {
                if (parameter.catch) {
                    parameter.catch && parameter.catch(error);
                    Toast.clear();
                }
                else {
                    console.log(parameter.url);
                    console.log(error);
                    Toast(error.message);
                }
            }
        )
    },
    GetHttpRequestDataWithBest(parameter, isFlag, isJSON) {
        /*
        * Get 请求
        *
        * @param url        接口链接
        * @param parameter  请求参数
        * @param isFlag     发起请求时是否加载等待样式提示框(默认显示,显示:true & 不显示:false)
        * @param isJSON     发起请求时数据格式是否转换为 JSon(默认不转JSON,转换:true & 不转换:false)
        * */
        isFlag = isFlag || true;
        if (isFlag) {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 0
            })
        }

        parameter = parameter || {};
        isJSON = isJSON || false;
        if (isJSON) {
            JSON.stringify(parameter);
        }

        axios.defaults.timeout = 15000;
        axios.get(parameter.url, parameter).then(function (res) {
            let result = null;
            console.log(parameter.url);

            if (res.data) {
                result = res.data;
                console.log(result);

                parameter.success && parameter.success(result);
                Toast.clear();
            }
            else {
                Toast('网络异常');
            }

        }).catch(function (error) {
            console.log(error);

            if (parameter.catch) {
                parameter.catch && parameter.catch(error);
                Toast.clear();
            }
            else {
                Toast(error.message);
            }
        })
    },
    PostHttpRequestDataWithCheck(url, isFlag, isJSON, parameter) {
        /*
        * Post 请求
        *
        * @param url        接口链接
        * @param parameter  请求参数
        * @param isFlag     发起请求时是否加载等待样式提示框(默认显示,显示:true & 不显示:false)
        * @param isJSON     发起请求时数据格式是否转换为 JSon(默认不转JSON,转换:true & 不转换:false)
        * */
        isFlag = isFlag || true;
        if (isFlag) {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 0
            });
        }

        parameter = parameter || {};// 判空

        isJSON = isJSON || false;
        if (isJSON) {
            JSON.stringify(parameter),
                {headers:{'Content-Type':'application/json'}}
        }

        console.info(url);
        console.log(parameter);
        axios.defaults.timeout = 15000;
        axios.post(url, parameter).then(function (response) {
            // console.info(response.data);
            let result = null;
            if (response.data) {
                result = response.data;
                console.info(result);

                if ('0' == result.state) {
                    /*
                    * 接口状态 ture: 0 & false:1
                    * */
                    parameter.success && parameter.success(result);
                    Toast.clear();
                }
                else {
                    if (result.msg) {
                        Toast(result.msg);
                    }
                    else {
                        Toast('服务异常');
                    }
                }
            }
            else {
                Toast('网络异常');
            }
        }).catch(
            error => {
                if (parameter.catch) {
                    parameter.catch && parameter.catch(error);
                    Toast.clear();
                }
                else  {
                    console.log(error);
                    Toast(error.message);
                }
            }
        )
    },
    PostHttpRequestData(url, isFlag, isJSON, parameter) {
        /*
        * Post 请求
        *
        * @param url        接口链接
        * @param parameter  请求参数
        * @param isFlag     发起请求时是否加载等待样式提示框(默认显示,显示:true & 不显示:false)
        * @param isJSON     发起请求时数据格式是否转换为 JSon(默认不转JSON,转换:true & 不转换:false)
        * */
        isFlag = isFlag || true;
        if (isFlag) {
            Toast.loading({
                mask: true,
                message: '加载中...',
                duration: 0
            });
        }

        parameter = parameter || {};// 判空

        isJSON = isJSON || false;
        if (isJSON) {
            JSON.stringify(parameter),
                {headers:{'Content-Type':'application/json'}}
        }

        axios.defaults.timeout = 15000;
        axios.post(url, parameter).then(function (response) {
            // console.info(response.data);
            let result = null;

            console.info(url);
            if (response.data) {
                result = response.data;
                console.info(result);

                if ('0' == result.state) {
                    /*
                    * ture: 0 & false:1
                    * */
                    parameter.success && parameter.success(result);
                    Toast.clear();
                }
                else {
                    if (result.errorMessage == null) {
                        Toast('服务异常');
                    }
                    else {
                        Toast(result.errorMessage);
                    }
                }
            }
            else {
                Toast('网络异常');
            }
        }).catch(
            error => {
                if (parameter.catch) {
                    parameter.catch && parameter.catch(error);
                    Toast.clear();
                }
                else  {
                    console.log(error);
                    Toast(error.message);
                }
            }
        )
    }
}

 

______

以上便是此次小结的内容,有什么不足还望大家多多指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值