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

var tip, api, checkPoint;

var $countrySelect,
    $areaCode,
    $nextBtn,
    $resetBtn,
    $captcha,
    $captchaPNG,
    $phoneNum;

var page;

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


// 图片验证码
let ImgCheck = require('plugin/img-check');

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

imgCheck.init({
    imgSrc: $('#js-img-check').find('input').val()
});



page = {
    init: function() {
        this.domInit();
        this.bindEvent();
    },
    domInit: function() {
        $countrySelect = $('#country-select');
        $areaCode = $('#area-code');
        $nextBtn = $('#btn-next');
        $phoneNum = $('#phone-num');
        $resetBtn = $('.clear-input');
    },
    bindEvent: function() {
        var 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();
        });
    },

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

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

        $resetBtn.toggle(bool);
    },

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

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

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

        if (captcha === '0000') {
            tip.show('请将图片旋转到正确位置');
            return;
        }

        $nextBtn.prop('disabled', true);
        $.post('/passport/sms_login/step1_check', {
            area: areaCode.replace('+', ''),
            mobile: phone,
            captcode: captcha
        })
            .done(function(data) {
                if (data.code === 200) {
                    checkPoint('YB_MOBILE_NEXT_C'); // 埋点
                    $nextBtn.off();
                    location.href = data.redirect;
                } else {
                    imgCheck.refresh();
                    tip.show(data.message);
                }
            })
            .fail(function(error) {
                var message = error && error.message || '';

                tip.show(message || '出错了, 请重试');
            })
            .always(function() {
                $nextBtn.prop('disabled', false);
            });
    }
};

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