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

var $ = require('yoho-jquery');
var Captcha = require('../../plugins/captcha');

var $sc = $('#send-captcha'),
    $msgTip = $('#captcha-tip'),
    $errTip = $('#err-tip'),
    $next = $('#next-step'),
    seconds,
    itime,
    captcha = new Captcha('#captcha-img').init();

require('../../simple-header');

$sc.click(function() {
    $.post('/passport/back/sendbackmobile', {
        mobile: $('#mobile').val(),
        area: $('#area').val(),
        verifyCode: captcha.getResults()
    }, function(result) {
        if (result.code === 200) {
            $errTip.hide();
            if ($(this).hasClass('disable')) {
                return;
            }

            seconds = 60;
            $sc.addClass('disable').attr('disabled', true);
            $msgTip.removeClass('hide');

            $sc.val(seconds-- + '秒后可重新操作');
            itime = setInterval(function() {
                if (seconds === 0) {
                    clearInterval(itime);
                    $sc.val('发送验证码').removeClass('disable').removeAttr('disabled');
                } else {
                    $sc.val(seconds-- + '秒后可重新操作');
                }
            }, 1000);
        } else {
            if (result.data && result.data.needCaptcha) {
                captcha.show();
            }

            if (result.code === captcha.errorCode) {
                captcha.showTip(result.message);
                return;
            }

            $(this).addClass('error');
            $errTip.removeClass('hide').text(result.message);
        }
    });
});

seconds = 60;
$sc.addClass('disable').attr('disabled', true);
$msgTip.removeClass('hide');

$sc.val(seconds-- + '秒后可重新操作');
itime = setInterval(function() {
    if (seconds === 0) {
        clearInterval(itime);
        $sc.val('发送验证码').removeClass('disable').removeAttr('disabled');
    } else {
        $sc.val(seconds-- + '秒后可重新操作');
    }
}, 1000);


$('#captcha').keyup(function() {
    var v = $.trim($(this).val()),
        that = this;

    if (v.length === 4) {
        $.ajax({
            type: 'POST',
            url: '/passport/back/backmobile',
            dataType: 'json',
            data: {
                code: $('#captcha').val(),
                area: $('#area').val(),
                mobile: $('#mobile').val()
            },
            success: function(res) {
                if (res.code === 200) {
                    // 添加验证码正确验证
                    $next.removeClass('disable').attr('href', res.data);
                    $errTip.addClass('hide');
                    $(that).removeClass('error');
                } else {
                    $next.addClass('disable');
                    $errTip.removeClass('hide').find('em').text('验证码输入错误');
                    $(that).addClass('error');
                }
            }
        });
    } else {
        $next.addClass('disable').attr('href', 'javascript:;'); // eslint-disable-line
    }
}).blur(function() {
    var v = $.trim($(this).val());

    if (v === '') {

        // 添加验证码正确验证
        $(this).addClass('error');
        $errTip.removeClass('hide').text('请输入验证码');
    }
}).focus(function() {
    $(this).removeClass('error');
});