reset.page.js 2.85 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-13 14:43:19
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-19 14:51:03
 */

/* ***************
 * 密码重置
 *****************/

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

require('common');

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

let $resetForm = $('.reset-form');
let $oldPasswordInput = $('#oldPassword');
let $newPasswordInput = $('#newPassword');
let $oldEye = $resetForm.find('#oldEye');
let $newEye = $resetForm.find('#newEye');
let $sureResetBtn = $('#sureResetBtn');
let $tipBottom = $('#tipBottom');

// 登录按钮状态切换
function switchLoginBtnStatus() {
    let active = ($oldPasswordInput.val() && $newPasswordInput.val()) ? true : false;

    $sureResetBtn.toggleClass('active', active);
}

/**
 * 是否显示 TIP
 */

$newPasswordInput.focus(function() {
    $tipBottom.addClass('show');
});

$newPasswordInput.blur(function() {
    $tipBottom.removeClass('show');
});

$oldPasswordInput.bind('input', function() {
    switchLoginBtnStatus();
});

$newPasswordInput.bind('input', function() {
    if (!$tipBottom.hasClass('show')) {
        $tipBottom.addClass('show');
    }
    switchLoginBtnStatus();
});


/**
 * 旧密码显示隐藏
 */
$oldEye.on('click', function() {
    if ($oldEye.hasClass('close')) {
        $oldPasswordInput.attr('type', 'text');
        $oldEye.removeClass('close');
    } else {
        $oldPasswordInput.attr('type', 'password');
        $oldEye.addClass('close');
    }
});

/**
 * 新密码显示隐藏
 */
$newEye.on('click', function() {
    if ($newEye.hasClass('close')) {
        $newPasswordInput.attr('type', 'text');
        $newEye.removeClass('close');
    } else {
        $newPasswordInput.attr('type', 'password');
        $newEye.addClass('close');
    }
});

/**
 * 提交密码重置
 */
$sureResetBtn.on('click', function() {
    if (!$sureResetBtn.hasClass('active')) {
        return false;
    }

    // 简单的密码校验
    if (!$oldPasswordInput.val() || !$newPasswordInput.val()) {
        tip.show('请输入旧密码和新密码');
        return false;
    }

    if (!validatePWD($newPasswordInput.val())) {
        tip.show('密码应为6-20位字母、数字的组合');
        return false;
    }

    $.ajax({
        type: 'post',
        url: '/passport/password/reset',
        data: {
            oldPwd: $oldPasswordInput.val(),
            newPwd: $newPasswordInput.val()
        },
        dataType: 'json',
        success: function(result) {
            if (result.code === 200) {
                window.location.href = '/passport/password/resetsuccess';
                return false;
            }

            if (result.code === 402) {
                window.location.href = '/passport/login';
            }
        },
        error: function() {
            tip.show('系统异常!');
        }
    });
});