事件中心不提示通知修复

This commit is contained in:
2025-11-10 13:46:34 +08:00
parent a00b8bb73d
commit 45ff13f4d0
2 changed files with 48 additions and 3 deletions

View File

@@ -666,6 +666,15 @@ export const NotificationProvider = ({ children }) => {
// 监听新事件推送(统一事件名) // 监听新事件推送(统一事件名)
socket.on('new_event', (data) => { socket.on('new_event', (data) => {
console.log('\n%c════════════════════════════════════════', 'color: #FF9800; font-weight: bold;');
console.log('%c[NotificationContext] 📨 收到 new_event 事件!', 'color: #FF9800; font-weight: bold;');
console.log('%c════════════════════════════════════════', 'color: #FF9800; font-weight: bold;');
console.log('[NotificationContext] 原始事件数据:', data);
console.log('[NotificationContext] 事件 ID:', data?.id);
console.log('[NotificationContext] 事件标题:', data?.title);
console.log('[NotificationContext] 事件类型:', data?.event_type || data?.type);
console.log('[NotificationContext] 事件重要性:', data?.importance);
logger.info('NotificationContext', 'Received new event', data); logger.info('NotificationContext', 'Received new event', data);
// ========== Socket层去重检查 ========== // ========== Socket层去重检查 ==========
@@ -673,11 +682,14 @@ export const NotificationProvider = ({ children }) => {
if (processedEventIds.current.has(eventId)) { if (processedEventIds.current.has(eventId)) {
logger.debug('NotificationContext', 'Duplicate event ignored at socket level', { eventId }); logger.debug('NotificationContext', 'Duplicate event ignored at socket level', { eventId });
console.warn('[NotificationContext] ⚠️ 重复事件,已忽略:', eventId);
console.log('%c════════════════════════════════════════\n', 'color: #FF9800; font-weight: bold;');
return; // 重复事件,直接忽略 return; // 重复事件,直接忽略
} }
// 记录已处理的事件ID // 记录已处理的事件ID
processedEventIds.current.add(eventId); processedEventIds.current.add(eventId);
console.log('[NotificationContext] ✓ 事件已记录,防止重复处理');
// 限制Set大小避免内存泄漏 // 限制Set大小避免内存泄漏
if (processedEventIds.current.size > MAX_PROCESSED_IDS) { if (processedEventIds.current.size > MAX_PROCESSED_IDS) {
@@ -690,8 +702,14 @@ export const NotificationProvider = ({ children }) => {
// ========== Socket层去重检查结束 ========== // ========== Socket层去重检查结束 ==========
// 使用适配器转换事件格式 // 使用适配器转换事件格式
console.log('[NotificationContext] 正在转换事件格式...');
const notification = adaptEventToNotification(data); const notification = adaptEventToNotification(data);
console.log('[NotificationContext] 转换后的通知对象:', notification);
console.log('[NotificationContext] 准备添加通知到队列...');
addNotification(notification); addNotification(notification);
console.log('[NotificationContext] ✅ 通知已添加到队列');
console.log('%c════════════════════════════════════════\n', 'color: #FF9800; font-weight: bold;');
}); });
// 保留系统通知监听(兼容性) // 保留系统通知监听(兼容性)

View File

@@ -64,6 +64,12 @@ class SocketService {
logger.info('socketService', 'Socket.IO connected successfully', { logger.info('socketService', 'Socket.IO connected successfully', {
socketId: this.socket.id, socketId: this.socket.id,
}); });
console.log(`%c[socketService] ✅ WebSocket 已连接`, 'color: #4CAF50; font-weight: bold;');
console.log('[socketService] Socket ID:', this.socket.id);
// 连接成功后自动订阅所有事件房间
this.subscribeToAllEvents();
}); });
// 监听断开连接 // 监听断开连接
@@ -142,11 +148,20 @@ class SocketService {
on(event, callback) { on(event, callback) {
if (!this.socket) { if (!this.socket) {
logger.warn('socketService', 'Cannot listen to event: socket not initialized', { event }); logger.warn('socketService', 'Cannot listen to event: socket not initialized', { event });
console.warn(`[socketService] ❌ 无法监听事件 ${event}: Socket 未初始化`);
return; return;
} }
this.socket.on(event, callback); // 包装回调函数,添加日志
const wrappedCallback = (...args) => {
console.log(`%c[socketService] 🔔 收到原始事件: ${event}`, 'color: #2196F3; font-weight: bold;');
console.log(`[socketService] 事件数据 (${event}):`, ...args);
callback(...args);
};
this.socket.on(event, wrappedCallback);
logger.info('socketService', `Event listener added: ${event}`); logger.info('socketService', `Event listener added: ${event}`);
console.log(`[socketService] ✓ 已注册事件监听器: ${event}`);
} }
/** /**
@@ -403,14 +418,26 @@ class SocketService {
/** /**
* 快捷方法:订阅所有类型的事件 * 快捷方法:订阅所有类型的事件
* @param {Function} onNewEvent - 收到新事件时的回调函数 * @param {Function} onNewEvent - 收到新事件时的回调函数(可选)
* @returns {Function} 取消订阅的函数 * @returns {Function} 取消订阅的函数
*/ */
subscribeToAllEvents(onNewEvent) { subscribeToAllEvents(onNewEvent) {
console.log('%c[socketService] 🔔 自动订阅所有事件...', 'color: #FF9800; font-weight: bold;');
// 如果没有提供回调,添加一个默认的日志回调
const defaultCallback = (event) => {
console.log('%c[socketService] 📨 收到新事件(默认回调)', 'color: #4CAF50; font-weight: bold;');
console.log('[socketService] 事件数据:', event);
};
this.subscribeToEvents({ this.subscribeToEvents({
eventType: 'all', eventType: 'all',
importance: 'all', importance: 'all',
onNewEvent, onNewEvent: onNewEvent || defaultCallback,
onSubscribed: (data) => {
console.log('%c[socketService] ✅ 订阅成功!', 'color: #4CAF50; font-weight: bold;');
console.log('[socketService] 订阅确认:', data);
},
}); });
// 返回取消订阅的清理函数 // 返回取消订阅的清理函数