let ajxjsum = 0;

// 判断是否未定义 或者 null
function IsUndefined(obj) {
    return typeof obj === 'undefined' || obj == null;
}

let tc = null;

function MyPost(url, data, back, badback, completeback) {

    if (IsUndefined(data) || IsUndefined(data.isSwal) || data.isSwal != false) {
        ajxjsum++;
        tc = Swal.fire({
            onClose: (elm) => {
                if (!(IsUndefined(completeback))) {
                    completeback();
                }
            },
            title: '',
            html: !(IsUndefined(data) || IsUndefined(data.title)) ? data.title : '加载中...',
            showConfirmButton: false,
            allowOutsideClick: false,
            showCancelButton: false,
            onBeforeOpen: () => {
                Swal.showLoading()
            }
        })
    }
    try {
        if ((!IsUndefined(data) && !IsUndefined(data.type) && data.type == 1)) {
            $.ajax({
                type: "post",
                url: url,
                cache: false,
                processData: false,
                contentType: false,
                data: !(IsUndefined(data) || IsUndefined(data.data))?data.data : {},
                "success": function (res) {
                    // 已经失去登录信息 刷新界面重新登录
                    if (typeof (res) == 'string' && res.indexOf("<title>BLV RCU | 登录</title>") > -1) {
                        location.reload();
                    }
                    if (!(IsUndefined(back))) {
                        try {
                            back(res);
                        } catch (e) {
                            console.log("bad - err");
                        }
                    }
                },
                "error": function (xmlhttp, errorText) {
                    if (!(IsUndefined(badback))) {
                        try {
                            badback(errorText);
                        } catch (e) {
                            console.log("badback - err");
                        }
                    }
                },
                "complete": function (XMLHttpRequest, status) {
                    if (IsUndefined(data) || IsUndefined(data.isSwal) || data.isSwal != false) {
                        ajxjsum--;
                    }
                    /// 动画不突兀
                    if (ajxjsum == 0) {
                            if (ajxjsum == 0 && tc != null) {
                                tc.close(); 
                            }
                    }
                },
                
            })
            return;
        }
        $.ajax({
            "url": url,
            "type": 'POST',
            "data": !(IsUndefined(data) || IsUndefined(data.data)) ? data.data : {},
            "success": function (res) {
                // 已经失去登录信息 刷新界面重新登录
                if (typeof (res) == 'string' && res.indexOf("<title>BLV RCU | 登录</title>") > -1) {
                    location.reload();
                }
                if (!(IsUndefined(back))) {
                    try {
                        back(res);
                    } catch (e) {
                        console.log(e)
                    }
                }
            },
            "error": function (xmlhttp, errorText) {
                if (!(IsUndefined(badback))) {
                    try {
                        badback(errorText);
                    } catch (e) {

                    }
                }
            },
            "complete": function (XMLHttpRequest, status) {
                if (IsUndefined(data) || IsUndefined(data.isSwal) || data.isSwal != false) {
                    ajxjsum--;
                }
                if (ajxjsum == 0 && tc != null) {
                    tc.close();
                   
                }
            }
        })
    } catch (e) {
        console.log(e)
        ajxjsum--;
        if (!(IsUndefined(badback))) {
            badback(e);
        }
    } finally {
        if (ajxjsum < 0) {
            ajxjsum = 0;
        }
    }
}




