22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 1 | import React, { useState, useEffect, useCallback } from 'react' |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 2 | import { useParams, useNavigate } from 'react-router-dom' |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 3 | import { ArrowLeft, ThumbsUp, MessageCircle, Share2, BookmarkPlus, Heart, Eye } from 'lucide-react' |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 4 | import { searchAPI } from '../api/search_jwlll' |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 5 | import { getUserInfo } from '../utils/auth' |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 6 | import FollowButton from './FollowButton' |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 7 | import postsAPI from '../api/posts_api' |
956303669 | 9de8c09 | 2025-06-21 16:41:18 +0800 | [diff] [blame] | 8 | import MediaPreview from './MediaPreview' |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 9 | import '../style/PostDetail.css' |
22301008 | 3c35e49 | 2025-06-20 22:24:22 +0800 | [diff] [blame] | 10 | import dayjs from 'dayjs' |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 11 | |
| 12 | export default function PostDetail() { |
| 13 | const { id } = useParams() |
| 14 | const navigate = useNavigate() |
| 15 | const [post, setPost] = useState(null) |
| 16 | const [loading, setLoading] = useState(true) |
| 17 | const [error, setError] = useState(null) |
| 18 | const [liked, setLiked] = useState(false) |
| 19 | const [bookmarked, setBookmarked] = useState(false) |
| 20 | const [likeCount, setLikeCount] = useState(0) |
| 21 | const [comments, setComments] = useState([]) |
| 22 | const [newComment, setNewComment] = useState('') |
| 23 | const [showComments, setShowComments] = useState(false) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 24 | const [isFollowing, setIsFollowing] = useState(false) |
| 25 | const [authorInfo, setAuthorInfo] = useState(null) |
wu | 32b0782 | 2025-06-24 23:10:02 +0800 | [diff] [blame] | 26 | const [previewImg, setPreviewImg] = useState(null) |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 27 | const [commentUserMap, setCommentUserMap] = useState({}) // user_id: username |
| 28 | const [commentUserAvatarMap, setCommentUserAvatarMap] = useState({}) // user_id: avatar // 获取当前用户ID |
| 29 | const getCurrentUserId = () => { |
| 30 | const userInfo = getUserInfo() |
| 31 | return userInfo?.id || '3' // 如果未登录或无用户信息,使用默认值3 |
| 32 | } |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 33 | const fetchPostDetail = useCallback(async () => { |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 34 | setLoading(true) |
| 35 | setError(null) |
| 36 | try { |
| 37 | const data = await searchAPI.getPostDetail(id) |
| 38 | setPost(data) |
| 39 | setLikeCount(data.heat || 0) |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 40 | |
| 41 | // 检查当前用户是否已点赞 |
| 42 | const currentUserId = getCurrentUserId() |
| 43 | try { |
| 44 | const hasLiked = await searchAPI.hasLiked(id, currentUserId) |
| 45 | setLiked(hasLiked) |
| 46 | } catch (error) { |
| 47 | console.error('检查点赞状态失败:', error) |
| 48 | setLiked(false) // 如果检查失败,默认为未点赞 |
| 49 | } |
| 50 | } catch (error) { |
| 51 | console.error('获取帖子详情失败:', error) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 52 | setError('帖子不存在或已被删除') |
| 53 | } finally { |
| 54 | setLoading(false) |
| 55 | } |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 56 | }, [id]) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 57 | |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 58 | const fetchComments = useCallback(async () => { |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 59 | try { |
| 60 | const data = await searchAPI.getComments(id) |
| 61 | setComments(data.comments || []) |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 62 | } catch (error) { |
| 63 | console.error('获取评论失败:', error) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 64 | } |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 65 | }, [id]) |
| 66 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 67 | // 检查当前用户是否已关注发帖人 |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 68 | useEffect(() => { |
| 69 | if (post && post.user_id) { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 70 | // 这里假设有API postsAPI.getUserFollowing |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 71 | const checkFollow = async () => { |
| 72 | try { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 73 | const userInfo = getUserInfo() |
| 74 | if (!userInfo?.id) return |
| 75 | const res = await postsAPI.getUserFollowing(userInfo.id) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 76 | if (Array.isArray(res)) { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 77 | setIsFollowing(res.some(u => u.id === post.user_id)) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 78 | } else if (Array.isArray(res.following)) { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 79 | setIsFollowing(res.following.some(u => u.id === post.user_id)) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 80 | } |
| 81 | } catch {} |
| 82 | } |
| 83 | checkFollow() |
| 84 | } |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 85 | }, [post]) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 86 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 87 | // 拉取发帖人信息 |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 88 | useEffect(() => { |
| 89 | if (post && post.user_id) { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 90 | postsAPI.getUser(post.user_id).then(res => setAuthorInfo(res || {})).catch(() => setAuthorInfo({})) |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 91 | } |
| 92 | }, [post]) |
| 93 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 94 | // 拉取所有评论用户昵称 |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 95 | const fetchCommentUserNames = async (userIds) => { |
| 96 | const map = {} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 97 | await Promise.all(userIds.map(async uid => { |
| 98 | try { |
| 99 | const user = await postsAPI.getUser(uid) |
| 100 | map[uid] = user.username || user.nickname || `用户${uid}` |
| 101 | } catch { |
| 102 | map[uid] = `用户${uid}` |
| 103 | } |
| 104 | })) |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 105 | setCommentUserMap(map) |
| 106 | } |
| 107 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 108 | // 拉取所有评论用户头像 |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 109 | const fetchCommentUserAvatars = async (userIds) => { |
| 110 | const map = {} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 111 | await Promise.all(userIds.map(async uid => { |
| 112 | try { |
| 113 | const user = await postsAPI.getUser(uid) |
| 114 | map[uid] = user.avatar && user.avatar.startsWith('http') ? user.avatar : (user.avatar ? `http://10.126.59.25:5715/static/${user.avatar}` : `https://i.pravatar.cc/40?img=${uid}`) |
| 115 | } catch { |
| 116 | map[uid] = `https://i.pravatar.cc/40?img=${uid}` |
| 117 | } |
| 118 | })) |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 119 | setCommentUserAvatarMap(map) |
| 120 | } |
| 121 | |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 122 | useEffect(() => { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 123 | fetchPostDetail() |
| 124 | fetchComments() |
| 125 | }, [fetchPostDetail, fetchComments]) |
| 126 | |
| 127 | // 评论区用户昵称和头像拉取 |
| 128 | useEffect(() => { |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 129 | if (comments.length > 0) { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 130 | const userIds = [...new Set(comments.map(c => c.user_id).filter(Boolean))] |
| 131 | fetchCommentUserNames(userIds) |
| 132 | fetchCommentUserAvatars(userIds) |
22301008 | 082d2ab | 2025-06-20 22:45:17 +0800 | [diff] [blame] | 133 | } |
| 134 | }, [comments]) |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 135 | const handleBack = () => { |
| 136 | navigate(-1) |
| 137 | } |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 138 | const handleLike = async () => { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 139 | const currentUserId = getCurrentUserId() |
| 140 | const originalLiked = liked |
| 141 | const originalLikeCount = likeCount |
| 142 | |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 143 | try { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 144 | // 先乐观更新UI,提供即时反馈 |
| 145 | const newLiked = !liked |
| 146 | setLiked(newLiked) |
| 147 | setLikeCount(prev => newLiked ? prev + 1 : prev - 1) |
| 148 | |
| 149 | // 调用后端API |
| 150 | if (newLiked) { |
| 151 | await searchAPI.likePost(id, currentUserId) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 152 | } else { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 153 | await searchAPI.unlikePost(id, currentUserId) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 154 | } |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 155 | |
| 156 | // API调用成功,状态已经更新,无需再次设置 |
| 157 | } catch (error) { |
| 158 | console.error('点赞操作失败:', error) |
| 159 | |
| 160 | // 检查是否是可以忽略的错误 |
| 161 | const errorMessage = error.message || error.toString() |
| 162 | const isIgnorableError = errorMessage.includes('already liked') || |
| 163 | errorMessage.includes('not liked yet') || |
| 164 | errorMessage.includes('already favorited') || |
| 165 | errorMessage.includes('not favorited yet') |
| 166 | |
| 167 | if (isIgnorableError) { |
| 168 | // 这些错误可以忽略,因为最终状态是正确的 |
| 169 | console.log('忽略重复操作错误:', errorMessage) |
| 170 | return |
| 171 | } |
| 172 | |
| 173 | // 发生真正的错误时回滚到原始状态 |
| 174 | setLiked(originalLiked) |
| 175 | setLikeCount(originalLikeCount) |
| 176 | alert('操作失败,请重试') |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | const handleBookmark = () => { |
| 181 | setBookmarked(!bookmarked) |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 182 | // 实际项目中这里应该调用后端API保存收藏状态 |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | const handleShare = () => { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 186 | // 分享功能 |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 187 | if (navigator.share) { |
| 188 | navigator.share({ |
| 189 | title: post?.title, |
| 190 | text: post?.content, |
| 191 | url: window.location.href, |
| 192 | }) |
| 193 | } else { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 194 | // 复制链接到剪贴板 |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 195 | navigator.clipboard.writeText(window.location.href) |
| 196 | alert('链接已复制到剪贴板') |
| 197 | } |
| 198 | } |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 199 | const handleAddComment = async (e) => { |
| 200 | e.preventDefault() |
| 201 | if (!newComment.trim()) return |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 202 | |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 203 | try { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 204 | const currentUserId = getCurrentUserId() |
22301008 | ba662fe | 2025-06-20 18:10:20 +0800 | [diff] [blame] | 205 | await searchAPI.addComment(id, currentUserId, newComment) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 206 | setNewComment('') |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 207 | fetchComments() // 刷新评论列表 |
| 208 | } catch (error) { |
| 209 | console.error('添加评论失败:', error) |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 210 | alert('评论失败,请重试') |
| 211 | } |
| 212 | } |
| 213 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 214 | // 关注后刷新关注状态 |
| 215 | const handleFollowChange = async (followed) => { |
| 216 | setIsFollowing(followed) |
| 217 | // 关注/取关后重新拉取一次关注状态,保证和数据库同步 |
| 218 | if (post && post.user_id) { |
| 219 | try { |
| 220 | const userInfo = getUserInfo() |
| 221 | if (!userInfo?.id) return |
| 222 | const res = await postsAPI.getUserFollowing(userInfo.id) |
| 223 | if (Array.isArray(res)) { |
| 224 | setIsFollowing(res.some(u => u.id === post.user_id)) |
| 225 | } else if (Array.isArray(res.following)) { |
| 226 | setIsFollowing(res.following.some(u => u.id === post.user_id)) |
| 227 | } |
| 228 | } catch {} |
| 229 | } |
| 230 | } |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 231 | |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 232 | if (loading) { |
| 233 | return ( |
| 234 | <div className="post-detail"> |
| 235 | <div className="loading-container"> |
| 236 | <div className="loading-spinner"></div> |
| 237 | <p>加载中...</p> |
| 238 | </div> |
| 239 | </div> |
| 240 | ) |
| 241 | } |
| 242 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 243 | // 优化错误和不存在的判断逻辑 |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 244 | if (error) { |
| 245 | return ( |
| 246 | <div className="post-detail"> |
| 247 | <div className="error-container"> |
| 248 | <h2>😔 出错了</h2> |
| 249 | <p>{error}</p> |
| 250 | <button onClick={handleBack} className="back-btn"> |
| 251 | <ArrowLeft size={20} /> |
| 252 | 返回 |
| 253 | </button> |
| 254 | </div> |
| 255 | </div> |
| 256 | ) |
| 257 | } |
| 258 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 259 | // 只有明确为 null 或 undefined 时才显示不存在 |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 260 | if (post === null || post === undefined) { |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 261 | return ( |
| 262 | <div className="post-detail"> |
| 263 | <div className="error-container"> |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 264 | <h2>😔 帖子不存在或已被删除</h2> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 265 | <p>该帖子可能已被删除或不存在</p> |
| 266 | <button onClick={handleBack} className="back-btn"> |
| 267 | <ArrowLeft size={20} /> |
| 268 | 返回 |
| 269 | </button> |
| 270 | </div> |
| 271 | </div> |
| 272 | ) |
| 273 | } |
| 274 | |
| 275 | return ( |
| 276 | <div className="post-detail"> |
| 277 | {/* 顶部导航栏 */} |
| 278 | <header className="post-header"> |
| 279 | <button onClick={handleBack} className="back-btn"> |
| 280 | <ArrowLeft size={20} /> |
| 281 | 返回 |
| 282 | </button> |
| 283 | <div className="header-actions"> |
| 284 | <button onClick={handleShare} className="action-btn"> |
| 285 | <Share2 size={20} /> |
| 286 | </button> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 287 | <button |
| 288 | onClick={handleBookmark} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 289 | className={`action-btn ${bookmarked ? 'active' : ''}`} |
| 290 | > |
| 291 | <BookmarkPlus size={20} /> |
| 292 | </button> |
| 293 | </div> |
| 294 | </header> |
| 295 | |
| 296 | {/* 主要内容区 */} |
| 297 | <main className="post-content"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 298 | {/* 帖子标题 */} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 299 | <h1 className="post-title">{post.title}</h1> |
| 300 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 301 | {/* 作者信息和元数据 */} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 302 | <div className="post-meta"> |
| 303 | <div className="author-info"> |
| 304 | <div className="avatar"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 305 | {authorInfo && authorInfo.avatar && authorInfo.avatar.startsWith('http') ? ( |
| 306 | <img className="avatar" src={authorInfo.avatar} alt={authorInfo.username || authorInfo.nickname || post.author || '用户'} /> |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 307 | ) : ( |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 308 | <img className="avatar" src={`https://i.pravatar.cc/40?img=${post.user_id}`} alt={authorInfo?.username || authorInfo?.nickname || post.author || '用户'} /> |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 309 | )} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 310 | </div> |
| 311 | <div className="author-details"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 312 | <span className="author-name">{authorInfo?.username || authorInfo?.nickname || post.author || '匿名用户'}</span> |
wu | 32b0782 | 2025-06-24 23:10:02 +0800 | [diff] [blame] | 313 | <span className="post-date"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 314 | {post.created_at ? dayjs(post.created_at).format('YYYY-MM-DD HH:mm:ss') : '未知时间'} |
wu | 32b0782 | 2025-06-24 23:10:02 +0800 | [diff] [blame] | 315 | </span> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 316 | {/* 关注按钮 */} |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 317 | {post.user_id && ( |
| 318 | <FollowButton |
| 319 | userId={post.user_id} |
| 320 | isFollowing={isFollowing} |
| 321 | onFollowChange={handleFollowChange} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 322 | style={{marginLeft: 12}} |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 323 | /> |
| 324 | )} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 325 | </div> |
| 326 | </div> |
| 327 | <div className="post-stats"> |
| 328 | <span className="stat-item"> |
| 329 | <Eye size={16} /> |
| 330 | {post.views || 0} |
| 331 | </span> |
| 332 | <span className="stat-item"> |
| 333 | <Heart size={16} /> |
| 334 | {likeCount} |
| 335 | </span> |
| 336 | </div> |
| 337 | </div> |
| 338 | |
| 339 | {/* 标签 */} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 340 | {post.tags && post.tags.length > 0 && ( |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 341 | <div className="post-tags"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 342 | {post.tags.map((tag, index) => ( |
| 343 | <span key={index} className="tag">{tag}</span> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 344 | ))} |
| 345 | </div> |
| 346 | )} |
| 347 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 348 | {/* 帖子媒体(支持多图/多视频) */} |
22301008 | c980507 | 2025-06-20 22:38:02 +0800 | [diff] [blame] | 349 | {Array.isArray(post.media_urls) && post.media_urls.length > 0 && ( |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 350 | <div className="post-media" style={{display:'flex',gap:8,marginBottom:16,flexWrap:'wrap'}}> |
22301008 | c980507 | 2025-06-20 22:38:02 +0800 | [diff] [blame] | 351 | {post.media_urls.map((url, idx) => ( |
956303669 | 9de8c09 | 2025-06-21 16:41:18 +0800 | [diff] [blame] | 352 | <MediaPreview |
| 353 | key={idx} |
| 354 | url={url} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 355 | alt={`媒体${idx+1}`} |
956303669 | 9de8c09 | 2025-06-21 16:41:18 +0800 | [diff] [blame] | 356 | onClick={(mediaUrl) => { |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 357 | // 对于图片,显示预览 |
| 358 | if (!mediaUrl.toLowerCase().includes('video') && !mediaUrl.includes('.mp4') && !mediaUrl.includes('.webm')) { |
956303669 | 9de8c09 | 2025-06-21 16:41:18 +0800 | [diff] [blame] | 359 | setPreviewImg(mediaUrl) |
| 360 | } |
| 361 | }} |
| 362 | style={{ cursor: 'pointer' }} |
| 363 | maxWidth={320} |
| 364 | maxHeight={320} |
| 365 | /> |
22301008 | c980507 | 2025-06-20 22:38:02 +0800 | [diff] [blame] | 366 | ))} |
| 367 | </div> |
| 368 | )} |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 369 | {/* 大图预览弹窗 */} |
22301008 | c980507 | 2025-06-20 22:38:02 +0800 | [diff] [blame] | 370 | {previewImg && ( |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 371 | <div className="img-preview-mask" style={{position:'fixed',zIndex:9999,top:0,left:0,right:0,bottom:0,background:'rgba(0,0,0,0.7)',display:'flex',alignItems:'center',justifyContent:'center'}} onClick={()=>setPreviewImg(null)}> |
| 372 | <img src={previewImg} alt="大图预览" style={{maxWidth:'90vw',maxHeight:'90vh',borderRadius:12,boxShadow:'0 4px 24px #0008'}} /> |
22301008 | c980507 | 2025-06-20 22:38:02 +0800 | [diff] [blame] | 373 | </div> |
| 374 | )} |
| 375 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 376 | {/* 帖子正文 */} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 377 | <div className="post-body"> |
| 378 | <p>{post.content}</p> |
| 379 | </div> |
| 380 | |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 381 | {/* 类别信息 */} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 382 | {(post.category || post.type) && ( |
| 383 | <div className="post-category"> |
| 384 | {post.category && ( |
| 385 | <> |
| 386 | <span className="category-label">分类:</span> |
| 387 | <span className="category-name">{post.category}</span> |
| 388 | </> |
| 389 | )} |
| 390 | {post.type && ( |
| 391 | <> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 392 | <span className="category-label" style={{marginLeft: '1em'}}>类型:</span> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 393 | <span className="category-name">{post.type}</span> |
| 394 | </> |
| 395 | )} |
| 396 | </div> |
| 397 | )} |
| 398 | |
| 399 | {/* 评论区 */} |
| 400 | <div className="comments-section"> |
| 401 | <div className="comments-header"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 402 | <button |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 403 | onClick={() => setShowComments(!showComments)} |
| 404 | className="comments-toggle" |
| 405 | > |
| 406 | <MessageCircle size={20} /> |
| 407 | 评论 ({comments.length}) |
| 408 | </button> |
| 409 | </div> |
| 410 | |
| 411 | {showComments && ( |
| 412 | <div className="comments-content"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 413 | {/* 添加评论 */} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 414 | <form onSubmit={handleAddComment} className="comment-form"> |
| 415 | <textarea |
| 416 | value={newComment} |
| 417 | onChange={(e) => setNewComment(e.target.value)} |
| 418 | placeholder="写下你的评论..." |
| 419 | className="comment-input" |
| 420 | rows={3} |
| 421 | /> |
| 422 | <button type="submit" className="comment-submit"> |
| 423 | 发布评论 |
| 424 | </button> |
| 425 | </form> |
| 426 | |
| 427 | {/* 评论列表 */} |
| 428 | <div className="comments-list"> |
| 429 | {comments.length === 0 ? ( |
| 430 | <p className="no-comments">暂无评论</p> |
| 431 | ) : ( |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 432 | comments.map((comment, index) => ( |
| 433 | <div key={index} className="comment-item"> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 434 | <div className="comment-author"> |
| 435 | <div className="comment-avatar"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 436 | <img className="avatar" src={commentUserAvatarMap[comment.user_id] || `https://i.pravatar.cc/40?img=${comment.user_id}`} alt={commentUserMap[comment.user_id] || comment.user_name || '用户'} /> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 437 | </div> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 438 | <span className="comment-name">{commentUserMap[comment.user_id] || comment.user_name || '匿名用户'}</span> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 439 | <span className="comment-time"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 440 | {comment.create_time ? new Date(comment.create_time).toLocaleString('zh-CN') : ''} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 441 | </span> |
| 442 | </div> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 443 | <div className="comment-content"> |
| 444 | {comment.content} |
| 445 | </div> |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 446 | </div> |
| 447 | )) |
| 448 | )} |
| 449 | </div> |
| 450 | </div> |
| 451 | )} |
| 452 | </div> |
| 453 | </main> |
| 454 | |
| 455 | {/* 底部操作栏 */} |
| 456 | <footer className="post-footer"> |
| 457 | <div className="action-bar"> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 458 | <button |
| 459 | onClick={handleLike} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 460 | className={`action-button ${liked ? 'liked' : ''}`} |
| 461 | > |
| 462 | <ThumbsUp size={20} /> |
| 463 | <span>{likeCount}</span> |
| 464 | </button> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 465 | |
| 466 | <button |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 467 | onClick={() => setShowComments(!showComments)} |
| 468 | className="action-button" |
| 469 | > |
| 470 | <MessageCircle size={20} /> |
| 471 | <span>评论</span> |
| 472 | </button> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 473 | |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 474 | <button onClick={handleShare} className="action-button"> |
| 475 | <Share2 size={20} /> |
| 476 | <span>分享</span> |
| 477 | </button> |
22301008 | e4acb92 | 2025-06-24 23:50:35 +0800 | [diff] [blame^] | 478 | |
| 479 | <button |
| 480 | onClick={handleBookmark} |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 481 | className={`action-button ${bookmarked ? 'bookmarked' : ''}`} |
| 482 | > |
| 483 | <BookmarkPlus size={20} /> |
| 484 | <span>收藏</span> |
| 485 | </button> |
| 486 | </div> |
| 487 | </footer> |
| 488 | </div> |
| 489 | ) |
| 490 | } |