validate.js 4.52 KB
/**
 * [个人中心]个人设置-第一步身份验证
 * @author: jiangmin
 * @date: 2016/07/14
 */
var $imgCaptchaInput = $('#captcha');
var dialog = require('../../plugins/dialog');
var _alert = dialog.Alert;
var types = location.pathname.split('/');
var type = types[types.length - 1];// 界面操作类型
var area = $('#country-code').text().substring(1) || '86';

/**
 * 手机号码验证
 */
var second = 60;
var $sms = $('#send-code');// 发送短信验证码按钮
// 发送短信后倒计时显示
var disableSMSBtn = function() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $sms.removeClass('progress').removeClass('disable').text('获取短信验证码');
    } else {
        $sms.add('progress');
        $sms.text(second + '秒后可重新操作');
        window.setTimeout(disableSMSBtn, 1000);
    }
};

require('yoho-jquery-placeholder');

$('[placeholder]').placeholder();

// 发送手机验证码
$sms.click(function() {
    var mobile = $('#real-mobile').val();


    if ($(this).hasClass('disable')) {
        return;
    }
    $sms.addClass('disable');
    $.ajax({
        type: 'POST',
        url: '/me/account/sendMobileMsg',
        dataType: 'json',
        data: {
            mobile: mobile,
            area: area
        },
        success: function(data) {
            if (data.code !== 200) {
                new _alert(data.message).show();
                $sms.removeClass('disable');
            } else {
                disableSMSBtn();
            }
        }
    });
});

// 手机验证第一步提交
$('#mobile-step1').click(function() {
    var code = $('#msg-code').val().trim();
    var mobile = $('#real-mobile').val();

    $.ajax({
        type: 'POST',
        url: '/me/setting/step1/mobile',
        data: {
            code: code,
            mobile: mobile,
            area: area
        },
        success: function(data) {
            if (data.code === 200) {
                location.href = '/me/setting/step2/' + type + '?checkCode=' + $('#checkCode').val();
            } else {
                // location.href = '/me/setting/step2/' + type + "?checkCode=" + $("#checkCode").val();
                new _alert('验证码不正确!').show();
                $('#msg-code').val('');
            }
        }
    });
});


/**
 * 密码校验
 */
// 换图形验证码
$('.change-captcha').click(function() {
    var time = new Date();
    var $captchaImg = $('.captcha-img');
    var captchaImgSrc = $captchaImg.attr('src').split('?')[0];

    $captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
});

// 校验图片验证码
$imgCaptchaInput.blur(function() {
    $.ajax({
        type: 'POST',
        url: '/passport/images/check',
        data: {
            verifyCode: $imgCaptchaInput.val()
        },
        success: function(data) {
            if (data.code === 200) {
                $imgCaptchaInput.parent().find('.tips-success').addClass('ok').show();
                $imgCaptchaInput.parent().find('.tips-error').removeClass('notok').hide();
            } else {
                $imgCaptchaInput.parent().find('.tips-success').removeClass('ok').hide();
                $imgCaptchaInput.parent().find('.tips-error').addClass('notok').show();
            }
        }
    });
});

// 第一步提交
$('#pwd-step1').click(function() {
    var password = $('#verifyPwd').val();

    if ($('.notok').length === 0) {
        $.ajax({
            type: 'POST',
            url: '/me/setting/step1/password',
            data: {
                password: password
            },
            success: function(data) {
                if (data.code === 200) {
                    location.href = '/me/setting/step2/' + type + '?checkCode=' + $('#checkCode').val();
                } else {
                    new _alert('登录密码校验错误!').show();
                }
            }
        });
    }
});


/**
 * 邮箱验证
 */

$('#email-step1').click(function() {
    if ($('.notok').length === 0) {
        $.ajax({
            type: 'POST',
            url: '/me/setting/step1/email',
            data: {
                email: $('#real-email').val()
            },
            success: function(data) {
                // todo 发送邮件
                if (data.code === 200) {
                    $('.operate1').hide();
                    $('.operate2').show();
                    $('.footer-tips').eq(0).hide();
                    $('.footer-tips').eq(2).show();
                } else {
                    new _alert(data.message).show();
                }
            }
        });
    }
});