add i18n step1

This commit is contained in:
wjqserver 2025-07-01 07:04:39 +08:00
parent 34d553a890
commit 79e3db6078
23 changed files with 2309 additions and 450 deletions

View file

@ -1,18 +1,14 @@
// 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';
import { initializePage } from './common.js';
import { api } from './api.js';
import { notification } from './notifications.js';
import { t, setLanguage, getCurrentLanguage } from './locale.js';
import { createCustomSelect } from './ui.js';
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"]');
@ -20,81 +16,62 @@ 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();
if (!username || !currentPassword || !newPassword || !confirmPassword) {
notification.toast(t('toasts.error_all_fields_required'), 'error');
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');
notification.toast(t('toasts.init_error_mismatch'), 'error');
return;
}
if (newPassword.length < 8) {
notification.toast('新密码长度至少为8位', 'error');
notification.toast(t('toasts.init_error_short'), 'error');
return;
}
resetButton.disabled = true;
resetButton.querySelector('span').textContent = '重置中...';
const formData = new FormData(DOMElements.resetForm);
resetButton.querySelector('span').textContent = t('pages.settings.resetting_password_btn');
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 || '重置密码失败');
}
const result = await api.post('/auth/resetpwd', new URLSearchParams(new FormData(DOMElements.resetForm)));
notification.toast(t('toasts.pwd_reset_success'), 'success');
setTimeout(() => { window.location.href = '/v0/api/auth/logout'; }, 1500);
} catch (error) {
notification.toast(error.message, 'error');
notification.toast(`${t('common.error_prefix')}: ${error.message}`, 'error');
resetButton.disabled = false;
resetButton.querySelector('span').textContent = '重置密码';
resetButton.querySelector('span').textContent = t('pages.settings.reset_password_btn');
}
}
async function handleLogout() {
if (await notification.confirm('您确定要退出登录吗?')) {
notification.toast('正在退出...', 'info');
setTimeout(() => { window.location.href = LOGOUT_API_URL; }, 500);
if (await notification.confirm(t('dialogs.logout_msg'))) {
notification.toast(t('toasts.logout_processing'), 'info');
setTimeout(() => { window.location.href = '/v0/api/auth/logout'; }, 500);
}
}
function init() {
theme.init(DOMElements.themeToggleInput);
notification.init(DOMElements.toastContainer, DOMElements.dialogContainer);
activateNav('settings');
initCaddyStatus(); // 初始化通用Caddy状态检查
// 页面特有的初始化逻辑
function pageInit() {
const langOptions = { 'en': 'English', 'zh-CN': '简体中文' };
const langSelectOptions = Object.keys(langOptions).map(key => ({ name: langOptions[key], value: key }));
createCustomSelect('select-language', langSelectOptions, (selectedValue) => {
setLanguage(selectedValue);
});
const langSelect = document.getElementById('select-language');
if (langSelect) {
const currentLangName = langOptions[getCurrentLanguage()];
const selectedDiv = langSelect.querySelector('.select-selected');
if (selectedDiv) selectedDiv.textContent = currentLangName;
}
DOMElements.resetForm.addEventListener('submit', handleResetPassword);
DOMElements.logoutBtn.addEventListener('click', handleLogout);
}
init();
// 使用通用初始化函数
initializePage({ pageId: 'settings', pageInit: pageInit });