Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 1 | // FriendMoments.js |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 2 | import React, { useContext, useState, useEffect } from 'react'; |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 3 | import axios from 'axios'; |
| 4 | import './FriendMoments.css'; |
| 5 | import Header from '../../components/Header'; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 6 | import { Edit, GoodTwo, Comment } from '@icon-park/react'; |
| 7 | import { UserContext } from '../../context/UserContext'; // 引入用户上下文 |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 8 | |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 9 | // 修改后的封面图 URL 拼接函数 |
| 10 | const formatImageUrl = (url) => { |
| 11 | if (!url) return ''; |
| 12 | const filename = url.split('/').pop(); // 提取文件名部分 |
22301009 | 4158f3a | 2025-06-06 19:59:10 +0800 | [diff] [blame] | 13 | return `http://localhost:5011/uploads/dynamic/${filename}`; |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 14 | }; |
| 15 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 16 | const FriendMoments = () => { |
| 17 | const [feeds, setFeeds] = useState([]); |
| 18 | const [filteredFeeds, setFilteredFeeds] = useState([]); |
| 19 | const [query, setQuery] = useState(''); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 20 | const [loading, setLoading] = useState(true); |
| 21 | const [error, setError] = useState(null); |
| 22 | const [commentBoxVisibleId, setCommentBoxVisibleId] = useState(null); // 当前显示评论框的动态ID |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 23 | const [replyToCommentId, setReplyToCommentId] = useState(null); // 当前回复的评论ID |
| 24 | const [replyToUsername, setReplyToUsername] = useState(''); // 当前回复的用户名 |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 25 | const [commentInput, setCommentInput] = useState(''); // 当前输入的评论内容 |
| 26 | |
| 27 | // 从上下文中获取用户信息 |
| 28 | const { user } = useContext(UserContext); |
| 29 | const userId = user?.userId || null; // 从用户上下文中获取userId |
| 30 | const username = user?.username || '未知用户'; // 获取用户名 |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 31 | |
| 32 | // Modal state & form fields |
| 33 | const [showModal, setShowModal] = useState(false); |
| 34 | const [title, setTitle] = useState(''); |
| 35 | const [content, setContent] = useState(''); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 36 | const [selectedImages, setSelectedImages] = useState([]); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 37 | const [previewUrls, setPreviewUrls] = useState([]); |
| 38 | |
| 39 | // 检查用户是否已登录 |
| 40 | const isLoggedIn = !!userId; |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 41 | |
| 42 | // 拉取好友动态列表 |
| 43 | const fetchFeeds = async () => { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 44 | if (!isLoggedIn) { |
| 45 | setLoading(false); |
| 46 | setError('请先登录'); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | setLoading(true); |
| 51 | setError(null); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 52 | try { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 53 | // 注意这里修改了API路径,使用getAllDynamics接口 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 54 | const res = await axios.get(`/echo/dynamic/${userId}/getAllDynamics`); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 55 | |
| 56 | // 检查API返回的数据结构 |
| 57 | console.log('API响应数据:', res.data); |
| 58 | |
| 59 | // 从响应中提取dynamic数组 |
| 60 | const dynamicList = res.data.dynamic || []; |
| 61 | |
| 62 | // 将API返回的数据结构转换为前端期望的格式 |
| 63 | const formattedFeeds = dynamicList.map(item => ({ |
| 64 | postNo: item.dynamic_id, // 使用API返回的dynamic_id作为帖子ID |
| 65 | title: item.title, |
| 66 | postContent: item.content, |
| 67 | imageUrl: item.images, // 使用API返回的images字段 |
| 68 | postTime: item.time, // 使用API返回的time字段 |
| 69 | postLikeNum: item.likes?.length || 0, // 点赞数 |
| 70 | liked: item.likes?.some(like => like.user_id === userId), // 当前用户是否已点赞 |
| 71 | user_id: item.user_id, // 发布者ID |
| 72 | username: item.username, // 发布者昵称 |
| 73 | avatar_url: item.avatar_url, // 发布者头像 |
| 74 | comments: item.comments || [] // 评论列表 |
| 75 | })); |
| 76 | |
| 77 | setFeeds(formattedFeeds); |
| 78 | setFilteredFeeds(formattedFeeds); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 79 | } catch (err) { |
| 80 | console.error('获取动态列表失败:', err); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 81 | setError('获取动态列表失败,请稍后重试'); |
| 82 | } finally { |
| 83 | setLoading(false); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
| 87 | useEffect(() => { |
| 88 | fetchFeeds(); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 89 | }, [userId]); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 90 | |
| 91 | // 搜索处理 |
| 92 | const handleSearch = () => { |
| 93 | const q = query.trim().toLowerCase(); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 94 | if (!q) { |
| 95 | setFilteredFeeds(feeds); |
| 96 | return; |
| 97 | } |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 98 | setFilteredFeeds( |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 99 | feeds.filter(f => |
| 100 | (f.title || '').toLowerCase().includes(q) || |
| 101 | (f.postContent || '').toLowerCase().includes(q) |
| 102 | ) |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 103 | ); |
| 104 | }; |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 105 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 106 | const handleReset = () => { |
| 107 | setQuery(''); |
| 108 | setFilteredFeeds(feeds); |
| 109 | }; |
| 110 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 111 | // 对话框内:处理图片选择 |
| 112 | const handleImageChange = (e) => { |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 113 | const files = Array.from(e.target.files); |
| 114 | if (!files.length) return; |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 115 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 116 | const previewUrls = files.map(file => URL.createObjectURL(file)); |
| 117 | |
| 118 | setSelectedImages(files); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 119 | setPreviewUrls(previewUrls); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // 对话框内:提交新动态 |
| 123 | const handleSubmit = async () => { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 124 | if (!isLoggedIn) { |
| 125 | alert('请先登录'); |
| 126 | return; |
| 127 | } |
| 128 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 129 | if (!content.trim()) { |
| 130 | alert('内容不能为空'); |
| 131 | return; |
| 132 | } |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 133 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 134 | try { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 135 | // 使用formData格式提交 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 136 | const formData = new FormData(); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 137 | formData.append('title', title.trim() || ''); |
| 138 | formData.append('content', content.trim()); |
| 139 | |
| 140 | // 添加图片文件 |
| 141 | selectedImages.forEach((file, index) => { |
| 142 | formData.append('image_url', file); |
| 143 | }); |
| 144 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 145 | // 调用创建动态API |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 146 | await axios.post(`/echo/dynamic/${userId}/createDynamic`, formData, { |
| 147 | headers: { |
| 148 | 'Content-Type': 'multipart/form-data' |
| 149 | } |
| 150 | }); |
| 151 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 152 | // 重置表单 |
| 153 | setTitle(''); |
| 154 | setContent(''); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 155 | setSelectedImages([]); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 156 | setPreviewUrls([]); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 157 | setShowModal(false); |
| 158 | fetchFeeds(); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 159 | alert('发布成功'); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 160 | } catch (err) { |
| 161 | console.error('发布失败', err); |
| 162 | alert('发布失败,请稍后重试'); |
| 163 | } |
| 164 | }; |
| 165 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 166 | // 删除动态 - 注意:API文档中未提供删除接口,这里保留原代码 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 167 | const handleDelete = async (dynamicId) => { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 168 | |
| 169 | if (!isLoggedIn) { |
| 170 | alert('请先登录'); |
| 171 | return; |
| 172 | } |
| 173 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 174 | if (!window.confirm('确定要删除这条动态吗?')) return; |
| 175 | try { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 176 | // 注意:API文档中未提供删除接口,这里使用原代码中的路径 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 177 | await axios.delete(`/echo/dynamic/me/deleteDynamic/${dynamicId}`); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 178 | fetchFeeds(); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 179 | alert('删除成功'); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 180 | } catch (err) { |
| 181 | console.error('删除失败', err); |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 182 | alert('删除失败,请稍后重试'); |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | // 点赞动态 |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 187 | const handleLike = async (dynamicId, islike) => { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 188 | if (islike) { |
| 189 | handleUnlike(dynamicId); |
| 190 | return |
| 191 | } |
| 192 | if (!isLoggedIn) { |
| 193 | alert('请先登录'); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // 验证dynamicId是否有效 |
| 198 | if (!dynamicId) { |
| 199 | console.error('无效的dynamicId:', dynamicId); |
| 200 | alert('点赞失败:动态ID无效'); |
| 201 | return; |
| 202 | } |
| 203 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 204 | console.log('当前用户ID:', userId); |
| 205 | console.log('即将点赞的动态ID:', dynamicId); |
| 206 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 207 | try { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 208 | // 确保参数是整数类型 |
| 209 | const requestData = { |
| 210 | userId: parseInt(userId), |
| 211 | dynamicId: parseInt(dynamicId) |
| 212 | }; |
| 213 | |
| 214 | // 验证参数是否为有效数字 |
| 215 | if (isNaN(requestData.userId) || isNaN(requestData.dynamicId)) { |
| 216 | console.error('无效的参数:', requestData); |
| 217 | alert('点赞失败:参数格式错误'); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | console.log('点赞请求数据:', requestData); |
| 222 | |
| 223 | const res = await axios.post(`/echo/dynamic/like`, requestData, { |
| 224 | headers: { |
| 225 | 'Content-Type': 'application/json' // 明确指定JSON格式 |
| 226 | } |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 227 | }); |
| 228 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 229 | console.log('点赞API响应:', res.data); |
| 230 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 231 | if (res.status === 200) { |
| 232 | // 更新本地状态 |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 233 | feeds.forEach(feed => { |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 234 | if (feed.postNo === dynamicId) { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 235 | feed.postLikeNum = (feed.postLikeNum || 0) + 1; |
| 236 | feed.liked = true; |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 237 | } |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 238 | }); |
| 239 | setFeeds([...feeds]); // 更新状态以触发重新渲染 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 240 | } else { |
| 241 | alert(res.data.message || '点赞失败'); |
| 242 | } |
| 243 | } catch (err) { |
| 244 | console.error('点赞失败', err); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 245 | |
| 246 | // 检查错误响应,获取更详细的错误信息 |
| 247 | if (err.response) { |
| 248 | console.error('错误响应数据:', err.response.data); |
| 249 | console.error('错误响应状态:', err.response.status); |
| 250 | console.error('错误响应头:', err.response.headers); |
| 251 | } |
| 252 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 253 | alert('点赞失败,请稍后重试'); |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | // 取消点赞 |
| 258 | const handleUnlike = async (dynamicId) => { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 259 | if (!isLoggedIn) { |
| 260 | alert('请先登录'); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | // 验证dynamicId是否有效 |
| 265 | if (!dynamicId) { |
| 266 | console.error('无效的dynamicId:', dynamicId); |
| 267 | alert('取消点赞失败:动态ID无效'); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // 检查是否已经取消点赞,防止重复请求 |
| 272 | const currentFeed = feeds.find(feed => feed.postNo === dynamicId); |
| 273 | if (currentFeed && !currentFeed.liked) { |
| 274 | console.warn('尝试重复取消点赞,已忽略'); |
| 275 | return; |
| 276 | } |
| 277 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 278 | try { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 279 | // 确保参数是整数类型 |
| 280 | const requestData = { |
| 281 | userId: parseInt(userId), |
| 282 | dynamicId: parseInt(dynamicId) |
| 283 | }; |
| 284 | |
| 285 | // 验证参数是否为有效数字 |
| 286 | if (isNaN(requestData.userId) || isNaN(requestData.dynamicId)) { |
| 287 | console.error('无效的参数:', requestData); |
| 288 | alert('取消点赞失败:参数格式错误'); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | console.log('取消点赞请求数据:', requestData); |
| 293 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 294 | const res = await axios.delete(`/echo/dynamic/unlike`, { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 295 | headers: { |
| 296 | 'Content-Type': 'application/json' // 明确指定JSON格式 |
| 297 | }, |
| 298 | data: requestData // 将参数放在data属性中 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 299 | }); |
| 300 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 301 | console.log('取消点赞API响应:', res.data); |
| 302 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 303 | if (res.status === 200) { |
| 304 | // 更新本地状态 |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 305 | feeds.forEach(feed => { |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 306 | if (feed.postNo === dynamicId) { |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 307 | feed.postLikeNum = Math.max(0, (feed.postLikeNum || 0) - 1); |
| 308 | feed.liked = false; |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 309 | } |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 310 | }); |
| 311 | setFeeds([...feeds]); // 更新状态以触发重新渲染 |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 312 | } else { |
| 313 | alert(res.data.message || '取消点赞失败'); |
| 314 | } |
| 315 | } catch (err) { |
| 316 | console.error('取消点赞失败', err); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 317 | |
| 318 | // 检查错误响应,获取更详细的错误信息 |
| 319 | if (err.response) { |
| 320 | console.error('错误响应数据:', err.response.data); |
| 321 | console.error('错误响应状态:', err.response.status); |
| 322 | console.error('错误响应头:', err.response.headers); |
| 323 | } |
| 324 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 325 | alert('取消点赞失败,请稍后重试'); |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 326 | } |
| 327 | }; |
| 328 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 329 | // 评论好友动态 |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 330 | const handleComment = async (dynamicId) => { |
| 331 | if (!isLoggedIn) { |
| 332 | alert('请先登录'); |
| 333 | return; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 334 | } |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 335 | |
| 336 | if (!commentInput.trim()) { |
| 337 | alert('评论内容不能为空'); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | try { |
| 342 | // 准备请求数据 |
| 343 | const requestData = { |
| 344 | content: commentInput.trim() |
| 345 | }; |
| 346 | |
| 347 | // 如果是回复,添加parent_comment_id |
| 348 | if (replyToCommentId) { |
| 349 | requestData.parent_comment_id = replyToCommentId; |
| 350 | } |
| 351 | |
| 352 | const res = await axios.post(`/echo/dynamic/${userId}/feeds/${dynamicId}/comments`, requestData); |
| 353 | |
| 354 | if (res.status === 200 || res.status === 201) { |
| 355 | // 成功获取评论数据 |
| 356 | const newComment = { |
| 357 | user_id: userId, |
| 358 | username: username, |
| 359 | content: commentInput.trim(), |
| 360 | time: new Date().toISOString(), // 使用当前时间作为评论时间 |
| 361 | // 如果是回复,添加parent_comment_id和reply_to_username |
| 362 | ...(replyToCommentId && { parent_comment_id: replyToCommentId }), |
| 363 | ...(replyToUsername && { reply_to_username: replyToUsername }) |
| 364 | }; |
| 365 | |
| 366 | // 更新本地状态,添加新评论 |
| 367 | setFeeds(prevFeeds => { |
| 368 | return prevFeeds.map(feed => { |
| 369 | if (feed.postNo === dynamicId) { |
| 370 | // 确保comments是数组,并且正确合并新评论 |
| 371 | const currentComments = Array.isArray(feed.comments) ? feed.comments : []; |
| 372 | return { |
| 373 | ...feed, |
| 374 | comments: [...currentComments, newComment] |
| 375 | }; |
| 376 | } |
| 377 | return feed; |
| 378 | }); |
| 379 | }); |
| 380 | |
| 381 | // 更新过滤后的动态列表 |
| 382 | setFilteredFeeds(prevFeeds => { |
| 383 | return prevFeeds.map(feed => { |
| 384 | if (feed.postNo === dynamicId) { |
| 385 | // 确保comments是数组,并且正确合并新评论 |
| 386 | const currentComments = Array.isArray(feed.comments) ? feed.comments : []; |
| 387 | return { |
| 388 | ...feed, |
| 389 | comments: [...currentComments, newComment] |
| 390 | }; |
| 391 | } |
| 392 | return feed; |
| 393 | }); |
| 394 | }); |
| 395 | |
| 396 | // 清空回复状态 |
| 397 | setReplyToCommentId(null); |
| 398 | setReplyToUsername(''); |
| 399 | setCommentInput(''); |
| 400 | setCommentBoxVisibleId(null); // 关闭评论框 |
| 401 | } else { |
| 402 | alert(res.data.error || '评论失败'); |
| 403 | } |
| 404 | } catch (err) { |
| 405 | console.error('评论失败', err); |
| 406 | alert('评论失败,请稍后重试'); |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | // 切换回复框显示状态 |
| 411 | const toggleReplyBox = (dynamicId, commentId = null, username = '') => { |
| 412 | // 如果点击的是当前正在回复的评论,关闭回复框 |
| 413 | if (commentBoxVisibleId === dynamicId && replyToCommentId === commentId) { |
| 414 | setCommentBoxVisibleId(null); |
| 415 | setReplyToCommentId(null); |
| 416 | setReplyToUsername(''); |
| 417 | setCommentInput(''); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // 显示回复框,设置回复目标 |
| 422 | setCommentBoxVisibleId(dynamicId); |
| 423 | setReplyToCommentId(commentId); |
| 424 | setReplyToUsername(username); |
| 425 | setCommentInput(username ? `回复 ${username}: ` : ''); |
| 426 | }; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 427 | |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 428 | return ( |
| 429 | <div className="friend-moments-container"> |
| 430 | <Header /> |
| 431 | <div className="fm-header"> |
| 432 | <button className="create-btn" onClick={() => setShowModal(true)}> |
| 433 | <Edit theme="outline" size="18" style={{ marginRight: '6px' }} /> |
| 434 | 创建动态 |
| 435 | </button> |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 436 | </div> |
| 437 | |
| 438 | <div className="feed-list"> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 439 | {loading ? ( |
| 440 | <div className="loading-message">加载中...</div> |
| 441 | ) : error ? ( |
| 442 | <div className="error-message">{error}</div> |
| 443 | ) : !isLoggedIn ? ( |
| 444 | <div className="login-prompt"> |
| 445 | <p>请先登录查看好友动态</p> |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 446 | </div> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 447 | ) : filteredFeeds.length === 0 ? ( |
| 448 | <div className="empty-message">暂无动态</div> |
| 449 | ) : ( |
| 450 | filteredFeeds.map(feed => ( |
| 451 | <div className="feed-item" key={feed.postNo || `feed-${Math.random()}`}> |
| 452 | {/* 显示发布者信息 */} |
| 453 | <div className="feed-author"> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 454 | <img |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 455 | style={{ width: '70px', height: '70px', borderRadius: '50%' }} |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 456 | src={feed.avatar_url || 'https://example.com/default-avatar.jpg'} |
| 457 | alt={feed.username || '用户头像'} |
| 458 | /> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 459 | <div> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 460 | <div style={{ fontWeight: 'bold', fontSize: '20px', marginBottom: '5px' }}>{feed.username || '未知用户'}</div> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 461 | <span className="feed-date">{new Date(feed.postTime || Date.now()).toLocaleString()}</span> |
| 462 | </div> |
| 463 | </div> |
| 464 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 465 | {feed.title && <h4 style={{ fontWeight: 'bold', fontSize: '18px', margin: '15px 0' }}>{feed.title}</h4>} |
| 466 | <div style={{ margin: '20px 0' }}>{feed.postContent || '无内容'}</div> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 467 | |
| 468 | {feed.imageUrl && ( |
| 469 | <div className="feed-images"> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 470 | {typeof feed.imageUrl === 'string' ? ( |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 471 | <img src={formatImageUrl(feed.imageUrl)} alt="动态图片" /> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 472 | ) : ( |
| 473 | feed.imageUrl.map((url, i) => ( |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 474 | <img key={i} src={formatImageUrl(url)} alt={`动态图${i}`} /> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 475 | )) |
| 476 | )} |
| 477 | </div> |
| 478 | )} |
| 479 | |
| 480 | <div className="feed-footer"> |
| 481 | <div className="like-container"> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 482 | <button className="icon-btn" onClick={() => handleLike(feed.postNo, feed.liked, feed.user_id)}> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 483 | <GoodTwo theme="outline" size="24" fill={feed.liked ? '#ffa600dd' : '#000000'} /> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 484 | <span>{feed.postLikeNum || 0}</span> |
| 485 | </button> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 486 | |
| 487 | <button |
| 488 | className="icon-btn" |
| 489 | onClick={() => { |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 490 | toggleReplyBox(feed.postNo); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 491 | }} |
| 492 | > |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 493 | <Comment theme="outline" size="24" fill="#333" />评论 |
| 494 | {/* <span style={{ fontSize: '14px', color: '#333' }}>评论</span> */} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 495 | </button> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 496 | </div> |
| 497 | |
| 498 | {feed.user_id === userId && ( |
| 499 | <button className="delete-btn" onClick={() => handleDelete(feed.postNo)}> |
| 500 | 删除 |
| 501 | </button> |
| 502 | )} |
| 503 | </div> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 504 | |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 505 | {/* 评论输入框 */} |
| 506 | {commentBoxVisibleId === feed.postNo && ( |
| 507 | <div className="comment-box"> |
| 508 | <textarea |
| 509 | className="comment-input" |
| 510 | placeholder={replyToUsername ? `回复 ${replyToUsername}...` : '请输入评论内容...'} |
| 511 | value={commentInput} |
| 512 | onChange={(e) => setCommentInput(e.target.value)} |
| 513 | /> |
| 514 | <button |
| 515 | className="submit-comment-btn" |
| 516 | onClick={() => handleComment(feed.postNo)} |
| 517 | > |
| 518 | 发布评论 |
| 519 | </button> |
| 520 | </div> |
| 521 | )} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 522 | |
| 523 | {/* 评论列表 */} |
| 524 | {Array.isArray(feed.comments) && feed.comments.length > 0 && ( |
| 525 | <div className="comments-container"> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 526 | <h5 style={{ fontWeight: 'bold', fontSize: '18px', marginTop: '10px', marginBottom: '20px' }}>评论 ({feed.comments.length})</h5> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 527 | <div className="comments-list"> |
| 528 | {feed.comments.map((comment, index) => ( |
| 529 | <div className="comment-item" key={index}> |
| 530 | <div className="comment-header"> |
| 531 | <span className="comment-user">{comment.username || '用户'}</span> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 532 | <span className="comment-time"> |
| 533 | {new Date(comment.time || Date.now()).toLocaleString()} |
| 534 | </span> |
| 535 | </div> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 536 | <p className="comment-content"> |
| 537 | {/* 显示回复格式 */} |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 538 | {/* {comment.reply_to_username ? |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 539 | <span className="reply-prefix">{comment.username} 回复 {comment.reply_to_username}:</span> : |
| 540 | <span>{comment.username}:</span> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame] | 541 | } */} |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 542 | {comment.content} |
| 543 | </p> |
| 544 | <button |
| 545 | className="reply-btn" |
| 546 | onClick={() => toggleReplyBox(feed.postNo, comment.id || index, comment.username)} |
| 547 | > |
| 548 | 回复 |
| 549 | </button> |
| 550 | |
| 551 | {/* 嵌套回复 */} |
| 552 | {Array.isArray(comment.replies) && comment.replies.length > 0 && ( |
| 553 | <div className="nested-replies"> |
| 554 | {comment.replies.map((reply, replyIndex) => ( |
| 555 | <div className="reply-item" key={replyIndex}> |
| 556 | <p className="reply-content"> |
| 557 | {reply.reply_to_username ? |
| 558 | <span className="reply-prefix">{reply.username} 回复 {reply.reply_to_username}:</span> : |
| 559 | <span>{reply.username}:</span> |
| 560 | } |
| 561 | {reply.content} |
| 562 | </p> |
| 563 | <button |
| 564 | className="reply-btn" |
| 565 | onClick={() => toggleReplyBox(feed.postNo, reply.id || `${index}-${replyIndex}`, reply.username)} |
| 566 | > |
| 567 | 回复 |
| 568 | </button> |
| 569 | </div> |
| 570 | ))} |
| 571 | </div> |
| 572 | )} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 573 | </div> |
| 574 | ))} |
| 575 | </div> |
| 576 | </div> |
| 577 | )} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 578 | </div> |
| 579 | )) |
| 580 | )} |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 581 | </div> |
| 582 | |
| 583 | {/* Modal 对话框 */} |
| 584 | {showModal && ( |
| 585 | <div className="modal-overlay" onClick={() => setShowModal(false)}> |
| 586 | <div className="modal-dialog" onClick={e => e.stopPropagation()}> |
| 587 | <h3>发布新动态</h3> |
| 588 | <input |
| 589 | type="text" |
| 590 | placeholder="标题" |
| 591 | value={title} |
| 592 | onChange={e => setTitle(e.target.value)} |
| 593 | /> |
| 594 | <textarea |
| 595 | placeholder="写下你的内容..." |
| 596 | value={content} |
| 597 | onChange={e => setContent(e.target.value)} |
| 598 | /> |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 599 | <label className="file-label"> |
| 600 | 选择图片 |
| 601 | <input |
| 602 | type="file" |
| 603 | accept="image/*" |
| 604 | multiple |
| 605 | onChange={handleImageChange} |
| 606 | style={{ display: 'none' }} |
| 607 | /> |
| 608 | </label> |
| 609 | <div className="cf-preview"> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 610 | {previewUrls.map((url, i) => ( |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 611 | <img key={i} src={url} alt={`预览${i}`} /> |
| 612 | ))} |
| 613 | </div> |
| 614 | <div className="modal-actions"> |
| 615 | <button className="btn cancel" onClick={() => setShowModal(false)}> |
| 616 | 取消 |
| 617 | </button> |
| 618 | <button className="btn submit" onClick={handleSubmit}> |
| 619 | 发布 |
| 620 | </button> |
| 621 | </div> |
| 622 | </div> |
| 623 | </div> |
| 624 | )} |
| 625 | </div> |
| 626 | ); |
| 627 | }; |
| 628 | |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 629 | export default FriendMoments; |