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

require('js/common');

module.exports = function(useInRegister, useForBind, useForRelate) {
    let $captcha = $('#captcha'),
        $btnNext = $('#btn-next'),
        $captchaTip = $('#captcha-tip'),
        nickname = $('#nickname').val(),
        sourceType = $('#sourceType').val(),
        openId = $('#openId').val(),
        phoneNum = $('#phone-num').val(),
        areaCode = $('#area-code').val().replace('+', '');

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

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

    let urlMid = useInRegister ? 'reg' : 'back';

    let isReg = parseInt($('#isReg').val(), 10);

    function startBind() {
        $.ajax({
            url: useForBind ? '/passport/bind/bindMobile' : '/passport/bind/relateMobile',
            type: 'post',
            data: {
                areaCode: areaCode.replace('+', ''),
                phoneNum: phoneNum,
                openId: openId,
                sourceType: sourceType,
                nickname: nickname,
                password: '',
                code: trim($captcha.val())
            },
            success: function(res) {
                if (res.code === 200) {
                    tip.show('登录成功');
                    setTimeout(function() {
                        location.href = res.data.refer;
                    }, 2000);
                } else {
                    tip.show(res.message);
                }
            },
            error: function() {
                tip.show('登录失败,请重试!');
            }
        });
    }

    function startReg() {
        $.ajax({
            type: 'POST',
            url: '/passport/' + urlMid + '/verifycode',
            data: {
                phoneNum: phoneNum,
                areaCode: areaCode,
                code: trim($captcha.val()),
                token: $('#token').val()
            },
            success: function(data) {
                if (data.code === 200) {
                    location.href = data.data;
                } else {
                    // 验证码不正确,显示提示
                    showErrTip(data.message);
                    if (data.refer) {
                        location.href = data.refer;
                    }
                }

            }
        });
    }

    function countDown(during) {
        let count = during || 59,
            itime;

        itime = setInterval(function() {
            if (count === 0) {
                $captchaTip.text('重新发送').removeClass('disable');
                clearInterval(itime);
            } else {
                $captchaTip.text('重新发送 (' + count-- + '秒)');
                window.setCookie('count', count);

                if (during && parseInt(during, 10) !== 0) {
                    $captchaTip.addClass('disable');
                }
            }
        }, 1000);
    }

    api.bindClearEvt();

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

    // 重新发送验证码
    $captchaTip.on('touchstart', function() {
        if ($captchaTip.hasClass('disable')) {
            return;
        }

        $.ajax({
            type: 'POST',
            url: (useForBind || useForRelate) ? '/passport/bind/sendBindMsg' : '/passport/' + urlMid + '/sendcode',
            data: {
                phoneNum: phoneNum,
                areaCode: areaCode
            },
            success: function(data) {
                if (data.code === 200) {
                    $captchaTip.text('重发验证码 (60秒)').addClass('disable');
                    countDown();
                } else {
                    data.during && countDown(data.during);

                    // 验证码不正确,显示提示
                    showErrTip(data.message);
                }
            }
        });
    });

    $btnNext.on('touchstart', function() {
        if ($btnNext.hasClass('disable')) {
            return;
        }

        if (useForBind || useForRelate) {
            if (isReg) {
                startBind();
            } else {
                location.href = '/passport/bind/password?phoneNum=' +
                    phoneNum + '&areaCode=' + areaCode + '&openId=' +
                    openId + '&sourceType=' + sourceType + '&nickname=' + nickname + '&code=' + trim($captcha.val());
            }

        } else {
            startReg();
        }
    });

    if (window.cookie('count') && parseInt(window.cookie('count'), 10) > 0) {
        countDown(window.cookie('count'));
    } else {
        countDown();
    }
};