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

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

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

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

require('common');

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

$pwd.bind('input', function() {
    let val = $.trim(this.value);
    let 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(),
            inviteCode: $('#invite-code').val()
        },
        success: function(data) {
            let res = data.data;

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

                window.setCookie('refer', data.session);
                window.setCookie('msqDelivery', data.msqDelivery);

                // 统计代码:用于统计从哪个渠道注册成功的
                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() {
    let 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');
}