import { View, Text, Image, Video, ScrollView, Input, Button, RichText } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useState, useEffect } from 'react';
import { getContent, getComments, addComment } from '@/utils/content';
import { isAuthed } from '@/utils/auth';
import './index.scss';
const rw = (s) => (s || '').replace('T', ' ').replace('Z', '').slice(0, 16);
/** 资讯详情(微信文章式):标题/作者/阅读 + 正文 + 动作条 + 留言。 */
export default function ContentDetail() {
const [item, setItem] = useState(null);
const [comments, setComments] = useState([]);
const [err, setErr] = useState('');
const [loaded, setLoaded] = useState(false);
const [like, setLike] = useState(false);
const [fav, setFav] = useState(false);
const [draft, setDraft] = useState('');
const [sending, setSending] = useState(false);
useEffect(() => {
const inst = Taro.getCurrentInstance();
const id = (inst && inst.router && inst.router.params && inst.router.params.id) || '';
if (!id) { setErr('无效内容'); setLoaded(true); return; }
getContent(id).then(setItem).catch(() => setErr('加载失败')).finally(() => setLoaded(true));
getComments(id).then(setComments).catch(() => {});
}, []);
if (!loaded) return 加载中…;
if (err || !item) return {err || '内容不存在'};
const id = item.id;
const img = item.cover || (item.video ? item.video_cover : '');
const isSmall = item.card_mode === 'small'; // 小图模式:详情页不显示封面
const images = Array.isArray(item.images) ? item.images : [];
const author = item.publisher_name || '云超服';
const views = item.read_count || 0;
const copyLink = () => Taro.setClipboardData({ data: `https://opc.pinesound.cn/#/content-detail?id=${id}` }).then(() => Taro.showToast({ title: '链接已复制', icon: 'none' }));
const scrollToComment = () => { Taro.pageScrollTo({ selector: '.cd-comments', duration: 300 }); };
const send = async () => {
if (!isAuthed()) { Taro.showToast({ title: '请先登录', icon: 'none' }); return; }
if (!draft.trim()) return;
setSending(true);
try {
const c = await addComment(id, draft.trim());
if (c) { setComments((m) => [c, ...m]); setDraft(''); Taro.showToast({ title: '已留言', icon: 'success' }); }
} catch (e) { Taro.showToast({ title: (e && e.message) || '留言失败', icon: 'none' }); }
finally { setSending(false); }
};
return (
{/* 封面/视频(小图模式不显示封面) */}
{!isSmall && (item.video
?
: img
?
: null)}
{/* 标题 + 元信息 */}
{item.title}
{item.publisher_avatar ? : }
{author}
{rw(item.created_at)}
·
阅读 {views}
{/* 正文 */}
{item.summary ? {item.summary} : null}
{item.body
?
: 暂无正文}
{images.length > 0 && (
{images.map((u, i) => )}
)}
{item.link ? (
) : null}
{/* 作者条 + 动作 */}
{item.publisher_avatar ? : }
{author}
setLike((v) => !v)}>{like ? 1 : 0}
转发
setFav((v) => !v)}>
留言
{/* 阅读 + 留言 */}
阅读 {views}
留言
setDraft(e.detail.value)} />
{comments.length === 0
? 还没有留言,来写首评
: comments.map((c) => (
{c.avatar ? : }
{c.nickname || c.username || '用户'}{rw(c.created_at)}
{c.content}
))}
);
}