cert.js 2.91 KB
/**
 * 第三方绑定完善个人信息
 * @author: wq
 * @date: 2016/1/27
 */
var $ = require('yoho-jquery');
var Captcha = require('../../plugins/captcha');
var second = ''; // 倒计时时间
var nopermissionoption = ''; // 倒计时的dom
var sendmessagehtml = ''; // 发送短信的dom
var captcha = new Captcha('#captcha').init();

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

nopermissionoption = $('#nopermissionmessage').html();
sendmessagehtml = $('.validatewrapper').html();
second = +$('.second').text();

function changeSecond() {
    second -= 1;
    if (second < 0) {
        second = 60;
        $('.validatewrapper').html(sendmessagehtml);
        return;
    } else {
        $('.second').text(second);
        window.setTimeout(changeSecond, 1000);
    }

}

/**
 * 启动倒计时
 * @return {[type]} [description]
 */
function circleTime() {
    $('.validatewrapper').html(nopermissionoption);
    window.setTimeout(changeSecond, 1000);
}

function sendMessageValidate() {
    var mobile = '';
    var area = '';

    $(document).on('click', '#sendmessage', function() {
        circleTime();
        mobile = $('#mobile').val();
        area = $('#area').val();
        $.ajax({
            type: 'POST',
            url: '/passport/cert/sendCertMsg',
            data: {
                mobile: mobile,
                area: area,
                verifyCode: captcha.getResults()
            }
        }).then(function(data) {
            if (data.data && data.data.needCaptcha) {
                captcha.show();
            }

            if (data.code === captcha.errorCode) {
                captcha.showTip(data.message);
                return;
            }

            if (data.code !== 200) {
                alert(data.message); // eslint-disable-line
            }
        });

    });
}

/**
 * 最终提交表单
 */
function actionSubmit() {
    var mobile = '';
    var area = '';
    var code = '';
    var refer = '';

    area = $('#area').val();
    mobile = $('#mobile').val();
    code = $('#validatenum').val();
    refer = $('#refer').val();

    $.ajax({
        type: 'POST',
        url: '/passport/cert/certMobile',
        data: {
            area: area,
            mobile: mobile,
            code: code,
            refer: refer
        }
    }).then(function(data) {
        if (data.code === 200) {
            window.location.href = data.data.nextUrl;
        } else {
            alert(data.message); // eslint-disable-line
        }
    });
}

/**
 * 确定完善信息
 * @return {[type]} [description]
 */
function actionConfirm() {
    var validatenum = '';

    $('#confirmsubmit').on('click', function() {
        validatenum = $('#validatenum').val();
        if (validatenum === '') {
            alert('短信验证码不能为空'); // eslint-disable-line
            return;
        }

        actionSubmit();
    });
}



function init() {
    sendMessageValidate();
    actionConfirm();
    circleTime();
}

init();