新增首次登录初始化密码接口 init-passwd(无需原密码)

1. 后端新增 POST /api/auth/init-passwd:仅 need_change_password=1 时可用,校验密码策略后直接设置新密码并清除旧 Token
2. 管理端所有页面增加首次登录设置密码弹窗(无需原密码),成功后强制重新登录
3. 学生端首次登录改密改用 init-passwd;管理员重置密码后也要求下次登录重新设置
4. 日常修改密码仍走 change-password(需原密码)
This commit is contained in:
canglan committed 2026-11-02 12:37:40 +08:00
1 parent 2d3513b246
commit db278188c6
7 files changed
+114 -6

No files matched your search

+57
View File
@@ -107,4 +107,61 @@ $page_title = $page_title ?? '首页';
], JSON_UNESCAPED_UNICODE);
?>;
</script>
<?php endif; ?>
<?php if ($user_type === 'admin'): ?>
<!-- 首次登录/重置后:设置新密码(无需原密码) -->
<div id="initPasswordModal" class="modal" style="display:none;">
<div class="modal-content" style="max-width:420px;">
<div class="modal-header">
<h3>首次登录:请设置新密码</h3>
</div>
<div class="form-group">
<label>新密码</label>
<input type="password" id="initNewPassword" autocomplete="new-password">
<small>密码要求:<span class="pwd-policy"></span></small>
</div>
<div class="form-group">
<label>确认新密码</label>
<input type="password" id="initConfirmPassword" autocomplete="new-password">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="initPasswordBtn" onclick="submitInitPassword()">确认修改</button>
</div>
</div>
</div>
<script>
(function() {
var user = (typeof getUserInfo === 'function') ? getUserInfo() : null;
if (!user || !user.need_change_password) return;
document.getElementById('initPasswordModal').style.display = 'flex';
window.submitInitPassword = async function() {
var p1 = document.getElementById('initNewPassword').value;
var p2 = document.getElementById('initConfirmPassword').value;
if (!p1) { showToast('请输入新密码', 'warning'); return; }
if (p1 !== p2) { showToast('两次输入的密码不一致', 'warning'); return; }
var err = (typeof validatePasswordByPolicy === 'function') ? validatePasswordByPolicy(p1) : '';
if (err) { showToast(err, 'warning'); return; }
var btn = document.getElementById('initPasswordBtn');
btn.disabled = true;
btn.textContent = '提交中...';
try {
var res = await apiPost('/api/auth/init-passwd', { new_password: p1 });
if (res && res.success) {
showToast(res.message || '密码设置成功,请重新登录');
setTimeout(function() { logout(); }, 1200);
} else {
showToast((res && (res.message || res.detail)) || '设置失败', 'error');
btn.disabled = false;
btn.textContent = '确认修改';
}
} catch (e) {
showToast('设置失败:' + (e && e.message ? e.message : '未知错误'), 'error');
btn.disabled = false;
btn.textContent = '确认修改';
}
};
})();
</script>
<?php endif; ?>
+2 -4
View File
@@ -485,10 +485,8 @@ include __DIR__ . '/../includes/header.php';
return;
}
const res = await apiPost('/api/auth/change-password', {
old_password: '',
new_password: newPassword,
force: true
const res = await apiPost('/api/auth/init-passwd', {
new_password: newPassword
});
if (res && res.success) {