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

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

    $captcha = $('#js-img-check'),
    useVerify = $captcha.data('userverify'), // 170406 是否使用验证

    pnPass = false,
    pwdPass = false;

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

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

let validate = {};

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

    validate.init();
}

// 登录按钮状态切换
function switchLoginBtnStatus() {
    let bool = !(pnPass && pwdPass);

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

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

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

            validate && validate.type === 2 && validate.refresh();
            if (data.code === 200) {
                res = data.data;
                showErrTip('登录成功');

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

                $loginBtn.text('登录成功');
                showErrTip('登录成功');
            } else {
                if (useVerify && data.captchaShow) {
                    ((data.changeCaptcha && validate.type !== 2) && validate.refresh());
                }

                showErrTip(data.message);
                resetForm();
            }
        },
        error: function() {
            showErrTip('网络断开连接啦~');
            $loginBtn.text('登录');

            validate && validate.refresh();
        }
    });
}

// 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() {
    let pwd = trim($pwd.val());

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

    switchLoginBtnStatus();
});

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

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

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

    if ((api.phoneRegx[areaCode].test(pn) || areaCode !== '+86') && api.pwdValidate(pwd)) {
        let params = {
            areaCode: areaCode.replace('+', ''),
            account: pn,
            password: pwd
        };

        if (useVerify) {
            validate.getResults().then((result) => {
                $loginBtn.text('正在登录...').addClass('disable');
                $.extend(params, result);
                loginAuth(params);
            });
        } else {
            loginAuth(params);
        }
    } else {
        showErrTip('账号或密码有错误,请重新输入');
        $loginBtn.text('登录').addClass('disable');
    }
});

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