添加改密接口

This commit is contained in:
CN-JS-HuiBai
2026-04-05 01:10:22 +08:00
parent 316e0e1b7e
commit 2fc84f999c
3 changed files with 140 additions and 2 deletions

View File

@@ -70,9 +70,14 @@
detailMemTotal: document.getElementById('detailMemTotal'),
detailUptime: document.getElementById('detailUptime'),
detailContainer: document.getElementById('detailContainer'),
sourceFilter: document.getElementById('sourceFilter'),
pageSizeSelect: document.getElementById('pageSizeSelect'),
paginationControls: document.getElementById('paginationControls')
paginationControls: document.getElementById('paginationControls'),
// Auth security
oldPasswordInput: document.getElementById('oldPassword'),
newPasswordInput: document.getElementById('newPassword'),
confirmNewPasswordInput: document.getElementById('confirmNewPassword'),
btnChangePassword: document.getElementById('btnChangePassword'),
changePasswordMessage: document.getElementById('changePasswordMessage')
};
// ---- State ----
@@ -124,6 +129,11 @@
// Site settings
dom.btnSaveSiteSettings.addEventListener('click', saveSiteSettings);
// Auth password change
if (dom.btnChangePassword) {
dom.btnChangePassword.addEventListener('click', saveChangePassword);
}
// Keyboard shortcut
document.addEventListener('keydown', (e) => {
@@ -721,6 +731,12 @@
dom.settingsModal.classList.remove('active');
hideMessage();
hideSiteMessage();
hideChangePasswordMessage();
// Reset password fields
if (dom.oldPasswordInput) dom.oldPasswordInput.value = '';
if (dom.newPasswordInput) dom.newPasswordInput.value = '';
if (dom.confirmNewPasswordInput) dom.confirmNewPasswordInput.value = '';
}
// ---- Tab Switching ----
@@ -849,6 +865,76 @@
dom.siteSettingsMessage.className = 'form-message';
}
async function saveChangePassword() {
if (!user) {
showChangePasswordMessage('请先登录后操作', 'error');
openLoginModal();
return;
}
const oldPassword = dom.oldPasswordInput.value;
const newPassword = dom.newPasswordInput.value;
const confirmNewPassword = dom.confirmNewPasswordInput.value;
if (!oldPassword || !newPassword || !confirmNewPassword) {
showChangePasswordMessage('请填写所有密码字段', 'error');
return;
}
if (newPassword !== confirmNewPassword) {
showChangePasswordMessage('两次输入的新密码不一致', 'error');
return;
}
if (newPassword.length < 6) {
showChangePasswordMessage('新密码长度至少为 6 位', 'error');
return;
}
dom.btnChangePassword.disabled = true;
dom.btnChangePassword.textContent = '提交中...';
try {
const response = await fetch('/api/auth/change-password', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
oldPassword,
newPassword
})
});
const data = await response.json();
if (response.ok) {
showChangePasswordMessage('密码修改成功', 'success');
dom.oldPasswordInput.value = '';
dom.newPasswordInput.value = '';
dom.confirmNewPasswordInput.value = '';
} else {
showChangePasswordMessage(data.error || '修改失败', 'error');
if (response.status === 401 && data.error === 'Auth required') openLoginModal();
}
} catch (err) {
showChangePasswordMessage(`请求失败: ${err.message}`, 'error');
} finally {
dom.btnChangePassword.disabled = false;
dom.btnChangePassword.textContent = '提交修改';
}
}
function showChangePasswordMessage(text, type) {
dom.changePasswordMessage.textContent = text;
dom.changePasswordMessage.className = `form-message ${type}`;
setTimeout(hideChangePasswordMessage, 5000);
}
function hideChangePasswordMessage() {
dom.changePasswordMessage.className = 'form-message';
}
function updateSourceFilterOptions(sources) {
if (!dom.sourceFilter) return;
const current = dom.sourceFilter.value;