import React, { useState, useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router'; import { HomeIcon, UserIcon, NewspaperIcon, BellIcon } from '@heroicons/react/24/outline'; import { useAuth } from '../hooks.tsx'; import { formatRelativeTime } from '../utils.ts'; interface BannerItem { id: number; title: string; image: string; link: string; } interface NewsItem { id: number; title: string; summary: string; publish_date: string; cover?: string; category: string; } interface NoticeItem { id: number; title: string; content: string; created_at: string; is_read: boolean; } // 首页组件 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(() => { // 模拟API请求 setTimeout(() => { // 模拟轮播图数据 setBanners([ { id: 1, title: '欢迎使用移动端应用', image: 'https://images.unsplash.com/photo-1518655048521-f130df041f66?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cG9ydGZvbGlvJTIwYmFja2dyb3VuZHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80', link: '/welcome' }, { id: 2, title: '新功能上线了', image: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cG9ydGZvbGlvJTIwYmFja2dyb3VuZHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80', link: '/new-features' } ]); // 模拟新闻数据 setNews([ { id: 1, title: '用户体验升级,新版本发布', summary: '我们很高兴地宣布,新版本已经发布,带来了更好的用户体验和更多新功能。', publish_date: '2023-05-01T08:30:00', cover: 'https://images.unsplash.com/photo-1496171367470-9ed9a91ea931?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTB8fHRlY2h8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60', category: '产品更新' }, { id: 2, title: '新的数据分析功能上线', summary: '新的数据分析功能让您更深入地了解您的业务数据,提供更好的决策支持。', publish_date: '2023-04-25T14:15:00', cover: 'https://images.unsplash.com/photo-1551288049-bebda4e38f71?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTJ8fGNoYXJ0fGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60', category: '功能介绍' }, { id: 3, title: '如何提高工作效率的5个小技巧', summary: '这篇文章分享了5个可以立即实施的小技巧,帮助您提高日常工作效率。', publish_date: '2023-04-20T09:45:00', category: '使用技巧' } ]); // 模拟通知数据 setNotices([ { id: 1, title: '系统维护通知', content: '我们将于本周六凌晨2点至4点进行系统维护,期间系统可能会出现短暂不可用。', created_at: '2023-05-02T10:00:00', is_read: false }, { id: 2, title: '您的账户信息已更新', content: '您的账户信息已成功更新,如非本人操作,请及时联系客服。', created_at: '2023-05-01T16:30:00', is_read: true } ]); setLoading(false); }, 800); }, []); // 处理轮播图点击 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.is_read) && ( )}
{/* 轮播图 */} {!loading && banners.length > 0 && (
{banners.map((banner) => (
handleBannerClick(banner.link)} > {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 && ( {item.title} )}

{item.title}

{item.summary}

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

{!item.is_read && ( )} {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;