import { useState, useCallback } from 'react'; import { View, Text } from '@tarojs/components'; import Taro, { useRouter, useShareAppMessage } from '@tarojs/taro'; import { usePageData } from '@/hooks/usePageData'; import { getArticleDetail, getPublicArticleDetail, Article } from '../../../services/article'; import { trackEvent } from '@/services/analytics'; import { useElderClass } from '../../../hooks/useElderClass'; import { useAuthStore } from '../../../stores/auth'; import PageShell from '@/components/ui/PageShell'; import RichArticle from '@/components/RichArticle'; import './index.scss'; export default function ArticleDetail() { const modeClass = useElderClass(); const router = useRouter(); const id = router.params.id || ''; const [article, setArticle] = useState
(null); const [loading, setLoading] = useState(true); useShareAppMessage(() => { trackEvent('article_share', { article_id: id }); return { title: article?.title || '健康资讯', path: `/pages/article/detail/index?id=${id}`, }; }); const fetchArticle = useCallback(async () => { if (!id) return; setLoading(true); try { const user = useAuthStore.getState().user; const fetcher = user ? getArticleDetail(id) : getPublicArticleDetail(id); const data = await fetcher; setArticle(data); } catch (err) { console.warn('[article] 加载文章详情失败:', err); Taro.showToast({ title: '加载失败', icon: 'none' }); } finally { setLoading(false); } }, [id]); usePageData(fetchArticle, { throttleMs: 60000 }); if (loading) { return ( 加载中... ); } if (!article) { return ( 文章不存在 ); } const handleCollect = () => { Taro.showToast({ title: '已收藏', icon: 'success' }); }; const handleShare = () => { Taro.showShareMenu({ withShareTicket: true }); }; return ( {article.title} {article.author && ( {article.author} )} {article.published_at && ( 📅 {article.published_at.slice(0, 10)} )} 收藏 分享 ); }