Blame view

public/js/passport/back/back.js 4.27 KB
姜枫 authored
1 2 3 4 5 6
/**
 * 找回密码
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/12/14
 */
王水玲 authored
7 8 9
var $ = require('yoho-jquery');

var regx = require('../common/mail-phone-regx');
姜枫 authored
10
htoooth authored
11 12
var Captcha = require('../../plugins/captcha');
姜枫 authored
13 14
var emailAc = require('../common/ac-email'); // 邮箱自动完成
王水玲 authored
15 16 17
var emailReg = regx.emailRegx,
    phoneRegx = regx.phoneRegx;
姜枫 authored
18 19 20 21 22 23 24 25 26
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,
htoooth authored
27
    captcha = new Captcha('#captcha-img').init();
姜枫 authored
28
王水玲 authored
29
require('../../simple-header');
姜枫 authored
30
require('yoho-jquery-placeholder');
htoooth authored
31
require('../../common/promise');
姜枫 authored
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

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

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

    return {
        pass: pass,
        errTxt: errTxt
    };
}
htoooth authored
66
function validatePhone() {
姜枫 authored
67 68 69 70 71 72 73 74 75
    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');
    }
htoooth authored
76 77 78 79 80 81

    return pnVa.pass;
}

emailAc($phoneNum, function() {
    validatePhone();
htoooth authored
82
});
姜枫 authored
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

$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');
});
htoooth authored
136
// 下一步
htoooth authored
137 138 139 140 141
$btn.click(function(e) {
    if (!validatePhone()) {
        return;
    }
姜枫 authored
142 143 144
    if (/^[0-9]+$/.test($.trim($phoneNum.val()))) {
        $('#find-form').attr('action', '/passport/back/mobile');
    }
htoooth authored
145 146

    if (hasPh) {
147 148 149 150 151 152 153 154 155 156 157 158
        $.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.code === 402) {
                $accErr.removeClass('hide').find('em').text('该账号不存在');
htoooth authored
159
                $phoneNum.addClass('error');
160 161 162 163 164 165 166 167 168 169 170 171 172
                captcha.refresh();
                return $.Deferred().reject().promise();//eslint-disable-line
            }

            if (result.code === 405) {
                captcha.showTip(result.message);
                $accErr.addClass('hide');
                $phoneNum.removeClass('error');
                return;
            }

            $accErr.removeClass('hide').find('em').text(result.message);
            $phoneNum.addClass('error');
htoooth authored
173
        });
姜枫 authored
174
    }
htoooth authored
175 176 177

    e.preventDefault();
    return true;
姜枫 authored
178
});
htoooth authored
179 180

captcha.onSuccess(function() {
htoooth authored
181
    $btn.triggerHandler('click');
htoooth authored
182
});