Login.js 2.18 KB
let href = window.location.href + '';
let $ = require('jquery');
let $captcha = $('#captcha');

if(href) {
    let index = href.indexOf('?code');
    if(index > 0) {
        let code = +href.substring(index + '?code='.length);
        // 登陆错误
        if(code === 400) {
            alert("用户名或者密码错误,请确认!");
        }

        if(code === 405) {
            alert('图形验证失败');
        }

        window.location.href = "/login";
    }
}

var captcha = {
    init: function() {
        var that = this;

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'login/captcha',
            success: function (result) {
                if (result.code === 500) {
                    alert('验证码加载异常');
                    window.location.reload(true);
                    return;
                }
                initGeetest && initGeetest({  // eslint-disable-line
                    gt: result.data.gt,
                    challenge: result.data.challenge,
                    width: '100%',
                    product: 'float', // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
                    new_captcha: result.data.new_captcha,
                    offline: !result.data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
                }, that.initCallback);
            },
            error: function (rs) {
                console.log(rs)
            }
        });
    },
    initCallback: function(captchaObj) {
        captchaObj.onSuccess(function() {
            var validate = captchaObj.getValidate();

            var result = [
                validate.geetest_challenge,
                validate.geetest_validate,
                validate.geetest_seccode
            ];

            $captcha.val(result.join(','));
            $('#loginForm').submit();
        });

        captchaObj.onError(function() {
            $captcha.val('');
        });

        captchaObj.onClose(function() {
            $captcha.val('');
        });

        captchaObj.appendTo(document.getElementById('img-check-main'));
    }
}

$(function() {
    captcha.init();
});