gee-captcha.js 3.74 KB
/**
 * Created by TaoHuang on 2016/12/12.
 */

var $ = require('yoho-jquery');

require('../common/promise');

var GeeCaptcha = function(container, options) {
    var optionDefault = {
        template: require('hbs/common/gee-captcha.hbs')
    };

    $.extend(this, optionDefault, options);
    this.container = container;
    this.$container = $(this.container);
    this.$container.html(this.template());
    this.$tip = this.$container.find('.img-check-tip-gee');
    this._result = [];
    this._captchObj = null;
    return this;
};

GeeCaptcha.prototype = {
    init: function() {
        var _this = this;

        $.get('/passport/geetest/register?t=' + $.now()).then(function(data) {
            initGeetest && initGeetest({
                gt: data.gt,
                challenge: data.challenge,
                product: 'float', // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
                offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
            }, $.proxy(_this.initCallback, _this));
        });

        return this;
    },

    initCallback: function(captchaObj) {
        var _this = this;

        _this._captchObj = captchaObj;

        captchaObj.onSuccess(function() {
            var validate = captchaObj.getValidate();

            _this._result = [
                validate.geetest_challenge,
                validate.geetest_validate,
                validate.geetest_seccode
            ];
            _this.hideTip();
        });

        captchaObj.onFail(function() {
            _this._result = [];
            _this.hideTip();
        });

        captchaObj.onRefresh(function() {
            _this._result = [];
            _this.hideTip();
            this.refreshCb && _this.refreshCb();
        });

        captchaObj.appendTo(_this.$container.find('.img-check-main').get(0));
    },

    refresh: function() {
        var _this = this;

        return $.sleep(500).then(function() {
            _this._captchObj.refresh();
        });
    },

    onRefresh: function(cb) {
        this.refreshCb = cb;
        return this;
    },

    check: function() {
        var _this = this;

        if (_this.getResults() === '') {
            _this.showTip();
            return $.Deferred().reject().promise(); // eslint-disable-line
        }

        //return $.post('/passport/geetest/validate', {
        //    verifyCode: _this.getResults()
        //}).then(function(result) {
        //    if (result.code === 200) {
        //        _this.hideTip();
        //        return $.Deferred().resolve().promise(); // eslint-disable-line
        //    } else if (result.code === 403) {
        //        _this.refresh();
        //        return $.Deferred().reject().promise(); //eslint-disable-line
        //    } else {
        //        _this.showTip(result.message);
        //        return $.Deferred().reject().promise(); //eslint-disable-line
        //    }
        //});

        return $.Deferred().resolve().promise(); // eslint-disable-line
    },

    getResults: function() {
        return this._result.join(',');
    },

    showTip: function(msg) {
        if (msg) {
            this.$tip.find('em').html(msg);
        } else {
            this.$tip.find('em').html('请将图形验证码输入正确');
        }

        if (this.$tip) {
            this.$tip.removeClass('hide');
        }
        return this;
    },

    hideTip: function() {
        if (this.$tip) {
            this.$tip.addClass('hide');
        }
        return this;
    },

    show: function() {
        this.$container.removeClass('hide');
    },

    hide: function() {
        this.$container.addClass('hide');
    }
};

GeeCaptcha.prototype.construct = GeeCaptcha;


module.exports = GeeCaptcha;