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 && (
)}
-

+