This commit is contained in:
Baobhan Sith
2025-04-26 20:33:59 +08:00
parent 78a6207771
commit 9f4b248dff
2 changed files with 16 additions and 21 deletions
@@ -268,27 +268,22 @@ export class NotificationService {
async sendNotification(event: NotificationEvent, details?: Record<string, any> | string): Promise<void> { async sendNotification(event: NotificationEvent, details?: Record<string, any> | string): Promise<void> {
console.log(`[通知] 事件触发: ${event}`, details || ''); console.log(`[通知] 事件触发: ${event}`, details || '');
let userLang = defaultLng; // Start with default let userLang = defaultLng;
try {
// --- 修改开始 --- const langSetting = await settingsService.getSetting('language');
// 如果是设置更新事件,并且更新了语言,直接从 details 中获取新语言值 // --- 添加调试日志 ---
if (event === 'SETTINGS_UPDATED' && typeof details === 'object' && details?.updatedKeys?.includes('language') && typeof details?.language === 'string' && supportedLngs.includes(details.language)) { console.log(`[通知调试] 刚从数据库获取的 langSetting: ${langSetting}`);
userLang = details.language; // --- 结束调试日志 ---
console.log(`[通知] 事件 ${event} 检测到语言更新,直接使用新值 '${userLang}' (来自事件详情)`); if (langSetting && supportedLngs.includes(langSetting)) {
} else { userLang = langSetting;
// 对于其他事件或未更新语言的设置更新,才从数据库查询
try {
const langSetting = await settingsService.getSetting('language');
if (langSetting && supportedLngs.includes(langSetting)) {
userLang = langSetting;
}
} catch (error) {
console.error(`[通知] 获取事件 ${event} 的语言设置时出错:`, error);
} }
console.log(`[通知] 事件 ${event} 使用语言 '${userLang}' (来自数据库或默认)`); } catch (error) {
console.error(`[通知] 获取事件 ${event} 的语言设置时出错:`, error);
} }
// --- 修改结束 --- // --- 添加调试日志 ---
console.log(`[通知调试] 最终决定使用的 userLang: ${userLang}`);
// --- 结束调试日志 ---
console.log(`[通知] 事件 ${event} 使用语言 '${userLang}'`);
const payload: NotificationPayload = { const payload: NotificationPayload = {
event, event,
@@ -62,8 +62,8 @@ export const settingsController = {
} else { } else {
auditLogService.logAction('SETTINGS_UPDATED', { updatedKeys }); auditLogService.logAction('SETTINGS_UPDATED', { updatedKeys });
notificationService.sendNotification('SETTINGS_UPDATED', { updatedKeys }); // 添加通知调用 notificationService.sendNotification('SETTINGS_UPDATED', { updatedKeys }); // 添加通知调用
} }
} }
res.status(200).json({ message: '设置已成功更新' }); res.status(200).json({ message: '设置已成功更新' });
} catch (error: any) { } catch (error: any) {
console.error('更新设置时出错:', error); console.error('更新设置时出错:', error);