login.js 5.24 KB
/**
 * 登录
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/9/30
 */
let $ = require('yoho-jquery');
let Validate = require('plugin/validata');

let $account = $('#account'),
    $pwd = $('#pwd'),
    $loginBtn = $('#btn-login'),

    $mask = $('#retrive-pwd-mask'),
    $ways = $('#retrive-pwd-ways'),

    $captcha = $('#js-img-check'),

    accPass = false,
    pwdPass = false;

let api = require('../api');
let tip = require('plugin/tip');

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


let validate = new Validate($captcha, {
    useREM: {
        rootFontSize: 40,
        picWidth: 150
    }
});

if ($captcha.data('userverify')) {
    validate.init();
}

// 登录按钮状态切换
function switchLoginBtnStatus() {
    let bool = true;

    bool = !(accPass && pwdPass);

    $loginBtn.toggleClass('disable', bool);
}

function resetForm() {
    // $pwd.val('').focus();
    $loginBtn.text('登录').addClass('disable');
}

// 显示找回密码面板
function showRetrivePanel() {
    $mask.show();
    $ways.show();
}

// 隐藏找回密码面板
function hideRetrivePanel() {
    $mask.hide();
    $ways.hide();
}

/**
 * 登录校验
 */
function loginAuth(params, acc) {
    $.ajax({
        type: 'POST',
        url: '/passport/login/auth',
        data: params,
        success: function(data) {
            let res,
                LOGI_TYPE;

            if (acc.indexOf('@') > 0) {
                LOGI_TYPE = 8;
            } else {
                LOGI_TYPE = 5;
            }

            if (window._yas && window._yas.sendCustomInfo) {
                window._yas.sendCustomInfo({
                    op: 'YB_MY_LOGIN_C',
                    param: JSON.stringify({
                        C_ID: window._ChannelVary[window.cookie('_Channel')],
                        LOGI_TYPE: LOGI_TYPE
                    })
                }, true);
            }
            validate && validate.type === 2 && validate.refresh();
            if (data.code === 200) {
                res = data.data;

                showErrTip('登录成功');
                location.href = res.href;
                $loginBtn.text('登录成功');
            } else if (data.code === 4189) {
                localStorage.loginJumpUrl = $('#account').val();
                localStorage.loginJump = 'true';
                location.href = data.url;
            } else {
                $captcha.data('userverify', data.captchaShow);
                if (data.captchaShow) {
                    if (validate.atWorking) {
                        ((data.changeCaptcha && validate.type !== 2) && validate.refresh());
                    } else {
                        validate.init();
                    }
                }

                showErrTip(data.message);
                resetForm();
            }

            return data;
        },
        error: function() {
            showErrTip('网络断开连接啦~');

            validate && validate.refresh();
        },
        complete: function() {
            $loginBtn.text('登录').removeClass('disable');
        }
    });
}

// 密码显示与隐藏
api.bindEyesEvt();

// 清空账号输入框
api.bindClearEvt();

$account.bind('input', function() {
    if (trim($account.val()) !== '') {
        accPass = true;
    } else {
        accPass = false;
    }
    switchLoginBtnStatus();
});

$pwd.bind('input', function() {
    if (trim($pwd.val()) === '') {
        pwdPass = false;
    } else {
        pwdPass = true;
    }
    switchLoginBtnStatus();
});


// Login
$loginBtn.on('touchstart', function() {
    if ($loginBtn.hasClass('disable')) {
        return;
    }
    let acc = trim($account.val()),
        pwd = trim($pwd.val());

    // 验证账号(数字或者邮箱)和密码合理性
    if ((/^[0-9]+$/.test(acc) || api.emailRegx.test(acc)) && api.pwdValidate(pwd)) {
        let params = {
            account: acc,
            password: pwd
        };

        if ($captcha.data('userverify')) {
            validate.getResults().then((result) => {
                $loginBtn.text('正在登录...').addClass('disable');

                $.extend(params, result);

                // auth
                loginAuth(params, acc);
            }, () => {});
        } else {
            loginAuth(params, acc);
        }
    } else {
        showErrTip('账号或密码有错误,请重新输入');
        $loginBtn.text('登录').removeClass('disable');
    }
});


$('#forget-pwd').on('touchstart', function() {
    showRetrivePanel();
});

$mask.on('touchstart', function() {
    hideRetrivePanel();
});

$('#cancel-retrive').on('touchstart', function(e) {
    e.preventDefault();
    hideRetrivePanel();
});

// 对初始有默认值的情况去初始化登录按钮状态
$account.trigger('input');
$pwd.trigger('input');

function getTypeName(name) {
    return {
        qq: 1,
        wechat: 4,
        weibo: 2,
        alipay: 3
    }[name];
}

$('.tp-link a').on('touchstart', function() {
    let logType = getTypeName($(this).prop('className'));

    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_MY_LOGIN_C',
            param: JSON.stringify({
                C_ID: window._ChannelVary[window.cookie('_Channel')],
                LOGI_TYPE: logType
            })
        }, true);
    }
});