sms-check.page.js 3.21 KB
var tip = require('plugin/tip');

var $resendBtn,
    $nextBtn,
    $smsCode,
    $resetBtn,
    mobile, area;

var page = {
    disableAjax: false,

    time: 60,
    resendText: '重发验证码',
    timerId: null,
    init: function() {
        this.domInit();
        this.bindEvents();
        if (!window.canResend) {
            this.countDown();
        }
    },


    domInit: function() {
        $resendBtn = $('#resend-sms');
        $nextBtn = $('#btn-next');
        $resetBtn = $('.clear-input');
        $smsCode = $('#sms-code');
        mobile = $('#mobile').val();
        area = $('#area').val();
    },


    bindEvents: function() {
        var self = this;

        $resendBtn.on('click', function() {
            self.resendSMS();
        });

        $smsCode.on('input', function() {
            var hasVal = Boolean($.trim(this.value));

            $nextBtn.toggleClass('disable', !hasVal);
            $nextBtn.prop('disabled', !hasVal);
            $resetBtn.toggle(hasVal);
        });

        $nextBtn.on('click', function() {
            !self.disableAjax && self.submit();
        });

        $resetBtn.on('click', function() {
            $smsCode.val('');
            $resetBtn.hide();
        });
    },


    countDown: function() {
        var self = this;
        var second = this.time;

        if (this.timerId) {
            return;
        }

        $resendBtn.prop('disable', true);
        this.timerId = setInterval(function() {
            var txt = self.resendText;

            second = second - 1;

            if (second < 0) {
                clearInterval(self.timerId);
                self.timerId = null;
                $resendBtn.prop('disable', false);
            } else {
                txt = second + 's';
            }

            $resendBtn.text(txt);
        }, 1000);

    },


    resendSMS: function() {
        var self = this;

        if ($resendBtn.prop('disable')) {
            return;
        }

        $.get('/passport/sms_login/token.json', {
            area: area,
            mobile: mobile,
        })
            .done(function(res) {
                if (res.code === 200) {
                    self.countDown();
                    return;
                }

                tip.show(res.message);
            })
            .fail(function() {
                tip.show('出错啦~休息一下');
            });
    },

    submit: function() {
        var self = this;
        var code = $.trim($smsCode.val());

        $nextBtn.prop('disabled', this.disableAjax = true);
        $.get('/passport/sms_login/check.json', {
            code: code
        })
            .done(function(res) {
                if (res.code === 200) {
                    location.href = res.redirect;
                    self.disableAjax = true; // 成功后 disabled, 执行跳转
                    return;
                }

                tip.show(res.message);
            })
            .fail(function() {
                tip.show('出错了, 请重试');
                self.disableAjax = false; // 失败后 允许再次 请求
            })
            .always(function() {
                $nextBtn.prop('disabled', self.disableAjax);
            });
    }
};

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