login.js 2.65 KB
/**
 * 登录
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/9/30
 */
var $ = require('yoho.zepto');

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

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

    accPass = false,
    pwdPass = false;

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

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

//登录按钮状态切换
function switchLoginBtnStatus() {
    if (accPass && pwdPass) {
        $loginBtn.removeClass('disable');
    } else {
        $loginBtn.addClass('disable');
    }
}

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

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

//密码显示与隐藏
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());

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

    //验证账号(数字或者邮箱)和密码合理性
    if ((/^[0-9]+$/.test(acc) || api.emailRegx.test(acc)) && api.pwdValidate(pwd)) {
        $.ajax({
            type: 'POST',
            url: '/passport/login/auth',
            data: {
                account: acc,
                password: pwd
            },
            success: function(data) {
                if (data.code === 200) {
                    showErrTip('登录成功');

                    //1s后跳转页面
                    setTimeout(function() {
                        location.href = data.data;
                    }, 1000);
                } else {
                    showErrTip(data.message);
                }
            },
            error: function() {
                showErrTip('网络断开连接啦~');
            }
        });
    } else {
        showErrTip('账号或密码有错误,请重新输入');
    }
});


$('#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');