back.js 3.9 KB
/**
 * 找回密码
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/12/14
 */

var $ = require('yoho-jquery');

var regx = require('../common/mail-phone-regx');

var Captcha = require('../../plugins/captcha');

var emailAc = require('../common/ac-email'); // 邮箱自动完成

var emailReg = regx.emailRegx;

var $cr = $('#country-code-hide'),
    $phoneNum = $('#phone-num'),
    $ca = $('#captcha'),
    $ccList = $('#country-code-list'),
    $cc = $('#country-code'),
    $btn = $('#find-btn'),
    $accErr = $('#account-err'),
    caCount = 4, // 验证码位数
    hasPh = false,
    $captchaWrap = $('#captcha-img'),
    captcha = new Captcha('#captcha-img').init();

require('../../simple-header');
require('yoho-jquery-placeholder');
require('../../common/promise');

function vaPn(v) {
    var pass = true,
        errTxt = '';

    v = $.trim(v);
    if (v !== '') {
        if (/^[0-9]+$/.test(v)) {
            pass = true;
        } else {
            if (emailReg.test(v)) {
                pass = true;
            } else {
                errTxt = '邮箱格式不正确, 请重新输入';
                pass = false;
            }
        }
    } else {
        errTxt = '账户名不能为空';
        pass = false;
    }
    hasPh = pass;

    return {
        pass: pass,
        errTxt: errTxt
    };
}

function validatePhone() {
    var pnVa = vaPn($phoneNum.val());

    if (pnVa.pass) {
        $accErr.addClass('hide');
        $phoneNum.removeClass('error');
    } else {
        $accErr.removeClass('hide').find('em').text(pnVa.errTxt);
        $phoneNum.addClass('error');
    }

    return pnVa.pass;
}

function showCaptchaImg() {
    $captchaWrap.removeClass('hide');
}

emailAc($phoneNum, function() {
    validatePhone();
});

$ca.attr('maxlength', caCount);

// IE8 placeholder
$('input').placeholder();

$cc.on('click', function(e) {
    e.stopPropagation();
    if ($ccList.css('style') === 'block') {
        $ccList.slideUp('fast');
    } else {
        $ccList.slideDown('fast');
    }
});

$ccList.delegate('li', 'click', function(e) {
    var $cur = $(this),
        code = $cur.data('cc'),
        pnVa;

    e.stopPropagation();
    $cr.val(code);
    $cc.find('em').html($cur.text());

    // 切换后验证手机号码
    if ($.trim($phoneNum.val()) !== '') {
        pnVa = vaPn($phoneNum.val());
        if (hasPh) {
            $accErr.addClass('hide');
            $phoneNum.removeClass('error');
        } else {
            $accErr.removeClass('hide').text(pnVa.errTxt);
            $phoneNum.addClass('error');
        }
    }
    $ccList.slideUp('fast');
});

$(document).click(function() {
    if ($ccList.css('display') === 'block') {
        $ccList.slideUp();
    }
});

$phoneNum.keyup(function() {
    vaPn($.trim($(this).val()));
}).focus(function() {
    $(this).removeClass('error');

    // focus隐藏错误提示
    $accErr.addClass('hide');
});

// 下一步
$btn.click(function(e) {
    if (!validatePhone()) {
        return;
    }

    if (/^[0-9]+$/.test($.trim($phoneNum.val()))) {
        $('#find-form').attr('action', '/passport/back/mobile');
    }

    if (hasPh) {
        $.post('/passport/back/email', {
            verifyCode: captcha.getResults(),
            phoneNum: $('#phone-num').val(),
            area: $('#country-code-hide').val()
        }).then(function(result) {
            if (result.code === 200) {
                window.jumpUrl(result.data.refer);
                return;
            }

            if (result.data && result.data.needCaptcha) {
                showCaptchaImg();
            }

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

            $accErr.removeClass('hide').find('em').text(result.message);
            $phoneNum.addClass('error');
        });
    }

    e.preventDefault();
    return true;
});

captcha.onSuccess(function() {
    $btn.triggerHandler('click');
});