login.js 1.94 KB
var layer = null,
    $ = null,
    isSendFlag = false,
    tabIndex = 0;

var isChrome = window.navigator.userAgent.indexOf("Chrome") > -1;
if (!isChrome) {
    alert("推荐使用谷歌浏览器登录使用,以免产生浏览器不兼容出现的错误");
}

// 防抖
function debounce(fn, delay) {
    let timeout = null; 
    return function () {
        clearTimeout(timeout); 
        timeout = setTimeout(() => { 
            fn.apply(this, arguments);
        }, delay);
    };
}

layui.config({ base: "/js/" }).use(["form", "layer", "element"], function () {
    //如果在iframe中,则跳转
    if (self !== top) top.location.replace("/Login/Index");
    layer = parent.layer === undefined ? layui.layer : parent.layer;
    $ = layui.jquery;

    var form = layui.form,   
        element = layui.element;

    element.on("tab(loginWebcam)", function (data) {
        tabIndex = data.index;
        if (data.index===1) {
            login.initWebCam();
        } else if (data.index === 2) {
            IdCard.focus();
        } else {
            document.getElementById("loginMessage").innerHTML = "";
        }
    });

    //登录按钮事件
    form.on("submit(login)", function (data) {
        data["file"] = "";
        $.ajax({
            url: "/Login/Login",
            type: "post",
            data: data.field,
            dataType: "json",
            success: function (data) {
                if (data.Code === 200) {
                    localStorage.setItem("Account", data.Result.Account);
                    localStorage.setItem("Name", data.Result.Name);
                    window.location.href = "/Home/Index";
                } else {
                    layer.msg(data.Message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(errorThrown, { icon: 2, shadeClose: true, title: "错误信息" });
            }
        });
        return false;
    });
});