relate.js 2.96 KB
/**
 * 第三方关联
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/4/11
 */
var $ = require('yoho-jquery');

var $relateMain = $('#relate-main'),
    $codeValidate = $('#code-validate'),
    $vaWrapper = $codeValidate.find('.validate-wrapper'),
    $msgCode = $codeValidate.find('.msg-code'),
    $second = $codeValidate.find('.count-down span'),
    $errTip = $codeValidate.find('.code-error'),
    $hideDom = $('#hide-info');

var relate = {};

var circleTime = {};

require('../../simple-header');

relate = $hideDom.data();
$hideDom.remove();

// 倒计时
circleTime = {
    init: function() {
        if (this.timer) {
            window.clearTimeout(this.timer);
        }
        this.second = 60;
        $vaWrapper.addClass('waiting');
        this.changeTime();
    },
    changeTime: function() {
        var that = this;

        if (this.second && this.second > 0) {
            $second.text(this.second);
            this.second--;
            this.timer = window.setTimeout(function() {
                that.changeTime();
            }, 1000);
        } else {
            this.clearTime();
        }
    },
    clearTime: function() {
        if (this.timer) {
            window.clearTimeout(this.timer);
        }
        $vaWrapper.removeClass('waiting');
    }
};

function sendVerifyMsg() {
    circleTime.init();

    $.ajax({
        type: 'POST',
        url: '/passport/autouserinfo/sendBindMsg',
        data: {
            mobile: relate.mobile,
            area: relate.area
        }
    }).then(function(jsonData) {
        if (jsonData && jsonData.code !== 200) {
            circleTime.clearTime();
            alert(jsonData.message);
        }
    });
}

function relateMobile() {
    var code = $msgCode.val();

    if (!code) {
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/passport/autouserinfo/relateMobile',
        data: {
            mobile: relate.mobile,
            area: relate.area,
            openId: relate.openid,
            sourceType: relate.source,
            code: code
        }
    }).then(function(jsonData) {
        if (jsonData.code === 200) {
            window.location.href = jsonData.data.refer;
        } else if (jsonData.code === 402) {
            $errTip.removeClass('hide');
        } else {
            $errTip.addClass('hide');
            alert(jsonData.message);
        }
    });
}

$('#next-step-btn').click(function() {
    sendVerifyMsg();
    $relateMain.addClass('hide');
    $errTip.addClass('hide');
    $codeValidate.removeClass('hide');
});

$codeValidate.on('keydown', '.msg-code', function(e) {
    if (e.keyCode === 13) {
        relateMobile();
        return false;
    }
});

$codeValidate.on('click', '.get-code', function() {
    sendVerifyMsg();
});

$codeValidate.on('click', '.perv-step', function() {
    circleTime.clearTime();
    $relateMain.removeClass('hide');
    $codeValidate.addClass('hide');
    $msgCode.val('');
});

$('#relate-btn').click(function() {
    relateMobile();
});