1. 登录/会话/改密/重激活等主链路不再查询 email,仅找回密码与绑定邮箱功能查询,避免数据库未升级时登录500 2. 自助绑定邮箱改为验证码流程:发送验证码(当前密码+60秒限流)→ 校验验证码(10分钟有效、最多5次尝试)→ 绑定;解绑需当前密码 3. 重置密码保持一次性链接(30分钟、用后即删、重置后强制重新登录) 4. 新增 EMAIL_CODE_EXPIRE_MINUTES 配置,文档同步
186 lines
6.8 KiB
PHP
186 lines
6.8 KiB
PHP
<?php
|
|
/**
|
|
* 班级操行分管理系统 - 管理端修改密码
|
|
*
|
|
* 开发者: Canglan
|
|
* 联系方式: admin@sea-studio.top
|
|
* 版权归属: Sea Network Technology Studio
|
|
* 许可证: MIT License
|
|
*
|
|
* 版权所有 © Sea Network Technology Studio
|
|
*/
|
|
|
|
require_once __DIR__ . '/../config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'admin') {
|
|
header('Location: /index.php');
|
|
exit();
|
|
}
|
|
|
|
$page_title = '修改密码';
|
|
$role = current_group_name();
|
|
$bound_email = $_SESSION['email'] ?? '';
|
|
include __DIR__ . '/../includes/header.php';
|
|
?>
|
|
|
|
<?php include __DIR__ . '/../includes/nav.php'; ?>
|
|
|
|
<div class="container">
|
|
<div class="card">
|
|
<div class="card-title">修改密码</div>
|
|
<form id="passwordForm">
|
|
<div class="form-group">
|
|
<label>原密码 <span style="color:red;">*</span></label>
|
|
<input type="password" id="oldPassword" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>新密码 <span style="color:red;">*</span></label>
|
|
<input type="password" id="newPassword" required>
|
|
<small>密码长度6-20位,需包含大写字母、小写字母、数字、特殊符号中的至少3种</small>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>确认新密码 <span style="color:red;">*</span></label>
|
|
<input type="password" id="confirmPassword" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">确认修改</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-title">绑定邮箱(找回密码)</div>
|
|
<p style="font-size:13px; color:var(--color-text-secondary); margin-bottom:12px;">
|
|
绑定邮箱需通过邮箱验证码验证;忘记密码时可在登录页通过该邮箱获取一次性重置链接。
|
|
</p>
|
|
<div class="form-group">
|
|
<label>当前绑定邮箱</label>
|
|
<div id="currentEmail"><?php echo $bound_email ? htmlspecialchars($bound_email) : '未绑定'; ?></div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>新邮箱</label>
|
|
<input type="email" id="bindEmail" maxlength="100" placeholder="请输入要绑定的邮箱">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>当前密码 <span style="color:red;">*</span></label>
|
|
<input type="password" id="emailPassword">
|
|
<small>安全验证:发送验证码需要输入当前登录密码</small>
|
|
</div>
|
|
<div style="display:flex; gap:8px; flex-wrap:wrap;">
|
|
<button type="button" class="btn btn-secondary" onclick="sendBindEmailCode()">发送验证码</button>
|
|
</div>
|
|
<div class="form-group" style="margin-top:16px;">
|
|
<label>邮箱验证码</label>
|
|
<input type="text" id="emailCode" maxlength="6" inputmode="numeric" placeholder="请输入6位验证码">
|
|
</div>
|
|
<div style="display:flex; gap:8px; flex-wrap:wrap;">
|
|
<button type="button" class="btn btn-primary" onclick="confirmBindEmail()">确认绑定</button>
|
|
<button type="button" class="btn btn-outline-danger" onclick="unbindEmail()">解除绑定</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById('passwordForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const oldPassword = document.getElementById('oldPassword').value;
|
|
const newPassword = document.getElementById('newPassword').value;
|
|
const confirmPassword = document.getElementById('confirmPassword').value;
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
showToast('两次输入的新密码不一致', 'error');
|
|
return;
|
|
}
|
|
|
|
if (newPassword.length < 6 || newPassword.length > 20) {
|
|
showToast('密码长度需为6-20位', 'error');
|
|
return;
|
|
}
|
|
|
|
const res = await apiPost('/api/auth/change-password', {
|
|
old_password: oldPassword,
|
|
new_password: newPassword
|
|
});
|
|
|
|
if (res && res.success) {
|
|
showToast('密码修改成功,请重新登录');
|
|
setTimeout(() => logout(), 1500);
|
|
} else {
|
|
showToast(res?.message || '密码修改失败', 'error');
|
|
}
|
|
});
|
|
|
|
async function sendBindEmailCode() {
|
|
const email = document.getElementById('bindEmail').value.trim();
|
|
const password = document.getElementById('emailPassword').value;
|
|
if (!email) {
|
|
showToast('请输入要绑定的邮箱', 'warning');
|
|
return;
|
|
}
|
|
if (!password) {
|
|
showToast('请输入当前密码', 'warning');
|
|
return;
|
|
}
|
|
const res = await apiPost('/api/auth/email/send-code', {
|
|
password: password,
|
|
email: email
|
|
});
|
|
if (res && res.success) {
|
|
showToast(res.message || '验证码已发送,请查收邮箱');
|
|
document.getElementById('emailCode').focus();
|
|
} else {
|
|
showToast(res?.message || '验证码发送失败', 'error');
|
|
}
|
|
}
|
|
|
|
async function confirmBindEmail() {
|
|
const email = document.getElementById('bindEmail').value.trim();
|
|
const code = document.getElementById('emailCode').value.trim();
|
|
if (!email) {
|
|
showToast('请输入要绑定的邮箱', 'warning');
|
|
return;
|
|
}
|
|
if (!code) {
|
|
showToast('请输入邮箱验证码', 'warning');
|
|
return;
|
|
}
|
|
const res = await apiPost('/api/auth/email/confirm', {
|
|
email: email,
|
|
code: code
|
|
});
|
|
if (res && res.success) {
|
|
const bound = res.data && res.data.email ? res.data.email : '';
|
|
document.getElementById('currentEmail').textContent = bound || '未绑定';
|
|
document.getElementById('bindEmail').value = '';
|
|
document.getElementById('emailPassword').value = '';
|
|
document.getElementById('emailCode').value = '';
|
|
showToast(res.message || '邮箱绑定成功');
|
|
} else {
|
|
showToast(res?.message || '邮箱绑定失败', 'error');
|
|
}
|
|
}
|
|
|
|
async function unbindEmail() {
|
|
const password = document.getElementById('emailPassword').value;
|
|
if (!password) {
|
|
showToast('请输入当前密码', 'warning');
|
|
return;
|
|
}
|
|
if (!confirm('确定解除绑定邮箱吗?解除后将无法通过邮箱找回密码。')) {
|
|
return;
|
|
}
|
|
const res = await apiPost('/api/auth/email/unbind', {
|
|
password: password
|
|
});
|
|
if (res && res.success) {
|
|
document.getElementById('currentEmail').textContent = '未绑定';
|
|
document.getElementById('bindEmail').value = '';
|
|
document.getElementById('emailPassword').value = '';
|
|
document.getElementById('emailCode').value = '';
|
|
showToast(res.message || '邮箱已解除绑定');
|
|
} else {
|
|
showToast(res?.message || '解除绑定失败', 'error');
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<?php include __DIR__ . '/../includes/footer.php'; ?>
|