operate.js 5.68 KB
/**
 * [个人中心]个人设置-第二步操作
 * @author: jiangmin
 * @date: 2016/07/14
 */
var dialog = require('../../plugins/dialog');
var _alert = dialog.Alert;
var regValidate = require('../../passport/common/mail-phone-regx');
var types = location.pathname.split('/');
var type = types[types.length - 1];// 界面操作类型
var $imgCaptchaInput = $('#captcha');
var second = 60;
var $sms = $('#send-code2');// 发送短信验证码按钮
var area = $('#country-code').text().substring(0) || '86';// 地区码
// 发送短信后倒计时显示
var disableSMSBtn = function() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $sms.text('获取短信验证码');
        $sms.removeClass('disable');
        $sms.removeClass('progress');
    } else {
        $sms.add('progress');
        $sms.text(second + '秒后可重新操作');
        window.setTimeout(disableSMSBtn, 1000);
    }
};

// 校验手机号码格式
var validatePhoneNumLocal = function(phoneNum) {
    var length = phoneNum.length;

    if (length === 0) {
        new _alert('请输入手机号码!').show();
        return false;
    } else if (length !== 11 || !/^[0-9]+$/.test(phoneNum) || !regValidate.phoneRegx['+86'].test(phoneNum)) {
        new _alert('手机号码格式不正确,请重新输入!').show();
        return false;
    } else {
        return true;
    }
};

require('yoho-jquery-placeholder');

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

/**
 * 绑定手机号码部分
 */
$('#region').on('change', function() {
    $('#country-code').text($(this).val());
});

// 校验手机号码
$('#real-mobile').blur(function() {
    var mobile = $(this).val();
    var self = $('#real-mobile').parent();

    if (validatePhoneNumLocal(mobile)) {
        $.ajax({
            type: 'POST',
            url: '/me/account/checkVerifyMobile',
            dataType: 'json',
            data: {
                mobile: mobile,
                area: area
            },
            success: function(data) {
                if (data.code === 200) {
                    self.find('.tips-success').addClass('ok').show();
                    self.find('.tips-error').removeClass('notok').hide();
                } else {
                    new _alert(data.message).show();
                    self.find('.tips-success').removeClass('ok').hide();
                    self.find('.tips-error').addClass('notok').show();
                }
            }
        });
    }
});


// 发送手机验证码
$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-step2').click(function() {
    var code = $('#msg-code').val();
    var mobile = $('#real-mobile').val();
    var self = $('#msg-code').parent();


    $.ajax({
        type: 'POST',
        url: '/me/setting/step2/mobile',
        data: {
            code: code,
            mobile: mobile,
            area: area
        },
        success: function(data) {
            if (data.code === 200) {
                self.find('.tips-success').addClass('ok').show();
                self.find('.tips-error').removeClass('notok').hide();
                location.href = '/me/setting/step3/' + type + '?checkCode=' + $('#checkCode').val();
            } else {
                new _alert(data.message).show();
                self.find('.tips-success').removeClass('ok').hide();
                self.find('.tips-error').addClass('notok').show();
            }
        }
    });
});


/**
 * 修改密码部分
 */
// 输入确认密码
$('#checkPwd').blur(function() {
    var newPwd = $('#newPwd').val().trim();
    var checkPwd = $(this).val().trim();
    var self = $(this).parent();

    if (newPwd === checkPwd) {
        self.find('.tips-success').addClass('ok').show();
        self.find('.tips-error').removeClass('notok').hide();
    } else {
        self.find('.tips-success').removeClass('ok').hide();
        self.find('.tips-error').addClass('notok').show();
    }
});

// 校验图片验证码
$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();
            }
        }
    });
});

// 提交修改密码
$('#step2-pwd').click(function() {
    var password = $('#newPwd').val();

    if ($('.notok').length === 0) {
        $.ajax({
            type: 'POST',
            url: '/me/setting/step2/password',
            data: {
                password: password
            },
            success: function(data) {
                if (data.code === 200) {
                    location.href = '/me/setting/step3/' + type + '?checkCode=' + $('#checkCode').val();
                } else {
                    new _alert(data.message).show();
                }
            }
        });
    }

});