third-pwd.js 3.17 KB
/**
 * 第三方绑定重设密码
 * @author: TaoHuang
 * @date: 2016/7/12
 */
var $ = require('yoho-jquery');

var pwdRegx = require('../common/mail-phone-regx').pwdValidateRegx;

var $passwordInput = $('#pwd'),
    $repasswordInput = $('#repwd');

var $sourceType = $('#sourceType');
var $openId = $('#openId');
var $mobile = $('#mobile');
var $area = $('#area');
var $next = $('#next');

var EventProxy = require('../common/eventproxy');

var ep = new EventProxy();

var $errTip = $('.tips'),
    $errMsg = $errTip.find('.rectangle');

require('yoho-jquery-placeholder');

function errTip(ele, msg) {
    var topLeft = ele.offset();

    $errMsg.text(msg);
    return $errTip.css({
        top: topLeft.top + ele.height() - 2,
        left: topLeft.left,
        width: ele.width() + 2,
        height: ele.height
    }).removeClass('hide');
}

function hideTip() {
    return $errTip.addClass('hide');
}

ep.tail('pwd', 'repwd', function(pwd, repwd) {
    if (pwd && repwd) {
        $next.removeClass('disable');
    } else {
        $next.addClass('disable');
    }
});

$passwordInput.on('keyup blur', function() {
    var length = $passwordInput.val().length;

    if (length === 0) {
        errTip($passwordInput, '请输入密码');
        ep.emit('pwd', false);
        return;
    }

    if (length < 6 || length > 20) {
        errTip($passwordInput, '密码只支持 6-20 位字符,建议字母+数字的组合');
        ep.emit('pwd', false);
        return;
    }

    if (!pwdRegx.test($passwordInput.val())) {
        errTip($passwordInput, '密码只支持 6-20 位字符,建议字母+数字的组合');
        ep.emit('pwd', false);
        return;
    }

    ep.emit('pwd', true);
});

$repasswordInput.on('keyup blur', function() {
    var length = $repasswordInput.val().length;

    if (length === 0) {
        errTip($repasswordInput, '请再次输入密码');
        ep.emit('repwd', false);
        return;
    }

    if ($passwordInput.val() !== $repasswordInput.val()) {
        errTip($repasswordInput, '两次输入的密码不一致,请重新输入');
        ep.emit('repwd', false);
        return;
    }

    console.log($passwordInput.val(), $repasswordInput.val());

    ep.emit('repwd', true);
});

function nextPage() {
    $.ajax({
        type: 'POST',
        url: '/passport/thirdlogin/bindMobile',
        data: {
            openId: $openId.val(),
            sourceType: $sourceType.val(),
            mobile: $mobile.val(),
            area: $area.val(),
            password: $passwordInput.val()
        }
    }).then(function(result) {
        if (result.code === 200) {
            window.local = result.data.refer;
        } else {
            errTip($next, result.message);
        }
    });
}

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

$next.on('click', function() {
    if ($next.hasClass('disable')) {
        return;
    }

    if ($passwordInput.val() !== $repasswordInput.val()) {
        errTip($repasswordInput, '两次输入的密码不一致,请重新输入');
        return;
    }
    nextPage();
});

ep.on('repwd', function(repwdAuth) {
    if (repwdAuth) {
        hideTip();
    }
});

ep.on('pwd', function(pwdAuth) {
    if (pwdAuth) {
        hideTip();
    }
});