new-password.js 2.05 KB
/**
 * 密码找回-新密码
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/8
 */
let $ = require('yoho-jquery');

let $pwd = $('#pwd'),
    $pwdLint = $('.pwd-lint'),
    $pwdLintTxt = $pwdLint.find('.pwd-lint-txt'),
    $btnOk = $('#btn-ok');

let api = require('../api');
let tip = require('plugin/tip');
let validatePWD = require('../password-check');

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

let $phoneNum = $('#phone-num');

api.bindEyesEvt({
    status: 'open'
});

$pwd.bind('input', function() {
    let val = $.trim(this.value);
    let bool = validatePWD(val, function(res) {
        $pwdLint.css({visibility: res.valid ? 'hidden' : 'visible'});

        if (!res.valid) {
            $pwdLintTxt.text(res.msg);
        }
    });

    $btnOk.toggleClass('disable', !bool);
});

$btnOk.on('touchstart', function() {
    let pwd = trim($pwd.val()),
        mobileBack = true,
        setting,
        url;

    if ($btnOk.hasClass('disable')) {
        return;
    }

    setting = {
        password: pwd
    };

    if ($phoneNum.length === 0) {
        mobileBack = false;
    }

    if (mobileBack) {
        $.extend(setting, {
            phoneNum: $phoneNum.val(),
            areaCode: $('#area-code').val(),
            token: $('#token').val()
        });

        url = '/passport/back/passwordByMobile';
    } else {
        $.extend(setting, {
            code: $('#email-code').val()
        });

        url = '/passport/back/passwordByEmail';
    }


    if (validatePWD(pwd)) {
        $.ajax({
            type: 'POST',
            url: url,
            data: setting,
            success: function(data) {
                if (data.code === 200) {
                    showErrTip('密码修改成功');

                    // 1000ms后跳转页面
                    setTimeout(function() {
                        location.href = data.data;
                    }, 1000);
                } else {
                    showErrTip(data.message);
                }
            }
        });
    } else {
        showErrTip('密码6-20位,请重新输入');
    }
});