third-pwd.js 8.76 KB
/**
 * 第三方绑定完善个人信息
 * @author: wq
 * @date: 2016/1/27
 */
var $ = require('yoho.jquery');
var dovalidate = false; //校验验证码的标识
var isvalidatecode = false; //是否验证成功的标识
var isvalidatepwd = false; //密码验证是否通过的标识
var second = ''; //倒计时时间
var nopermissionoption = ''; //倒计时的dom
var sendmessagehtml = ''; //发送短信的dom
var submitStatus = false; //信息提交状态

var $wrapper = $('.bindwrapper'),
    $codeTip = $wrapper.find('.code-err-tip'),
    $pwdTip = $wrapper.find('.pwd-err-tip'),
    $pwdTip2 = $('#pwd-tip2'),
    $sendMsgBtn = $('#sendmessage');
var $pwd,
    $pwdParent,
    $pwdTip1,
    $pwdIntensity;

var pwdRegx = require('./mail-phone-regx').pwdValidateRegx;
var mobile = $('#mobile').val(),
    area = $('#area').val();

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://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 showErrTip($dom, info) {
    $dom.find('em').text(info);
    $dom.removeClass('hide');
}

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

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

function sendMessageValidate() {
    $(document).on('click', '#sendmessage', function() {
        circleTime();
        $.ajax({
            type: 'POST',
            url: '/passport/autouserinfo/sendBindMsg',
            data: {
                mobile: mobile,
                area: area
            }
        }).then(function(data) {
            if (data.code !== 200) {
                alert(data.message);
            }
        });

    });
}



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,
                        area: area
                    }
                }).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;
    }
    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');
        }
        if (/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$/.test(pwd)) {
            $pwdTip2.removeClass('default no red').addClass('yes');
        } else {
            $pwdTip2.removeClass('default yes').addClass('no red');
        }
    }
}

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 pwd = '';
    var code = '';

    // 查看提交状态,避免重复提交
    if (submitStatus) {
        return;
    }
    submitStatus = true;
    setTimeout(function() {
        submitStatus = false;
    }, 50000);

    pwd = $('#pwd').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) {
        submitStatus = false;
        if (data.code === 200) {
            if (data.data && data.data.refer) {
                window.location.href = data.data.refer;
            } else {
                window.location.href = '/passport/thirdlogin/bindsuccess';
            }
        } else {
            alert(data.message);
        }
    });
}
/**
 * 确定完善信息
 * @return {[type]} [description]
 */
function actionConfirm() {
    var validatenum = '';
    var pwd = '';

    $('#confirmsubmit').on('click', function() {
        var isAssess = true;

        validatenum = $('#validatenum').val();
        pwd = $('#pwd').val();

        if (validatenum === '') {
            showErrTip($codeTip, '短信验证码不能为空');
            isAssess = false;
        } else if (isvalidatecode === false) {
            showErrTip($codeTip, '请输入正确的验证码');
            isAssess = false;
        }

        if (pwd === '') {
            showErrTip($pwdTip, '密码不能为空');
            isAssess = false;
        } else if (pwd.length < 6 || pwd.length > 20) {
            showErrTip($pwdTip, '密码只支持6-20位字符');
            isAssess = false;
        } else if (!pwdRegx.test(pwd)) {
            showErrTip($pwdTip, '密码须由字母和数字组合');
            isAssess = false;
        } else if (isvalidatepwd === false) {
            showErrTip($pwdTip, '请输入符合强度的密码');
            isAssess = false;
        }

        if (!isAssess) {
            return;
        }

        actionSubmit();
    });
}



function init() {
    $pwd = $('#pwd');
    $pwdParent = $('.safelevel');
    $pwdIntensity = $('.pwd-intensity');
    $pwdTip1 = $('#pwd-tip1');
    sendMessageValidate();
    codeValidate();
    validatePwd();
    actionConfirm();
    if (mobile && area) {
        $sendMsgBtn.click();
    }
}

init();