bind-info.js 8.26 KB
/**
 * 第三方绑定完善个人信息
 * @author: wq
 * @date: 2016/1/27
 */
var $ = require('yoho-jquery');

var Captcha = require('../../plugins/captcha');
var captcha = new Captcha('#captcha');
var dovalidate = false; // 校验验证码的标识
var isvalidatecode = false; // 是否验证成功的标识
var isvalidatepwd = false; // 密码验证是否通过的标识
var second = ''; // 倒计时时间
var nopermissionoption = ''; // 倒计时的dom
var sendmessagehtml = ''; // 发送短信的dom
var $pwd,
    $pwdParent,
    $pwdTip1,
    $pwdIntensity;

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

nopermissionoption = $('#nopermissionmessage').html();
sendmessagehtml = $('.validatewrapper').html();
second = +$('.second').text();


function gettype(str, i) {
    if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
        return 1;
    } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
        return 2;
    } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
        return 3;
    }

    return 4;
}

function isregular(cur, pre, type) {
    var curCode = cur.charCodeAt(0);
    var preCode = pre.charCodeAt(0);

    if (curCode - preCode === 0) {
        return true;
    }

    if (type !== 4 && (curCode - preCode === 1 || curCode - preCode === -1)) {
        return true;
    }

    return false;
}


function getcomplex(curType, preType) {
    if (preType === 0 || curType === preType) {
        return 0;
    } else if (curType === 4 || preType === 4) {
        return 2;
    } else {
        return 1;
    }
}


/**
 * @desc: 计算密码复杂度(from:http://www.oschina.net/code/snippet_127301_17269)
 * @author: xuqi(qi.xu@yoho.cn)
 * @date: 2015/5/6
 */
function computeComplex(password) {
    var complex = 0;
    var length = password.length;
    var pre = '';
    var preType = 0;
    var i = 0;
    var cur = '';
    var curType = '';

    for (i; i < length; i++) {
        cur = password.charAt(i);
        curType = gettype(password, i);
        if (preType !== curType || !isregular(cur, pre, curType)) {
            complex += curType + getcomplex(curType, preType);
        }
        pre = cur;
        preType = curType;
    }

    return complex;
}



function changeSecond() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $('.validatewrapper').html(sendmessagehtml);
        captcha.init();
        return;
    } else {
        $('.second').text(second);
        window.setTimeout(changeSecond, 1000);
    }

}

/**
 * 启动倒计时
 * @return {[type]} [description]
 */
function circleTime() {
    $('.validatewrapper').html(nopermissionoption);
    window.setTimeout(changeSecond, 1000);
}

captcha.onSuccess(function() {
    $('#sendmessage').trigger('click');
});

function sendMessageValidate() {
    var mobile = '';
    var area = '';

    $(document).on('click', '#sendmessage', function() {
        mobile = $('#mobile').val();
        area = $('#area').val();
        $.ajax({
            type: 'POST',
            url: '/passport/autouserinfo/sendBindMsgCode',
            data: {
                mobile: mobile,
                area: area,
                verifyCode: captcha.getResults()
            }
        }).then(function(data) {
            if (data.code === 200) {
                circleTime();
                return true;
            }

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

            alert(data.message); // eslint-disable-line
        });
    });
}

function codeValidate() {
    var validatenum = '';

    $(document).on('keyup', '#validatenum', function() {
        $('#err-info').hide();
        validatenum = $(this).val();
        if (validatenum.length === 4) {
            if (!dovalidate) {
                dovalidate = true;
                $.ajax({
                    type: 'POST',
                    url: '/passport/autouserinfo/checkBindMsg',
                    data: {
                        code: validatenum,
                        mobile: $('#mobile').val(),
                        area: $('#area').val()
                    }
                }).then(function(data) {
                    dovalidate = false;
                    if (data.code !== 200) {
                        $('#err-info').show();
                    } else {
                        isvalidatecode = true;
                    }
                });
            }
        }
    });
}

function pwdKeyupEvt() {
    var pwd = $pwd.val(),
        pwdStrength = computeComplex(pwd),
        level = 0;

    if (pwdStrength === 0) {
        level = 0;
    } else if (pwdStrength <= 10) {
        level = 1;
    } else if (pwdStrength <= 20) {
        level = 2;
    } else {
        level = 3;
    }
    switch (level) {
        case 0:
            $pwdParent.removeClass('red yellow green');
            $pwdIntensity.removeClass('color');
            break;
        case 1:
            $pwdParent.addClass('red').removeClass('yellow green');
            $pwdIntensity.filter('.low').addClass('color');
            $pwdIntensity.filter('.mid,.high').removeClass('color');
            break;
        case 2:
            $pwdParent.addClass('yellow').removeClass('red green');
            $pwdIntensity.filter('.low,.mid').addClass('color');
            $pwdIntensity.filter('.high').removeClass('color');
            break;
        case 3:
            $pwdParent.addClass('green').removeClass('yellow red');
            $pwdIntensity.addClass('color');
            break;
        default:
            break;
    }
    if (pwd === '') {
        isvalidatepwd = false;
        $pwdTip1.removeClass('red yes no').addClass('default');
    } else {
        if (pwd.length < 6 || pwd.length > 20) {
            isvalidatepwd = false;
            $pwdTip1.removeClass('default yes').addClass('no red');
        } else {
            isvalidatepwd = true;
            $pwdTip1.removeClass('default no red').addClass('yes');
        }
    }
}

function validatePwd() {
    $(document).on('keyup', '.pwdcontent', function() {
        pwdKeyupEvt($(this));
    });
    $(document).on('focus', '.pwdcontent', function() {
        $('#pwd-tips').removeClass('hide');
    });
    $(document).on('blur', '.pwdcontent', function() {
        $('#pwd-tips').addClass('hide');
    });
}

/**
 * 最终提交表单
 */
function actionSubmit() {
    var mobile = '';
    var area = '';
    var pwd = '';
    var code = '';

    area = $('#area').val();
    pwd = $('#pwd').val();
    mobile = $('#mobile').val();
    code = $('#validatenum').val();
    $.ajax({
        type: 'POST',
        url: '/passport/autouserinfo/bindMobile',
        data: {
            area: area,
            openId: $('#openId').val(),
            sourceType: $('#sourceType').val(),
            mobile: mobile,
            password: pwd,
            code: code
        }
    }).then(function(data) {
        if (data.code === 200) {
            window.location.href = '/passport/thirdlogin/bindsuccess';
        } else {
            alert(data.message); // eslint-disable-line
        }
    });
}

/**
 * 确定完善信息
 * @return {[type]} [description]
 */
function actionConfirm() {
    var validatenum = '';
    var pwd = '';

    $('#confirmsubmit').on('click', function() {
        validatenum = $('#validatenum').val();
        pwd = $('#pwd').val();
        if (validatenum === '') {
            alert('短信验证码不能为空'); // eslint-disable-line
            return;
        }
        if (pwd === '') {
            alert('密码不能为空'); // eslint-disable-line
            return;
        }
        if (!/^(?![^a-zA-Z]+$)(?!\D+$).{6,20}$/.test(pwd)) {
            alert('密码只支持6-20位字符,建议字母+数字的组合。'); // eslint-disable-line
            return;
        }
        if (isvalidatecode === false) {
            alert('请输入正确的验证码'); // eslint-disable-line
            return;
        }
        if (isvalidatepwd === false) {
            alert('请输入符合强度的密码'); // eslint-disable-line
            return;
        }
        actionSubmit();
    });
}

function init() {
    $pwd = $('#pwd');
    $pwdParent = $('.safelevel');
    $pwdIntensity = $('.pwd-intensity');
    $pwdTip1 = $('#pwd-tip1');
    sendMessageValidate();
    codeValidate();
    validatePwd();
    actionConfirm();
    circleTime();
}

init();