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

var $eyeBtn,
    $pwd,
    $nextBtn;

var page = {
    init: function() {
        this.domInit();
        this.bindEvent();
    },
    domInit: function() {
        $eyeBtn = $('#eye');
        $pwd = $('#pwd');
        $nextBtn = $('#btn-next');
    },
    bindEvent: function() {
        var self = this;

        $eyeBtn.on('click', function() {
            self.togglePassword();
        });

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

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

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

    togglePassword: function() {
        var bool = $eyeBtn.hasClass('close');

        $eyeBtn.toggleClass(function() {
            $pwd.attr('type', !bool ? 'password' : 'text');
            return 'close';
        }, !bool);
    },

    setPasswordAndLogin: function() {
        var password = $.trim($pwd.val());

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

        $nextBtn.prop('disabled', true);
        $.post('/passport/sms_login/password.json', {
            password: password
        })
            .done(function(res) {
                if (res.code === 200) {
                    // $nextBtn.off();
                    location.href = res.redirect;
                    return;
                }

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

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