bind.js 4.75 KB
/**
 * 注册
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/8
 */
let $ = require('yoho-jquery');

let $phoneNum = $('#phone-num'),
    $countrySelect = $('#country-select'),
    $areaCode = $('#area-code'),
    $openId = $('#openId'),
    $nickname = $('#nickname'),
    $sourceType = $('#sourceType'),
    $btnNext = $('#btn-next');

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

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

let requested = false;

require('common');

let Validate = require('plugin/validata');

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

validate.init();

function nextStep(url, mobileNo, areaCode) {
    if (requested) {
        return false;
    }

    requested = true;
    $btnNext.addClass('disable').html('绑定中...');
    $.ajax({
        type: 'POST',
        url: '/passport/bind/sendBindMsg',
        data: {
            phoneNum: mobileNo,
            areaCode: areaCode.replace('+', '')
        },
        success: function(res) {

            // console.log(res.data);
            if (res.code === 200) {

                // 统计代码:用于统计从哪个渠道登录
                if (window._yas && window._yas.sendCustomInfo) {
                    window._yas.sendCustomInfo({
                        loginRefer: $sourceType.val()
                    }, false);
                }

                location.href = url;
            } else {
                tip.show(res.message);
            }
        },
        error: function() {
            tip.show('出错了,请重试!');
            $btnNext.removeClass('disable').html('下一步');
            requested = false;
        }
    });
}

api.selectCssHack($('#country-select'));

api.bindClearEvt();

$phoneNum.bind('input', function() {
    if (trim($phoneNum.val()) === '') {
        $btnNext.addClass('disable');
    } else {
        $btnNext.removeClass('disable');
    }
});

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

$btnNext.on('touchstart', function() {
    let pn = trim($phoneNum.val()),
        openId = trim($openId.val()),
        nickname = trim($nickname.val()),
        sourceType = trim($sourceType.val()),
        areaCode = $countrySelect.val();

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

    if (api.phoneRegx[areaCode].test(pn)) {
        validate.getResults().then((result) => {
            let params = {
                areaCode: areaCode.replace('+', ''),
                phoneNum: pn,
                openId: openId,
                sourceType: sourceType,
                nickname: nickname
            };

            $.extend(params, result);
            $.ajax({
                url: '/passport/bind/bindCheck',
                type: 'POST',
                data: params,
                success: function(res) {
                    validate.type === 2 && validate.refresh();
                    if (res.code === 200) {
                        if (res.data.isReg === 1) {
                            dialog.showDialog({
                                dialogText: '该手机号已注册过有货\n' + pn + ',确定绑定吗?',
                                hasFooter: {
                                    leftBtnText: '更换号码',
                                    rightBtnText: '继续绑定'
                                }
                            }, function() {
                                nextStep(res.data.next, pn, areaCode);
                            });
                        } else if (res.data.isReg === 3) {
                            dialog.showDialog({
                                dialogText: '绑定失败,该手机号已注册,请更换或使用该手机号直接登录',
                                hasFooter: {
                                    centerBtnText: '确定'
                                }
                            });
                        } else {
                            nextStep(res.data.next, pn, areaCode);
                        }
                    } else {
                        if (res.captchaShow) {
                            ((res.changeCaptcha && validate.type !== 2) && validate.refresh());
                        }
                        showErrTip(res.message);
                    }
                },
                error: function() {
                    showErrTip('网络断开连接啦~');
                    validate.refresh();
                }
            });
        });
    } else {
        showErrTip('手机号格式不正确,请重新输入');
    }
});

if (/MicroMessenger/i.test(navigator.userAgent) && $.queryString().refer) {
    $('.go-back').attr('href', decodeURIComponent($.queryString().refer));
}