update /api/events/<int:event_id>/stocks resp format

This commit is contained in:
2025-10-23 10:09:24 +08:00
parent 9ad2dc7fab
commit 37eba48906
2 changed files with 37 additions and 14 deletions

View File

@@ -37,28 +37,46 @@ export const useEventNotifications = (options = {}) => {
const [error, setError] = useState(null); const [error, setError] = useState(null);
const unsubscribeRef = useRef(null); const unsubscribeRef = useRef(null);
// 使用 ref 存储 onNewEvent 回调,避免因回调函数引用改变导致重新连接
const onNewEventRef = useRef(onNewEvent);
// 每次 onNewEvent 改变时更新 ref
useEffect(() => { useEffect(() => {
onNewEventRef.current = onNewEvent;
}, [onNewEvent]);
useEffect(() => {
console.log('[useEventNotifications DEBUG] ========== useEffect 执行 ==========');
console.log('[useEventNotifications DEBUG] enabled:', enabled);
console.log('[useEventNotifications DEBUG] eventType:', eventType);
console.log('[useEventNotifications DEBUG] importance:', importance);
// 如果禁用,则不订阅 // 如果禁用,则不订阅
if (!enabled) { if (!enabled) {
console.log('[useEventNotifications DEBUG] ⚠️ 订阅已禁用,跳过');
return; return;
} }
// 连接状态监听 // 连接状态监听
const handleConnect = () => { const handleConnect = () => {
console.log('[useEventNotifications DEBUG] ✓ WebSocket 已连接');
setIsConnected(true); setIsConnected(true);
setError(null); setError(null);
}; };
const handleDisconnect = () => { const handleDisconnect = () => {
console.log('[useEventNotifications DEBUG] ⚠️ WebSocket 已断开');
setIsConnected(false); setIsConnected(false);
}; };
const handleConnectError = (err) => { const handleConnectError = (err) => {
console.error('[useEventNotifications ERROR] WebSocket 连接错误:', err);
setError(err); setError(err);
setIsConnected(false); setIsConnected(false);
}; };
// 连接 WebSocket // 连接 WebSocket
console.log('[useEventNotifications DEBUG] 准备连接 WebSocket...');
socketService.connect(); socketService.connect();
// 监听连接事件 // 监听连接事件
@@ -66,7 +84,7 @@ export const useEventNotifications = (options = {}) => {
socketService.on('disconnect', handleDisconnect); socketService.on('disconnect', handleDisconnect);
socketService.on('connect_error', handleConnectError); socketService.on('connect_error', handleConnectError);
// 新事件处理函数 // 新事件处理函数 - 使用 ref 中的回调
const handleNewEvent = (eventData) => { const handleNewEvent = (eventData) => {
console.log('\n[useEventNotifications DEBUG] ========== Hook 收到新事件 =========='); console.log('\n[useEventNotifications DEBUG] ========== Hook 收到新事件 ==========');
console.log('[useEventNotifications DEBUG] 事件数据:', eventData); console.log('[useEventNotifications DEBUG] 事件数据:', eventData);
@@ -77,10 +95,10 @@ export const useEventNotifications = (options = {}) => {
setNewEvent(eventData); setNewEvent(eventData);
console.log('[useEventNotifications DEBUG] ✓ newEvent 状态已更新'); console.log('[useEventNotifications DEBUG] ✓ newEvent 状态已更新');
// 调用外部回调 // 调用外部回调(从 ref 中获取最新的回调)
if (onNewEvent) { if (onNewEventRef.current) {
console.log('[useEventNotifications DEBUG] 准备调用外部 onNewEvent 回调'); console.log('[useEventNotifications DEBUG] 准备调用外部 onNewEvent 回调');
onNewEvent(eventData); onNewEventRef.current(eventData);
console.log('[useEventNotifications DEBUG] ✓ 外部 onNewEvent 回调已调用'); console.log('[useEventNotifications DEBUG] ✓ 外部 onNewEvent 回调已调用');
} else { } else {
console.log('[useEventNotifications DEBUG] ⚠️ 没有外部 onNewEvent 回调'); console.log('[useEventNotifications DEBUG] ⚠️ 没有外部 onNewEvent 回调');
@@ -114,22 +132,27 @@ export const useEventNotifications = (options = {}) => {
// 组件卸载时清理 // 组件卸载时清理
return () => { return () => {
console.log('清理 WebSocket 订阅'); console.log('\n[useEventNotifications DEBUG] ========== 清理 WebSocket 订阅 ==========');
// 取消订阅 // 取消订阅
if (unsubscribeRef.current) { if (unsubscribeRef.current) {
console.log('[useEventNotifications DEBUG] 取消订阅...');
unsubscribeRef.current(); unsubscribeRef.current();
} }
// 移除监听器 // 移除监听器
console.log('[useEventNotifications DEBUG] 移除事件监听器...');
socketService.off('connect', handleConnect); socketService.off('connect', handleConnect);
socketService.off('disconnect', handleDisconnect); socketService.off('disconnect', handleDisconnect);
socketService.off('connect_error', handleConnectError); socketService.off('connect_error', handleConnectError);
// 断开连接 // 断开连接
console.log('[useEventNotifications DEBUG] 断开 WebSocket 连接...');
socketService.disconnect(); socketService.disconnect();
console.log('[useEventNotifications DEBUG] ========== 清理完成 ==========\n');
}; };
}, [eventType, importance, enabled, onNewEvent]); }, [eventType, importance, enabled]); // 移除 onNewEvent 依赖
return { return {
newEvent, // 最新收到的事件 newEvent, // 最新收到的事件

View File

@@ -187,17 +187,17 @@ const EventList = ({ events, pagination, onPageChange, onEventClick, onViewDetai
logger.info('EventList', '收到新事件推送', event); logger.info('EventList', '收到新事件推送', event);
console.log('[EventList DEBUG] 准备显示 Toast 通知'); console.log('[EventList DEBUG] 准备显示 Toast 通知');
// 显示 Toast 通知 // 显示 Toast 通知 - 更明显的配置
toast({ const toastId = toast({
title: '新事件发布', title: '🔔 新事件发布',
description: event.title, description: event.title,
status: 'info', status: 'success', // 改为 success更醒目
duration: 5000, duration: 8000, // 延长显示时间到 8 秒
isClosable: true, isClosable: true,
position: 'top-right', position: 'top', // 改为顶部居中,更显眼
variant: 'left-accent', variant: 'solid', // 改为 solid背景更明显
}); });
console.log('[EventList DEBUG] ✓ Toast 通知已显示'); console.log('[EventList DEBUG] ✓ Toast 通知已调用ID:', toastId);
console.log('[EventList DEBUG] 准备更新事件列表'); console.log('[EventList DEBUG] 准备更新事件列表');
// 将新事件添加到列表顶部(防止重复) // 将新事件添加到列表顶部(防止重复)