third-login.js 7.75 KB
/**
 * 第三方登录首页
 * @author: TaoHuang
 * @date: 2016/7/12
 */
var $ = require('yoho-jquery');
var phoneRegx = require('../common/mail-phone-regx').phoneRegx;
var EventProxy = require('yoho-eventproxy');

var $regionCodeText = $('#region-code'),
    $phoneNumInput = $('#phone-num'),
    $imgCaptchaInput = $('#verifyCode'),
    $imgCaptchaCtrl = $('.img-captcha-refresh'),
    $smsCaptchaInput = $('#sms-captcha-input'),
    $smsCaptchaCtrl = $('.sms-captcha-send'),
    $phone = $('#phone'),
    $nextBtn = $('#validate-phone-next');

var $bindsetpwdForm = $('#bindsetpwd'),
    $bindConfirmForm = $('#bindConfirm'),
    $relateConfirmForm = $('#relateConfirm');

var $openId = $('#openId');
var $sourceType = $('#sourceType');
var $refer = $('#refer');

var second = 60,
    ep = new EventProxy();

var $errTip = $('.tips'),
    $errMsg = $errTip.find('.rectangle');

function errTip(ele, msg) {
    var topLeft = ele.offset();

    $errMsg.text(msg);
    return $errTip.css({
        top: topLeft.top + ele.height() - 2,
        left: topLeft.left,
        width: ele.width() + 2,
        height: ele.height
    }).removeClass('hide');
}

function disableSMSBtn() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $smsCaptchaCtrl.text('获取短信验证码');
        $smsCaptchaCtrl.removeClass('disable');
        $smsCaptchaCtrl.removeClass('progress');
    } else {
        $smsCaptchaCtrl.text(second + '秒后可重新操作');
        window.setTimeout(disableSMSBtn, 1000);
    }
}

function sendSMSCaptcha() {
    return $.ajax({
        type: 'POST',
        url: '/passport/autouserinfo/sendBindMsg',
        data: {
            mobile: $phoneNumInput.val(),
            area: $regionCodeText.text().replace('+', '')
        }
    });
}

function refreshImgCaptcha() {
    var time = new Date(),
        $captchaImg = $('.img-captcha'),
        captchaImgSrc = $captchaImg.attr('src').split('?')[0];

    $captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
}

function validateImgCaptcha() {
    return $.ajax({
        type: 'POST',
        url: '/passport/images/check',
        data: {
            verifyCode: $imgCaptchaInput.val()
        }
    });
}

function validateSMSCaptcha() {
    return $.ajax({
        type: 'POST',
        url: '/passport/autouserinfo/checkBindMsg',
        data: {
            code: $smsCaptchaInput.val(),
            mobile: $phoneNumInput.val(),
            area: $regionCodeText.text().replace('+', '')
        }
    });
}

ep.tail('phoneNum', 'img-captcha', 'sms-captcha', function(phoneAuth, imgAuth, smsAuth) {

    if (phoneAuth && imgAuth && smsAuth) {
        $nextBtn.removeClass('disable');
    } else {
        $nextBtn.addClass('disable');
    }

});

$('#region').change(function() {
    var $this = $(this);

    $regionCodeText.text($this.val());
});

ep.tail('phoneNum', 'img-captcha', function(phoneAuth, imgAuth) {
    if (phoneAuth && imgAuth && !$smsCaptchaCtrl.hasClass('progress')) {
        $smsCaptchaCtrl.removeClass('disable');
    } else {
        $smsCaptchaCtrl.addClass('disable');
    }
});

$phoneNumInput.on('blur', function() {
    var length = $phoneNumInput.val().length;

    if (length === 0) {
        errTip($phone, '请输入手机号码');
        ep.emit('phoneNum', false);
        return;
    }

    if (/^[0-9]+$/.test($phoneNumInput.val())) {

        // 这里只做中国区验证
        if ($regionCodeText.text() === '+86') {
            if ($phoneNumInput.length === 11 && phoneRegx['+86'].test($phoneNumInput.val())) {
                ep.emit('phoneNum', true);
                return;
            } else {
                errTip($phone, '手机号码不正确,请重新输入');
                ep.emit('phoneNum', false);
                return;
            }
        }
        ep.emit('phoneNum', true);
        return;
    } else {
        ep.emit('phoneNum', false);
        return;
    }
});

$imgCaptchaInput.on('blur', function() {
    var length = $imgCaptchaInput.val().length;

    switch (length) {
        case 4 :
            break;
        case 0:
            errTip($imgCaptchaInput, '请输入验证码');
            refreshImgCaptcha();
            ep.emit('img-captcha', false);
            break;
        default:
            errTip($imgCaptchaInput, '验证码不正确');
            refreshImgCaptcha();
            ep.emit('img-captcha', false);
            break;
    }

    validateImgCaptcha().then(function(result) {
        if (result.code === 200) {
            ep.emit('img-captcha', true);
        } else {
            ep.emit('img-captcha', false);
            refreshImgCaptcha();
        }
    });
});

$imgCaptchaCtrl.on('click', function() {
    refreshImgCaptcha();
});

$smsCaptchaInput.on('blur', function() {
    var length = $smsCaptchaInput.val().length;

    switch (length) {
        case 4:
            break;
        case 0:
            errTip($smsCaptchaInput, '请输入短信验证码');
            ep.emit('sms-captcha', false);
            return;
        default:
            errTip($smsCaptchaInput, '验证码不正确');
            ep.emit('sms-captcha', false);
            return;
    }

    validateSMSCaptcha().then(function(result) {
        if (result.code === 200) {
            ep.emit('sms-captcha', true);
        } else {
            ep.emit('sms-captcha', false);
            errTip($smsCaptchaInput, '验证码不正确');
        }
    });
});

$smsCaptchaCtrl.on('click', function() {
    if ($smsCaptchaCtrl.hasClass('disable')) {
        return;
    }

    $smsCaptchaCtrl.addClass('disable');
    $smsCaptchaCtrl.addClass('progress');

    disableSMSBtn();
    sendSMSCaptcha();
});

function setUserInfo(user) {
    $('.username').val(user.username);
    $('.headImg').val(user.headImg);
}

function setThirdPartInfo(third) {
    $('.openId').val(third.openId);
    $('.sourceType').val(third.sourceType);
    $('.refer').val(third.refer);
    $('.mobile').val(third.mobile);
    $('.area').val(third.area);
    $('.verifyCode').val(third.verifyCode);
    $('.code').val(third.code);
}

function setPwdPage(thirdPart) {
    setThirdPartInfo(thirdPart);
    $bindsetpwdForm.submit();
}

function bindConfirmPage(thirdPart, user) {
    setThirdPartInfo(thirdPart);
    setUserInfo(user);
    $bindConfirmForm.submit();
}

function relateConfirmPage(thirdPart, user) {
    setThirdPartInfo(thirdPart);
    setUserInfo(user);
    $relateConfirmForm.submit();
}

function nextPage() {
    var thirdPart = {
        mobile: $phoneNumInput.val(),
        area: $regionCodeText.text().replace('+', ''),
        openId: $openId.val(),
        sourceType: $sourceType.val(),
        verifyCode: $imgCaptchaInput.val(),
        code: $smsCaptchaInput.val(),
        refer: $refer.val()
    };

    return $.ajax({
        type: 'post',
        url: '/passport/autouserinfo/bindCheck',
        data: {
            mobile: thirdPart.mobile,
            area: thirdPart.area,
            openId: thirdPart.openId,
            sourceType: thirdPart.sourceType
        }
    }).then(function(result) {
        switch (result.code) {
            case 200:
                // 未注册,可绑定,设定密码页面;
                setPwdPage(thirdPart);
                break;
            case 201:
            case 205:
                // 已注册绑定过其他的第三方,绑定确定页面
                bindConfirmPage(thirdPart, result.data.user);
                break;
            case 203:
                // 关联帐号,关联页面
                relateConfirmPage(thirdPart, result.data.user);
                break;
            default:
                // 出错
                errTip($nextBtn, '输入错误,请重新输入!');
                break;
        }
    });
}

$nextBtn.on('click', function() {
    if ($nextBtn.hasClass('disable')) {
        return;
    }

    nextPage();
});