verification.js 2.63 KB
/**
 * 验证手机
 * @author: TaoHuang
 * @date: 2016/7/14
 */

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

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

var second = 60;

function errTip(ele, msg) {
    var $errTip = ele.next('.tips');
    var $errMsg = $errTip.find('.content');

    $errMsg.text(msg);
    return $errTip.removeClass('hide');
}

function hideTip(ele) {
    return ele.next('.tips').addClass('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('获取短信验证码').removeClass('disable').removeClass('second-progress');
    } else {
        $smsCaptchaCtrl.addClass('second-progress').text(second + '秒后可重新操作');
        window.setTimeout(disableSMSBtn, 1000);
    }
}

$smsCaptchaCtrl.click(function() {
    if ($smsCaptchaCtrl.hasClass('disable') || $smsCaptchaCtrl.hasClass('second-progress')) {
        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);
                    hideTip($smsCaptchaInput);
                } else {
                    $next.addClass('disable');
                    errTip($smsCaptchaInput, '验证码不正确');
                }
            }
        });
    } else {
        errTip($smsCaptchaInput, '验证码不正确');
        $next.addClass('disable').attr('href', 'javascript:;');
    }
}).on('focus', function() {
    hideTip($smsCaptchaInput);
    $smsCaptchaInput.addClass('focus');
}).on('blur', function() {
    $smsCaptchaInput.removeClass('focus');
});

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

init();