From d0ffdae94e7f1947aff3bc1a05758a2d0bea584c Mon Sep 17 00:00:00 2001 From: admin <1297598740@qq.com> Date: Wed, 28 Jan 2026 16:05:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20`useNotifications`?= =?UTF-8?q?=20hook=20=E4=BB=A5=E5=A4=84=E7=90=86=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=EF=BC=8C=E5=B9=B6=E5=88=9B=E5=BB=BA=20`Home`?= =?UTF-8?q?=20=E9=A1=B5=E9=9D=A2=E5=B1=95=E7=A4=BA=E8=B4=A2=E5=8A=A1?= =?UTF-8?q?=E6=A6=82=E8=A7=88=E5=8F=8A=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useNotifications.tsx | 15 +++++++++++++++ src/pages/Home/Home.tsx | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/hooks/useNotifications.tsx b/src/hooks/useNotifications.tsx index f453a9a..44067e6 100644 --- a/src/hooks/useNotifications.tsx +++ b/src/hooks/useNotifications.tsx @@ -12,12 +12,23 @@ interface NotificationContextType { const NotificationContext = createContext(undefined); +// 检查用户是否已登录 +const isAuthenticated = (): boolean => { + const token = localStorage.getItem('token'); + return !!token; +}; + export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); const [loading, setLoading] = useState(false); const fetchNotifications = useCallback(async () => { + // 未登录时跳过请求,避免 401 错误 + if (!isAuthenticated()) { + return; + } + try { // Fetch unread count const countRes = await notificationService.getUnreadCount(); @@ -32,6 +43,10 @@ export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ setNotifications(listRes.data.notifications); } } catch (error) { + // 静默处理认证错误,避免控制台噪音 + if (error instanceof Error && error.message.includes('Authorization')) { + return; + } console.error('Failed to fetch notifications:', error); } }, []); diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 6979ec8..07697a6 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -324,9 +324,9 @@ function Home() {

{greeting},保持节奏

-

+

- {streakInfo?.message || insight} + {streakInfo?.message || insight}
{streakInfo && streakInfo.currentStreak > 0 && (
)}
-

+