password.js 3.9 KB
/**
 * 注册-密码
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/8
 */
var $ = require('yoho-jquery');

var $pwd = $('#pwd'),
    $pwdLint = $('.pwd-lint'),
    $pwdLintTxt = $pwdLint.find('.pwd-lint-txt'),
    $btnSure = $('#btn-sure');

var api = require('../api');
var tip = require('plugin/tip');
var validatePWD = require('../password-check');

var trim = $.trim;
var showErrTip = tip.show;
var qs;

require('common');

api.bindEyesEvt({
    status: 'open' // 默认眼睛打开
});

$pwd.bind('input', function() {
    var val = $.trim(this.value);
    var bool = validatePWD(val, function(res) {
        $pwdLint.css({visibility: res.valid ? 'hidden' : 'visible'});

        if (!res.valid) {
            $pwdLintTxt.text(res.msg);
        }
    });

    $btnSure.toggleClass('disable', !bool);
});


qs = window.queryString;

if (qs.selected && qs.selected === 'N') {
    $('.pitch').removeClass('select').html('&#xe647;');
}

if (qs.pwd) {
    $pwd.val(qs.pwd);

    if (trim($pwd.val()) === '') {
        $btnSure.addClass('disable');
    } else {
        $btnSure.removeClass('disable');
    }
}

$('.pitch').on('click', function() {
    if ($('.pitch').hasClass('select')) {
        $(this).removeClass('select');
        $(this).html('&#xe647;');
    } else {
        $(this).addClass('select');
        $(this).html('&#xe60a;');
    }
});

function setPassword() {
    $btnSure.addClass('disable');
    return $.ajax({
        type: 'POST',
        url: '/passport/reg/setpassword',
        data: {
            password: trim($pwd.val()),
            phoneNum: $('#phone-num').val(),
            areaCode: $('#area-code').val(),
            smsCode: $('#sms-code').val(),
            token: $('#token').val()
        },
        success: function(data) {
            var res = data.data;

            if (data.code === 200) {
                showErrTip('注册成功');

                // 统计代码:用于统计从哪个渠道注册成功的
                if (window._yas && window._yas.sendCustomInfo) {
                    window._yas.sendCustomInfo({
                        op: 'YB_REGISTER_SUCCESS_L',
                        ud: window.getUid(),
                        param: JSON.stringify({
                            C_ID: window._ChannelVary[window.cookie('_Channel')] || 1,
                            UNION_TYPE: window.queryString.union_type || window.cookie('unionTypeYas') || false
                        })
                    }, true);
                }
                setTimeout(function() {
                    location.href = res.href;
                }, 1500);
            } else {
                $btnSure.removeClass('disable');
                showErrTip(data.message);
            }
        },
        error: function(data) {
            $btnSure.removeClass('disable');

            if (data && data.responseJSON && data.responseJSON.message) {
                showErrTip(data.message);
            }
        }
    });
}

$btnSure.on('touchstart', function() {
    var pwd = trim($pwd.val());

    if ($btnSure.hasClass('disable')) {
        return;
    }

    if (!validatePWD(pwd)) {
        showErrTip('密码6-20位,请重新输入');
    } else {
        if ($('.pitch').hasClass('select')) {
            setPassword();

        } else {
            $('.prompt').show();

            $('.ensure').on('click', function() {
                $('.prompt').hide();
                $('.pitch').addClass('select');
                $('.pitch').html('&#xe60a;');

                setPassword();
            });

            $('.deny').on('click', function() {
                location.href = '//m.yohobuy.com/passport/agreement' + window.location.search + '&pwd=' + pwd;
            });
        }
    }
});

$('.agreement-detail').on('click', function() {
    $(this).attr('href', '//m.yohobuy.com/passport/agreement' + window.location.search);
});


// 如果有值, 立刻校验
if ($pwd.val()) {
    $pwd.triggerHandler('input');
}