sms-check.page.js 4.48 KB
'use strict';

let tip, checkPoint;

let $resendBtn,
    $nextBtn,
    $smsCode,
    $resetBtn,
    mobile, area;
let page;

require('common');
tip = require('plugin/tip');
checkPoint = require('./smslogin/check-point');

page = {
    disableAjax: false,

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


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


    bindEvents: function() {
        let self = this;

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

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

            $nextBtn.trigger('toggleDisable', !hasVal);
            $resetBtn.toggle(hasVal);
        });

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

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

        $nextBtn.on('toggleDisable', function(event, bool) {
            if (bool === void 0) {
                bool = true;
            }

            $nextBtn.toggleClass('disable', bool);
            $nextBtn.prop('disabled', bool);
        });
    },

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

        if (during) {
            clearInterval(this.timerId);
            second = during;
        } else if (this.timerId) {
            return;
        }

        $resendBtn.prop('disabled', true);
        $resendBtn.text('重新获取(' + second + ')');
        this.timerId = setInterval(function() {
            let txt = self.resendText;

            second = second - 1;

            if (second < 0) {
                clearInterval(self.timerId);
                self.timerId = null;
                $resendBtn.prop('disabled', false);
            } else {
                txt = '重新获取(' + second + '秒)';
            }

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

    },


    resendSMS: function() {
        let self = this;

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

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

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

    submit: function() {
        let code = $.trim($smsCode.val());

        if ($nextBtn.prop('disabled')) {
            return;
        }

        $nextBtn.prop('disabled', true);
        $.get('/passport/sms_login/check.json', {
            code: code
        })
            .done(function(res) {
                if (res.code === 200) {
                    checkPoint('YB_MOBILE_LOGIN_C'); // 埋点

                    if (res.newer) {
                        res.redirect = res.redirect + '&registerCode=' + res.registerCode;

                        // 暂时跳注册 TODO
                        // tip.show('请先注册!');
                        // setTimeout(function(){
                        //     location.href = '//m.yohobuy.com/reg.html';
                        // }, 2000);
                        // return;
                    }

                    location.href = res.redirect;
                    return;
                }

                tip.show(res.message);
            })
            .fail(function() {
                tip.show('出错了, 请重试');
            })
            .always(function() {
                $nextBtn.prop('disabled', false);
            });
    }
};

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

// 多次登录失败跳短信认证填充手机号
if (window.cookie('loginJump')) {
    $('.sms-login .title').text('身份验证');
    $('.sms-login-msg').before('<p class="sms-login-msg">您的账号存在安全隐患需要进行身份验证</p>');
    window.setCookie('loginJump', '');
}