import React, { useState, useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router'; import { HomeAPI } from './api/index.ts'; import { MessageAPI } from './api/index.ts'; import { HomeIcon, UserIcon, NewspaperIcon, BellIcon } from '@heroicons/react/24/outline'; import { useAuth } from './hooks.tsx'; import { formatRelativeTime } from './utils.ts'; import { KnowInfo, UserMessage, MessageType, MessageStatus } from '../share/types.ts'; // 首页组件 const HomePage: React.FC = () => { const { user } = useAuth(); const navigate = useNavigate(); const location = useLocation(); const [loading, setLoading] = useState(true); const [banners, setBanners] = useState([]); const [news, setNews] = useState([]); const [notices, setNotices] = useState([]); const [activeTab, setActiveTab] = useState('news'); // 模拟加载数据 useEffect(() => { const fetchData = async () => { try { // 获取数据 const [bannersRes, newsRes, messagesRes] = await Promise.all([ HomeAPI.getBanners(), HomeAPI.getNews(), MessageAPI.getMessages({ type: MessageType.ANNOUNCE }) ]); setBanners(bannersRes.data.map((item: KnowInfo) => ({ id: item.id, title: item.title, cover_url: item.cover_url, content: item.content, category: 'banner', created_at: new Date().toISOString(), updated_at: new Date().toISOString(), sort_order: item.sort_order || 0 } as KnowInfo))); setNews(newsRes.data); setNotices(messagesRes.data); setLoading(false); } catch (error) { console.error('获取首页数据失败:', error); setLoading(false); } }; fetchData(); }, []); // 处理轮播图点击 const handleBannerClick = (link: string) => { navigate(link); }; // 处理新闻点击 const handleNewsClick = (id: number) => { navigate(`/news/${id}`); }; // 处理通知点击 const handleNoticeClick = (id: number) => { navigate(`/notices/${id}`); }; return (
{/* 顶部用户信息 */}
{user?.avatar ? ( {user?.nickname ) : ( )}

{user ? `您好,${user.nickname || user.username}` : '您好,游客'}

{user ? '欢迎回来' : '请登录体验更多功能'}

{notices.some(notice => notice.user_status === MessageStatus.UNREAD) && ( )}
{/* 轮播图 */} {!loading && banners.length > 0 && (
{banners.map((banner) => (
handleBannerClick(banner.content || '')} > {banner.title}

{banner.title}

))}
{/* 指示器 */}
{banners.map((_, index) => ( ))}
)} {/* 快捷入口 */}
{[ { icon: , name: '首页', path: '/' }, { icon: , name: '资讯', path: '/news' }, { icon: , name: '通知', path: '/notices' }, { icon: , name: '我的', path: '/profile' } ].map((item, index) => (
navigate(item.path)} >
{item.icon}
{item.name}
))}
{/* 内容标签页 */}
{activeTab === 'news' ? ( loading ? (
) : (
{news.map((item) => (
handleNewsClick(item.id)} > {item.cover_url && ( {item.title} )}

{item.title}

{item.content?.substring(0, 100)}...

{item.category} {formatRelativeTime(item.created_at)}
))}
) ) : ( loading ? (
) : (
{notices.map((item) => (
handleNoticeClick(item.id)} >

{item.user_status === MessageStatus.UNREAD && ( )} {item.title}

{formatRelativeTime(item.created_at)}

{item.content}

))}
) )}
{/* 底部导航 */}
{[ { icon: , name: '首页', path: '/' }, { icon: , name: '资讯', path: '/news' }, { icon: , name: '通知', path: '/notices' }, { icon: , name: '我的', path: '/profile' } ].map((item, index) => (
navigate(item.path)} >
{item.icon}
{item.name}
))}
); }; export default HomePage;