sms-login.page.js 3.74 KB
'use strict';

let tip, api, checkPoint;

let $countrySelect,
    $areaCode,
    $nextBtn,
    $resetBtn,
    $phoneNum,
    $mask = $('#retrive-pwd-mask'),
    $ways = $('#retrive-pwd-ways');

let page;

require('common');
tip = require('plugin/tip');
api = require('./api');
checkPoint = require('./smslogin/check-point');


// 图片验证码
let Validate = require('plugin/validata');

let validate = new Validate('#js-img-check', {
    useREM: {
        rootFontSize: 40,
        picWidth: 150
    }
});

validate.init();

require('./login/qr-check');


page = {
    init: function() {
        this.domInit();
        this.bindEvent();
        this.toggleNextBtn();
    },
    domInit: function() {
        $countrySelect = $('#country-select');
        $areaCode = $('#area-code');
        $nextBtn = $('#btn-next');
        $phoneNum = $('#phone-num');
        $resetBtn = $('.clear-input');
    },
    bindEvent: function() {
        let self = this;

        $countrySelect.on('change', function() {
            $areaCode.text(this.value);
        });
        $phoneNum.on('input', function() {
            self.toggleNextBtn();
        });

        $nextBtn.on('click', function() {
            self.goNext();
        });

        $resetBtn.on('click', function() {
            $phoneNum.val('');
            $nextBtn
                .prop('disabled', true)
                .toggleClass('disable', true);
            $resetBtn.hide();
        });
        $('#forget-pwd').on('touchstart', () => {
            this.showRetrivePanel();
        });

        $mask.on('touchstart', () => {
            this.hideRetrivePanel();
        });

        $('#cancel-retrive').on('touchstart', (e) => {
            e.preventDefault();
            this.hideRetrivePanel();
        });
    },
    showRetrivePanel: () => {
        $mask.show();
        $ways.show();
    },
    hideRetrivePanel: () => {
        $mask.hide();
        $ways.hide();
    },

    // 切换$nextBtn disable状态
    toggleNextBtn: function() {
        let bool = Boolean($.trim($phoneNum.val()));

        $nextBtn
            .toggleClass('disable', !bool)
            .prop('disabled', !bool);

        $resetBtn.toggle(bool);
    },

    // 提交按钮
    goNext: function() {
        let areaCode = $countrySelect.val();
        let phone = $.trim($phoneNum.val());

        if ($nextBtn.prop('disabled')) {
            return;
        }

        if (!api.phoneRegx[areaCode].test(phone)) {
            tip.show('手机号码格式不正确, 请重新输入');
            return;
        }

        validate.getResults().then((result) => {
            $nextBtn.prop('disabled', true);
            let params = {
                area: areaCode.replace('+', ''),
                mobile: phone
            };

            $.extend(params, result);
            $.post('/passport/sms_login/step1_check', params).done(function(data) {
                validate.type === 2 && validate.refresh();
                if (data.code === 200) {
                    checkPoint('YB_MOBILE_NEXT_C'); // 埋点
                    // $nextBtn.off();
                    location.href = data.redirect;
                } else {
                    (data.changeCaptcha && validate.type !== 2) && validate.refresh();
                    tip.show(data.message);
                }
            })
                .fail(function() {
                    validate.refresh();
                    tip.show('出错了, 请重试');
                })
                .always(function() {
                    $nextBtn.prop('disabled', false);
                });
        });
    }
};

$(function() {
    page.init();
});

// 多次登录失败跳短信认证填充手机号
$('#phone-num').val(window.cookie('loginJumpUrl'));
window.setCookie('loginJumpUrl', '', {path: '/passport'});