verification.js 2.41 KB
/**
 * 验证手机
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/12/14
 */

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

var $smsCaptchaCtrl = $('#send-captcha'),
    $next = $('#next-step'),
    $smsCaptchaInput = $('#captcha');

var second = 60;

var $errTip = $('.tips'),
    $errMsg = $errTip.find('.rectangle');

function errTip(ele, msg) {
    var topLeft = ele.offset();

    $errMsg.text(msg);
    return $errTip.css({
        top: topLeft.top + ele.height() - 2,
        left: topLeft.left,
        width: ele.width() + 2,
        height: ele.height
    }).removeClass('hide');
}

function sendSMSCaptchaAsync() {
    return $.post('/passport/back/sendbackmobile', {
        mobile: $('#mobile').val(),
        area: $('#area').val(),
        verifyCode: $('#captchaPic').val()
    });
}

function disableSMSBtn() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $smsCaptchaCtrl.text('获取短信验证码');
        $smsCaptchaCtrl.removeClass('disable');
    } else {
        $smsCaptchaCtrl.text(second + '秒后可重新操作');
        window.setTimeout(disableSMSBtn, 1000);
    }
}

$smsCaptchaCtrl.click(function() {
    if ($smsCaptchaCtrl.hasClass('disable')) {
        return;
    }

    $smsCaptchaCtrl.addClass('disable');

    disableSMSBtn();
    sendSMSCaptchaAsync();
});

$smsCaptchaInput.on('blur', function() {
    var v = $.trim($(this).val());

    if (v === '') {
        errTip($smsCaptchaInput, '请输入短信验证码');
        return;
    }

    if (v.length === 4) {
        $.ajax({
            type: 'POST',
            url: '/passport/back/backmobile',
            dataType: 'json',
            data: {
                code: $('#captcha').val(),
                verifyCode: $('#captchaPic').val(),
                area: $('#area').val(),
                mobile: $('#mobile').val()
            },
            success: function(res) {
                if (res.code === 200) {
                    // 添加验证码正确验证
                    $next.removeClass('disable').attr('href', res.data);
                } else {
                    $next.addClass('disable');
                    errTip($smsCaptchaInput, '验证码不正确');
                }
            }
        });
    } else {
        errTip($smsCaptchaInput, '验证码不正确');
        $next.addClass('disable').attr('href', 'javascript:;');
    }
});

function init() {
    $smsCaptchaCtrl.addClass('disable');
    disableSMSBtn();
}

init();