var envEnum = {
    envTest: "test",
    envProduce: "produce"
}
var envCurrent = location.href.includes("https") ? envEnum.envProduce : envEnum.envTest;
var theHostIp = "http://127.0.0.1:11332";
if (envCurrent == envEnum.envProduce) {
    theHostIp = "https://xmjssvr.cn";
}
window.myConfig = {
    serverURL: theHostIp + "/SmartBowBusinessServer"
};
window.authToken = function() {
    return localStorage.getItem("authToken") ?? "";
};
window.authTokenKeep = function() {
    return localStorage.getItem("authTokenKeep") ? true : false;
}
window.saveLoginAuth = function(a, b) {
    localStorage.setItem("authToken", a)
    if (b) {
        localStorage.setItem("authTokenKeep", "true");
    } else {
        localStorage.removeItem("authTokenKeep");
    }
}
window.clearLoginAuth = function() {
    localStorage.removeItem("authToken");
    localStorage.removeItem("authTokenKeep");
}
window.requestAPI = function(obj) {
    $.ajax({
        headers: {
            "x-access-token": window.authToken()
        },
        type: obj.type ? obj.type : "GET",
        url: window.myConfig.serverURL + obj.url,
        data: obj.data ? obj.data : {},
        success: (data) => {
            if (obj.success) {
                obj.success(data);
            }
        },
        error: (err) => {
            if (obj.error) {
                obj.error(err);
            }
        }
    });
};
window.getBinary = function(url, data, success) {
    var xmlhttp = new XMLHttpRequest();
    var i = 0;
    for (var key in data) {
        if (i++ === 0) {
            url += '?' + key + "=" + data[key];
        } else {
            url += '&' + key + "=" + data[key];
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.responseType = "blob";
    xmlhttp.onload = function () {
        success(this.response);
    };
    xmlhttp.send();
}