100 lines
No EOL
3.5 KiB
JavaScript
100 lines
No EOL
3.5 KiB
JavaScript
// js/settings.js - 设置页面的逻辑
|
|
|
|
import { theme, toast, activateNav } from './common.js';
|
|
import { initCaddyStatus } from './caddy.js'; // 导入 Caddy 状态模块
|
|
import { notification } from './notifications.js'; // 导入通知模块
|
|
|
|
const RESET_PWD_API_URL = '/v0/api/auth/resetpwd';
|
|
const LOGOUT_API_URL = '/v0/api/auth/logout';
|
|
|
|
const DOMElements = {
|
|
resetForm: document.getElementById('reset-password-form'),
|
|
themeToggleInput: document.getElementById('theme-toggle-input'),
|
|
logoutBtn: document.getElementById('logout-btn'),
|
|
toastContainer: document.getElementById('toast-container'),
|
|
dialogContainer: document.getElementById('dialog-container'),
|
|
};
|
|
const resetButton = DOMElements.resetForm.querySelector('button[type="submit"]');
|
|
|
|
async function handleResetPassword(e) {
|
|
e.preventDefault();
|
|
const newPassword = DOMElements.resetForm.new_password.value;
|
|
const confirmPassword = DOMElements.resetForm.confirm_new_password.value;
|
|
|
|
//保证字段均不为空, 用户名 密码 新密码
|
|
const currentPassword = DOMElements.resetForm.old_password.value;
|
|
const username = DOMElements.resetForm.username.value;
|
|
|
|
if (username === '') {
|
|
toast.show('用户名不能为空', 'error');
|
|
DOMElements.resetForm.username.focus();
|
|
return;
|
|
}
|
|
|
|
if (currentPassword === '') {
|
|
toast.show('当前密码不能为空', 'error');
|
|
DOMElements.resetForm.old_password.focus();
|
|
return;
|
|
}
|
|
if (newPassword === '') {
|
|
notification.toast('新密码不能为空', 'error');
|
|
DOMElements.resetForm.new_password.focus();
|
|
return;
|
|
}
|
|
if (confirmPassword === '') {
|
|
notification.toast('确认新密码不能为空', 'error');
|
|
DOMElements.resetForm.confirm_new_password.focus();
|
|
return;
|
|
}
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
notification.toast('新密码与确认密码不匹配', 'error');
|
|
return;
|
|
}
|
|
if (newPassword.length < 8) {
|
|
notification.toast('新密码长度至少为8位', 'error');
|
|
return;
|
|
}
|
|
|
|
resetButton.disabled = true;
|
|
resetButton.querySelector('span').textContent = '重置中...';
|
|
|
|
const formData = new FormData(DOMElements.resetForm);
|
|
|
|
try {
|
|
const response = await fetch(RESET_PWD_API_URL, {
|
|
method: 'POST',
|
|
body: new URLSearchParams(formData),
|
|
});
|
|
const result = await response.json();
|
|
if (response.ok) {
|
|
notification.toast('密码重置成功!请重新登录。', 'success');
|
|
setTimeout(() => { window.location.href = LOGOUT_API_URL; }, 1500);
|
|
} else {
|
|
throw new Error(result.error || '重置密码失败');
|
|
}
|
|
} catch (error) {
|
|
notification.toast(error.message, 'error');
|
|
resetButton.disabled = false;
|
|
resetButton.querySelector('span').textContent = '重置密码';
|
|
}
|
|
}
|
|
|
|
async function handleLogout() {
|
|
if (await notification.confirm('您确定要退出登录吗?')) {
|
|
notification.toast('正在退出...', 'info');
|
|
setTimeout(() => { window.location.href = LOGOUT_API_URL; }, 500);
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
theme.init(DOMElements.themeToggleInput);
|
|
notification.init(DOMElements.toastContainer, DOMElements.dialogContainer);
|
|
activateNav('settings');
|
|
initCaddyStatus(); // 初始化通用Caddy状态检查
|
|
|
|
DOMElements.resetForm.addEventListener('submit', handleResetPassword);
|
|
DOMElements.logoutBtn.addEventListener('click', handleLogout);
|
|
}
|
|
|
|
init(); |