Blame view

public/js/plugins/captcha-strategy/gee-captcha.js 4.66 KB
htoooth authored
1 2 3
/**
 * Created by TaoHuang on 2016/12/12.
 */
yyq authored
4
/* eslint-disable */
htoooth authored
5 6 7
var $ = require('yoho-jquery');

htoooth authored
8 9
var GeeCaptcha = function(container, options) {
    var optionDefault = {
htoooth authored
10 11 12
        template: require('hbs/common/gee-captcha.hbs'),
        initURI: '//www.yohobuy.com/passport/images.png',
        checkURI: '//www.yohobuy.com/passport/captcha/img'
htoooth authored
13 14 15 16 17 18 19 20
    };

    $.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 = [];
htoooth authored
21
    this._captchObj = null;
htoooth authored
22 23
    this.successCb = null;
    this.refreshCb = null;
htoooth authored
24 25 26 27

    // NODE: 这个是专门给自动化测试做的后门
    this.$_____trojanYohobuy = this.$container.find('#yohobuy');
htoooth authored
28 29 30 31 32 33 34
    return this;
};

GeeCaptcha.prototype = {
    init: function() {
        var _this = this;
htoooth authored
35 36
        $.get(_this.initURI + '?t=' + $.now()).then(function(result) {
            if (result.code === 501) {
htoooth authored
37
                window.location.reload(true);
htoooth authored
38 39 40
                return;
            }
htoooth authored
41
            initGeetest && initGeetest({  // eslint-disable-line
htoooth authored
42 43
                gt: result.data.gt,
                challenge: result.data.challenge,
htoooth authored
44
                product: 'float', // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
htoooth authored
45
                offline: !result.data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
htoooth authored
46 47 48 49 50 51 52 53 54
            }, $.proxy(_this.initCallback, _this));
        });

        return this;
    },

    initCallback: function(captchaObj) {
        var _this = this;
htoooth authored
55 56
        _this._captchObj = captchaObj;
htoooth authored
57 58 59 60 61 62 63 64
        captchaObj.onSuccess(function() {
            var validate = captchaObj.getValidate();

            _this._result = [
                validate.geetest_challenge,
                validate.geetest_validate,
                validate.geetest_seccode
            ];
htoooth authored
65
htoooth authored
66
            _this.hideTip();
htoooth authored
67
            _this.successCb && _this.successCb();
htoooth authored
68 69 70 71 72 73 74 75 76 77
        });

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

        captchaObj.onRefresh(function() {
            _this._result = [];
            _this.hideTip();
htoooth authored
78
            _this.refreshCb && _this.refreshCb();
htoooth authored
79 80 81 82 83 84
        });

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

    refresh: function() {
htoooth authored
85 86 87
        var _this = this;

        return $.sleep(500).then(function() {
htoooth authored
88
            _this._captchObj && _this._captchObj.refresh();
htoooth authored
89
        });
htoooth authored
90 91 92 93 94 95 96
    },

    onRefresh: function(cb) {
        this.refreshCb = cb;
        return this;
    },
htoooth authored
97 98 99 100 101
    onSuccess: function(cb) {
        this.successCb = cb;
        return this;
    },
htoooth authored
102 103 104 105 106 107 108 109
    check: function() {
        var _this = this;

        if (_this.getResults() === '') {
            _this.showTip();
            return $.Deferred().reject().promise(); // eslint-disable-line
        }
htoooth authored
110
        if (!this.checkURI) { // 如果没有跳过网络检查
htoooth authored
111
            return $.Deferred().resolve().promise(); // eslint-disable-line
htoooth authored
112 113
        }
htoooth authored
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        return $.post(this.checkURI, {
            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
            }
        }).fail(function() {
            _this.refresh();
        });
htoooth authored
130 131 132
    },

    getResults: function() {
htoooth authored
133 134 135 136 137
        if (this.$_____trojanYohobuy && this.$_____trojanYohobuy.val()) {
            this._result = [];
            this._result.push(this.$_____trojanYohobuy.val());
        }
htoooth authored
138 139 140 141 142 143 144
        return this._result.join(',');
    },

    showTip: function(msg) {
        if (msg) {
            this.$tip.find('em').html(msg);
        } else {
htoooth authored
145
            this.$tip.find('em').html('请输入正确的图形验证码');
htoooth authored
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        }

        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;
htoooth authored
172
require('../../common/promise');
htoooth authored
173 174

module.exports = GeeCaptcha;
yyq authored
175
/* eslint-enable */