relate.js 2.7 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'),
    $hideDom = $('#hide-info');

var relate = {};

var circleTime = {};

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 {
            alert(jsonData.message);
        }
    });
}

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

$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();
});