From a5bc1e1ce379eea8632ff67825ba04d4dd90e4e5 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Thu, 18 Dec 2025 18:03:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=87=8F=E5=B0=91=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E5=99=AA=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - axiosConfig: 忽略 CanceledError 错误日志(组件卸载时的正常行为) - socketService: 首次连接失败使用 warn 级别,后续重试使用 debug 级别 - useEventData: 添加防御性检查,防止 pagination 为 undefined 时崩溃 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/services/socketService.js | 15 ++++++++++++--- src/utils/axiosConfig.js | 5 +++++ src/views/Community/hooks/useEventData.js | 17 ++++++++++------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/services/socketService.js b/src/services/socketService.js index 74e1bb28..42c879e0 100644 --- a/src/services/socketService.js +++ b/src/services/socketService.js @@ -92,9 +92,18 @@ class SocketService { // 监听连接错误 this.socket.on('connect_error', (error) => { this.reconnectAttempts++; - logger.error('socketService', 'connect_error', error, { - attempts: this.reconnectAttempts, - }); + + // 首次连接失败使用 warn 级别,后续使用 debug 级别减少日志噪音 + if (this.reconnectAttempts === 1) { + logger.warn('socketService', `Socket 连接失败,将在后台重试`, { + url: API_BASE_URL, + error: error.message, + }); + } else { + logger.debug('socketService', `Socket 重连尝试 #${this.reconnectAttempts}`, { + error: error.message, + }); + } // 使用指数退避策略安排下次重连 this.scheduleReconnection(); diff --git a/src/utils/axiosConfig.js b/src/utils/axiosConfig.js index 945fecdd..8da13245 100644 --- a/src/utils/axiosConfig.js +++ b/src/utils/axiosConfig.js @@ -52,6 +52,11 @@ axios.interceptors.response.use( return response; }, (error) => { + // 忽略取消请求的错误(组件卸载时正常行为) + if (error.name === 'CanceledError' || axios.isCancel(error)) { + return Promise.reject(error); + } + const method = error.config?.method?.toUpperCase() || 'UNKNOWN'; const url = error.config?.url || 'UNKNOWN'; const requestData = error.config?.data || error.config?.params || null; diff --git a/src/views/Community/hooks/useEventData.js b/src/views/Community/hooks/useEventData.js index 84da1c52..175d9cc2 100644 --- a/src/views/Community/hooks/useEventData.js +++ b/src/views/Community/hooks/useEventData.js @@ -52,18 +52,21 @@ export const useEventData = (filters, pageSize = 10) => { total: response.data?.pagination?.total }); - if (response.success) { - setEvents(response.data.events); + if (response.success && response.data) { + const events = response.data.events || []; + const paginationData = response.data.pagination || {}; + + setEvents(events); setPagination({ - current: response.data.pagination.page, - pageSize: response.data.pagination.per_page, - total: response.data.pagination.total + current: paginationData.page || page, + pageSize: paginationData.per_page || pagination.pageSize, + total: paginationData.total || 0 }); setLastUpdateTime(new Date()); logger.debug('useEventData', 'loadEvents 成功', { - count: response.data.events.length, - total: response.data.pagination.total + count: events.length, + total: paginationData.total || 0 }); } } catch (error) {