international.js 4 KB
/**
 * 国际账号登录
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/8
 */
var $ = require('yoho-jquery');
var SlideUnlock = require('plugin/slideunlock');

var $phoneNum = $('#phone-num'),
    $countrySelect = $('#country-select'),
    $areaCode = $('#area-code'),
    $pwd = $('#pwd'),
    $loginBtn = $('#btn-login'),

    slideUnlock,

    pnPass = false,
    pwdPass = false;

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

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

// 登录按钮状态切换
function switchLoginBtnStatus() {
    var bool = slideUnlock ?
        !(slideUnlock.isOk && pnPass && pwdPass) :
        !(pnPass && pwdPass);

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

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

// Android-UC下显示select的direction:rtl无效的临时解决办法
api.selectCssHack($countrySelect);

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

// 清空手机号码
api.bindClearEvt();

$phoneNum.bind('input', function() {
    if (trim($phoneNum.val()) === '') {
        pnPass = false;
    } else {
        pnPass = true;
    }

    switchLoginBtnStatus();
});

$pwd.bind('input', function() {
    var pwd = trim($pwd.val());

    if (pwd === '') {
        pwdPass = false;
    } else {
        pwdPass = true;
    }

    switchLoginBtnStatus();
});

$countrySelect.change(function() {
    $areaCode.text($countrySelect.val());
});

$loginBtn.on('touchstart', function() {
    var pn = trim($phoneNum.val()),
        areaCode = $countrySelect.val(),
        pwd = trim($pwd.val());

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

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

    if (api.phoneRegx[areaCode].test(pn) && api.pwdValidate(pwd)) {
        $.ajax({
            type: 'POST',
            url: '/passport/login/auth',
            data: {
                areaCode: areaCode.replace('+', ''),
                account: pn,
                password: pwd
            },
            success: function(data) {
                var res,
                    time;

                if (data.code === 200) {
                    res = data.data;
                    showErrTip('登录成功');

                    $.ajax({
                        url: res.session,
                        dataType: 'jsonp',
                        success: function() {
                            clearTimeout(time);

                            // Cookie写入成功后,1s后跳转页面
                            setTimeout(function() {
                                location.href = res.href;
                            }, 1000);
                        }
                    });

                    // 3秒后强制跳转
                    time = setTimeout(function() {
                        location.href = res.href;
                    }, 3000);

                    $loginBtn.text('登录成功').off();
                    showErrTip('登录成功');
                } else {
                    if (!slideUnlock && data.slideUnlock) {
                        slideUnlock = new SlideUnlock('#js-slideunlock', {}, switchLoginBtnStatus);
                        slideUnlock.init();
                    } else {
                        slideUnlock && slideUnlock.reset();
                    }


                    showErrTip(data.message);
                    resetForm();
                }
            },
            error: function() {
                showErrTip('网络断开连接啦~');
                slideUnlock && slideUnlock.reset();
                $loginBtn.text('登录');
            }
        });
    } else {
        showErrTip('账号或密码有错误,请重新输入');
        $loginBtn.text('登录').addClass('disable');
    }
});

if ($('#js-slideunlock').data('init') === true) {
    slideUnlock = new SlideUnlock('#js-slideunlock', {}, switchLoginBtnStatus);
    slideUnlock.init();
}

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