login.js 3.86 KB
/**
 * 登录
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/9/30
 */
var $ = require('yoho-jquery');
var ImgCheck = require('plugin/img-check');

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

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

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

    accPass = false,
    pwdPass = false;

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

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


var imgCheck = new ImgCheck($captcha, {
    useREM: {
        rootFontSize: 40,
        picWidth: 150
    }
});

if ($captcha.data('init') != null) { //eslint-disable-line
    imgCheck.init();
}

// 登录按钮状态切换
function switchLoginBtnStatus() {
    var 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();
}

// 密码显示与隐藏
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() {
    var acc = trim($account.val()),
        pwd = trim($pwd.val()),
        captcha = null;

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

    // if (imgCheck.atWorking) {
    captcha = imgCheck.getResults();

    //     if (captcha === '0000') {
    //         return tip.show(' 请将图片旋转到正确方向');
    //     }
    // }

    $loginBtn.text('正在登录...').addClass('disable');

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

        if (imgCheck.atWorking) {
            $.extend(data, {captcha});
        }

        $.ajax({
            type: 'POST',
            url: '/passport/login/auth',
            data,
            success: function(data) {
                var res;

                if (data.code === 200) {
                    res = data.data;

                    showErrTip('登录成功');
                    location.href = res.href;
                    $loginBtn.text('登录成功').off();
                } else {
                    if (data.captchaShow) {
                        imgCheck.atWorking ? (data.changeCaptcha && imgCheck.refresh()) : imgCheck.init();
                    }

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

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

                imgCheck.atWorking && imgCheck.refresh();
            },
            complete: function() {
                $loginBtn.text('登录').removeClass('disable');
            }
        });
    } 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');