diff --git a/miniprogram/src/pages/content-detail/index.jsx b/miniprogram/src/pages/content-detail/index.jsx index 24972e0..a811126 100644 --- a/miniprogram/src/pages/content-detail/index.jsx +++ b/miniprogram/src/pages/content-detail/index.jsx @@ -1,70 +1,123 @@ -import { View, Text, Image, Video, ScrollView, Button, RichText } from '@tarojs/components'; +import { View, Text, Image, Video, ScrollView, Input, Button, RichText } from '@tarojs/components'; import Taro from '@tarojs/taro'; import { useState, useEffect } from 'react'; -import { getContent } from '@/utils/content'; +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 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 ( - {/* 视频或封面 */} + {/* 封面/视频 */} {item.video ?