index.page.js 4.46 KB

var $ = require('yoho-jquery'),
    num = 0,
    phone,
    reg,
    time;

$('#phone').bind('input propertychange', function() {
    if ($(this).val().length === 11) {
        $('.input-content div').eq('0').addClass('verification-code');
    } else {
        $('.input-content div').eq('0').removeClass();
    }
});
$('#verification').bind('input propertychange', function() {
    if ($(this).val().length === 4) {
        $('.input-content div').eq('1').addClass('get');
    } else {
        $('.input-content div').eq('1').removeClass();
    }
});

$('.input-content').on('click', '.verification-code', function() {
    $('.input-content div').eq('0').removeClass('verification-code');
    phone = $(this).siblings('input').val();
    reg = /[0-9]{11}/;
    if (!reg.test(phone)) {
        $('#dialog').removeClass('hidden');
        $('.keep-out').removeClass('hidden');
        $('#dialog .content').html('<p class="phone-error">手机号错误,请重新输入。<p>');
        setTimeout(function() {
            $('.messages').addClass('hidden');
            $('.keep-out').addClass('hidden');
            $('#dialog .content').html(' ');
        }, 1400);
        return;
    }
    $.ajax({
        url: '/activity/cocacola/CocacolaController/sendSms',
        data: {
            mobile: $('#phone').val()
        },
        dataType: 'json',
        success: function(data) {
            if (data.code === 200) {
                time = setInterval(function() {
                    num++;
                    if (num > 60) {
                        num = 0;
                        $('.input-content div').eq('0').addClass('verification-code').html('获取验证码');
                        clearInterval(time);

                    } else {
                        $('.input-content div').eq('0').removeClass('verification-code').html('重新发送' + (60 - num));
                    }
                }, 1000);
            } else {
                $('#dialog').removeClass('hidden');
                $('.keep-out').removeClass('hidden');
                $('#dialog .content').html('<p class="phone-error">' + data.message + '<p>');
                setTimeout(function() {
                    $('.messages').addClass('hidden');
                    $('.keep-out').addClass('hidden');
                    $('#dialog .content').html(' ');
                }, 1400);
            }
        }
    });
});
$('.input-content').on('click', '.get', function() {
    $('.input-content div').eq('1').removeClass('get');
    if ($(this).siblings('input').val().length === 4) {
        $.ajax({
            url: '/activity/cocacola/CocacolaController/validRegCodeAndSendCode',
            data: {
                code: $('#verification').val(),
                mobile: $('#phone').val(),
                client_id: $.cookie('_yasvd')
            },
            dataType: 'json',
            success: function(data) {
                if (data.code === 200) {
                    location.href = 'gain.html?mobile=' + $('#phone').val();
                } else {
                    $('#dialog').removeClass('hidden');
                    $('.keep-out').removeClass('hidden');
                    $('#dialog .content').html('<p class="phone-error">' + data.message + '<p>');
                    setTimeout(function() {
                        $('.messages').addClass('hidden');
                        $('.keep-out').addClass('hidden');
                        $('#dialog .content').html(' ');
                    }, 1400);
                }
            }
        });
    } else {
        $('#dialog').removeClass('hidden');
        $('.keep-out').removeClass('hidden');
        $('#dialog .content').html('<p class="phone-error">验证码错误,请重新输入。<p>');
        setTimeout(function() {
            $('.messages').addClass('hidden');
            $('.keep-out').addClass('hidden');
            $('#dialog .content').html(' ');
        }, 1400);
    }
});
$('.close').on('click', function() {
    $('.dialog').addClass('hidden');
    $('.keep-out').addClass('hidden');
    $('#dialog .content').html(' ');
});
$('.coupon-description span').on('click', function() {
    $('#message').removeClass('hidden');
    $('.keep-out').removeClass('hidden');
});
$('.description').on('click', function() {
    $('#message').removeClass('hidden');
    $('.keep-out').removeClass('hidden');
});

$('.input-content').on('click', '.get', function() {
    if (window._yas) {
        window._yas.sendCustomInfo({
            receiveCoupon: 'Y'
        }, true);
    }
});