This commit is contained in:
Baobhan Sith
2025-04-19 14:05:26 +08:00
parent ea9ce19b60
commit 6f403834ac
9 changed files with 471 additions and 233 deletions
@@ -18,24 +18,28 @@ export const getSettings = async (): Promise<AppearanceSettings> => {
export const updateSettings = async (settingsDto: UpdateAppearanceDto): Promise<boolean> => {
// 验证 activeTerminalThemeId (如果提供了)
if (settingsDto.activeTerminalThemeId !== undefined && settingsDto.activeTerminalThemeId !== null) {
const themeIdNum = parseInt(settingsDto.activeTerminalThemeId, 10);
if (isNaN(themeIdNum)) {
throw new Error(`无效的终端主题 ID 格式: ${settingsDto.activeTerminalThemeId}`);
const themeIdNum = settingsDto.activeTerminalThemeId; // ID is now number | null
// 验证 ID 是否为有效的数字
if (typeof themeIdNum !== 'number') {
console.error(`[AppearanceService] 收到的 activeTerminalThemeId 不是有效的数字: ${themeIdNum}`);
throw new Error(`无效的终端主题 ID 类型,应为数字。`);
}
try {
// 直接使用数字 ID 调用 findThemeById 进行验证
const themeExists = await terminalThemeRepository.findThemeById(themeIdNum);
if (!themeExists) {
throw new Error(`指定的终端主题 ID 不存在: ${settingsDto.activeTerminalThemeId}`);
console.warn(`[AppearanceService] 尝试更新为不存在的终端主题数字 ID: ${themeIdNum}`);
throw new Error(`指定的终端主题 ID 不存在: ${themeIdNum}`);
}
} catch (e: any) { // Catch potential errors from findThemeById as well
console.error(`验证终端主题 ID (${settingsDto.activeTerminalThemeId}) 时出错:`, e.message);
// Rethrow a more specific error or the original one
throw new Error(`验证终端主题 ID 时出错: ${e.message || settingsDto.activeTerminalThemeId}`);
console.log(`[AppearanceService] 终端主题数字 ID ${themeIdNum} 验证通过。`);
} catch (e: any) {
console.error(`[AppearanceService] 验证终端主题数字 ID (${themeIdNum}) 时出错:`, e.message);
throw new Error(`验证终端主题 ID 时出错: ${e.message || themeIdNum}`);
}
} else if (settingsDto.hasOwnProperty('activeTerminalThemeId')) {
// Handle explicit setting to null/undefined (meaning reset to default/no theme)
// The repository update logic handles null/undefined correctly, so no specific validation needed here.
// We just need to ensure the key exists in the DTO if it's meant to be cleared.
} else if (settingsDto.hasOwnProperty('activeTerminalThemeId') && settingsDto.activeTerminalThemeId === null) {
// 处理显式设置为 null (表示重置为默认/无用户主题)
console.log(`[AppearanceService] 接收到将 activeTerminalThemeId 设置为 null 的请求。`);
// 仓库层会处理 null
}
// 验证 terminalFontSize (如果提供了)