修改帖子
Change-Id: I467b35242bce8b27d612eb96f2710b27aa03d1d2
diff --git a/src/pages/Forum/posts-detail/PostDetailPage.jsx b/src/pages/Forum/posts-detail/PostDetailPage.jsx
index a505d88..159bdfa 100644
--- a/src/pages/Forum/posts-detail/PostDetailPage.jsx
+++ b/src/pages/Forum/posts-detail/PostDetailPage.jsx
@@ -1,9 +1,9 @@
-import React, { useEffect, useState } from 'react';
+import React, { useContext, useEffect, useState } from 'react';
import { useParams } from 'wouter';
import { GoodTwo, Star } from '@icon-park/react';
import { getPostDetail, getPostComments, likePost, unlikePost, addCommentToPost, collectPost, uncollectPost } from './api'; // 引入你的 API 函数
import './PostDetailPage.css';
-import { useUser } from '../../../context/UserContext'; // 注意路径
+import { UserContext, useUser } from '../../../context/UserContext'; // 注意路径
import Header from '../../../components/Header';
const PostDetailPage = () => {
@@ -20,7 +20,8 @@
const [replyToUsername, setReplyToUsername] = useState(null);
// 获取当前用户ID(假设从上下文中获取)
- const { user } = useUser(); // 你需要从用户上下文获取用户 ID
+ const { user } = useContext(UserContext);
+ // const { user } = useUser(); // 你需要从用户上下文获取用户 ID
useEffect(() => {
const fetchPostDetail = async () => {
@@ -69,7 +70,7 @@
try {
if (isLiked) {
// 取消点赞
- await unlikePost(postId, user.id);
+ await unlikePost(postId, user.userId);
setIsLiked(false);
setPostDetail((prev) => ({
...prev,
@@ -77,7 +78,7 @@
}));
} else {
// 点赞
- await likePost(postId, user.id);
+ await likePost(postId, user.userId);
setIsLiked(true);
setPostDetail((prev) => ({
...prev,
@@ -100,7 +101,7 @@
try {
if (isCollected) {
// 取消收藏 - 使用原有的collectPost函数,传递action: "cancel"
- await collectPost(postId, user.id, "cancel");
+ await collectPost(postId, user.userId, "cancel");
setIsCollected(false);
setPostDetail((prev) => ({
...prev,
@@ -108,7 +109,7 @@
}));
} else {
// 收藏
- await collectPost(postId, user.id, "collect");
+ await collectPost(postId, user.userId, "collect");
setIsCollected(true);
setPostDetail((prev) => ({
...prev,
@@ -121,40 +122,57 @@
}
};
- // 添加评论
+// 添加评论
const handleAddComment = async () => {
+ // 直接使用组件顶层获取的 user
+ if (!user || !user.userId) {
+ alert('请先登录后再评论');
+ return;
+ }
+
if (!newComment.trim()) {
alert('评论内容不能为空');
return;
}
try {
+ // 构建评论数据
const commentPayload = {
content: newComment,
- userId: user.id,
+ userId: user.userId, // 使用已获取的用户ID
isAnonymous: false,
com_comment_id: replyToCommentId || null,
};
+ // 发送评论请求
const commentData = await addCommentToPost(postId, commentPayload);
- setComments((prev) => [
- ...prev,
- {
- commentId: (commentData && commentData.commentId) || Date.now(),
- post_id: postId,
- userId: user.id,
- content: newComment,
- commentTime: new Date().toISOString(),
- comCommentId: replyToCommentId,
- },
- ]);
+ // 更新评论列表
+ const newCommentItem = {
+ commentId: commentData?.commentId || Date.now(),
+ post_id: postId,
+ userId: user.userId,
+ content: newComment,
+ commentTime: new Date().toISOString(),
+ comCommentId: replyToCommentId,
+ };
+ setComments((prevComments) => [newCommentItem, ...prevComments]);
+
+ // 重置表单
setNewComment('');
setReplyToCommentId(null);
- } catch (err) {
- console.error('评论添加失败:', err);
- alert('评论失败,请稍后再试');
+
+ // alert('评论成功!');
+ } catch (error) {
+ console.error('评论失败:', error);
+
+ const errorMessage =
+ error.response?.data?.message ||
+ error.message ||
+ '评论失败,请稍后再试';
+
+ alert(errorMessage);
}
};
diff --git a/src/pages/Forum/promotion-part/Promotion.jsx b/src/pages/Forum/promotion-part/Promotion.jsx
index 36d6319..49863e3 100644
--- a/src/pages/Forum/promotion-part/Promotion.jsx
+++ b/src/pages/Forum/promotion-part/Promotion.jsx
@@ -1,16 +1,153 @@
+// import React, { useEffect, useState, useRef } from 'react';
+// import './Promotion.css';
+
+// const Promotion = () => {
+// const [promotions, setPromotions] = useState([]);
+// const [coldResources, setColdResources] = useState([]);
+// const [loading, setLoading] = useState(true);
+
+// const [promoIndex, setPromoIndex] = useState(0);
+// const [coldIndex, setColdIndex] = useState(0);
+
+// const promoTimerRef = useRef(null);
+// const coldTimerRef = useRef(null);
+
+// useEffect(() => {
+// fetchData();
+// }, []);
+
+// useEffect(() => {
+// if (promotions.length === 0) return;
+// clearInterval(promoTimerRef.current);
+// promoTimerRef.current = setInterval(() => {
+// setPromoIndex(prev => (prev + 1) % promotions.length);
+// }, 5000);
+// return () => clearInterval(promoTimerRef.current);
+// }, [promotions]);
+
+// useEffect(() => {
+// if (coldResources.length === 0) return;
+// clearInterval(coldTimerRef.current);
+// coldTimerRef.current = setInterval(() => {
+// setColdIndex(prev => (prev + 1) % coldResources.length);
+// }, 5000);
+// return () => clearInterval(coldTimerRef.current);
+// }, [coldResources]);
+
+// const fetchData = async () => {
+// try {
+// // ✅ 获取促销活动列表(新接口)
+// const promoResponse = await fetch(`seeds/promotions`);
+// const promoJson = await promoResponse.json();
+// const promoData = Array.isArray(promoJson?.result) ? promoJson.result : [];
+// setPromotions(promoData);
+
+// // 冷门资源(接口保持不变,若已更换请提供文档)
+// const coldResponse = await fetch(`/echo/resources/cold`);
+// const coldData = await coldResponse.json();
+// setColdResources(coldData);
+// } catch (error) {
+// console.error('获取数据失败:', error);
+// } finally {
+// setLoading(false);
+// }
+// };
+
+// if (loading) {
+// return <div className="promotion-container">加载中...</div>;
+// }
+
+// const prevPromo = () => setPromoIndex((promoIndex - 1 + promotions.length) % promotions.length);
+// const nextPromo = () => setPromoIndex((promoIndex + 1) % promotions.length);
+// const prevCold = () => setColdIndex((coldIndex - 1 + coldResources.length) % coldResources.length);
+// const nextCold = () => setColdIndex((coldIndex + 1) % coldResources.length);
+
+// const currentPromo = promotions[promoIndex];
+// const currentCold = coldResources[coldIndex];
+
+// return (
+// <div className="promotion-container carousel-container">
+// {/* 促销活动轮播 */}
+// <section className="carousel-section">
+// <h2>当前促销活动</h2>
+// {promotions.length === 0 || !currentPromo ? (
+// <div className="empty-state">暂无促销活动</div>
+// ) : (
+// <div
+// className="carousel"
+// onMouseEnter={() => clearInterval(promoTimerRef.current)}
+// onMouseLeave={() => {
+// promoTimerRef.current = setInterval(() => {
+// setPromoIndex(prev => (prev + 1) % promotions.length);
+// }, 3000);
+// }}
+// >
+// <button className="arrow left" onClick={prevPromo}><</button>
+// <div className="slide">
+// <div><strong>促销名称:</strong>{currentPromo?.name ?? '未知'}</div>
+// <div><strong>促销时间:</strong>
+// {currentPromo?.pStartTime && currentPromo?.pEndTime
+// ? `${new Date(currentPromo.pStartTime).toLocaleString()} ~ ${new Date(currentPromo.pEndTime).toLocaleString()}`
+// : '未知'}
+// </div>
+// <div><strong>上传奖励系数:</strong>{currentPromo?.uploadCoeff ?? '无'}</div>
+// <div><strong>下载折扣系数:</strong>{currentPromo?.downloadCoeff ?? '无'}</div>
+// {currentPromo?.description && (
+// <div><strong>描述:</strong>{currentPromo.description}</div>
+// )}
+// </div>
+// <button className="arrow right" onClick={nextPromo}>></button>
+// </div>
+// )}
+// </section>
+
+// {/* 冷门资源轮播 */}
+// <section className="carousel-section">
+// <h2>冷门资源推荐</h2>
+// {coldResources.length === 0 || !currentCold ? (
+// <div className="empty-state">暂无冷门资源推荐</div>
+// ) : (
+// <div
+// className="carousel"
+// onMouseEnter={() => clearInterval(coldTimerRef.current)}
+// onMouseLeave={() => {
+// coldTimerRef.current = setInterval(() => {
+// setColdIndex(prev => (prev + 1) % coldResources.length);
+// }, 3000);
+// }}
+// >
+// <button className="arrow left" onClick={prevCold}><</button>
+// <div className="slide cold-slide">
+// <img src={currentCold.poster} alt={currentCold.title} className="resource-poster" />
+// <div className="resource-info">
+// <div><strong>标题:</strong>{currentCold.title}</div>
+// <div><strong>下载量:</strong>{currentCold.download_count ?? 0} | <strong>种子数:</strong>{currentCold.seed_count ?? 0}</div>
+// <div><strong>激励:</strong>
+// {currentCold.incentives?.download_exempt && <span className="incentive-badge">免下载量</span>}
+// {currentCold.incentives?.extra_seed_bonus && <span className="incentive-badge">做种加成</span>}
+// {!currentCold.incentives?.download_exempt && !currentCold.incentives?.extra_seed_bonus && '无'}
+// </div>
+// </div>
+// </div>
+// <button className="arrow right" onClick={nextCold}>></button>
+// </div>
+// )}
+// </section>
+// </div>
+// );
+// };
+
+// export default Promotion;
+
import React, { useEffect, useState, useRef } from 'react';
import './Promotion.css';
const Promotion = () => {
const [promotions, setPromotions] = useState([]);
- const [coldResources, setColdResources] = useState([]);
const [loading, setLoading] = useState(true);
const [promoIndex, setPromoIndex] = useState(0);
- const [coldIndex, setColdIndex] = useState(0);
-
const promoTimerRef = useRef(null);
- const coldTimerRef = useRef(null);
useEffect(() => {
fetchData();
@@ -25,29 +162,16 @@
return () => clearInterval(promoTimerRef.current);
}, [promotions]);
- useEffect(() => {
- if (coldResources.length === 0) return;
- clearInterval(coldTimerRef.current);
- coldTimerRef.current = setInterval(() => {
- setColdIndex(prev => (prev + 1) % coldResources.length);
- }, 5000);
- return () => clearInterval(coldTimerRef.current);
- }, [coldResources]);
-
const fetchData = async () => {
try {
// ✅ 获取促销活动列表(新接口)
- const promoResponse = await fetch(`seeds/promotions`);
+ const promoResponse = await fetch(`/seeds/promotions`);
const promoJson = await promoResponse.json();
+ console.log('接口返回数据:', promoJson);
const promoData = Array.isArray(promoJson?.result) ? promoJson.result : [];
setPromotions(promoData);
-
- // 冷门资源(接口保持不变,若已更换请提供文档)
- const coldResponse = await fetch(`/echo/resources/cold`);
- const coldData = await coldResponse.json();
- setColdResources(coldData);
} catch (error) {
- console.error('获取数据失败:', error);
+ console.error('获取促销活动失败:', error);
} finally {
setLoading(false);
}
@@ -59,11 +183,7 @@
const prevPromo = () => setPromoIndex((promoIndex - 1 + promotions.length) % promotions.length);
const nextPromo = () => setPromoIndex((promoIndex + 1) % promotions.length);
- const prevCold = () => setColdIndex((coldIndex - 1 + coldResources.length) % coldResources.length);
- const nextCold = () => setColdIndex((coldIndex + 1) % coldResources.length);
-
const currentPromo = promotions[promoIndex];
- const currentCold = coldResources[coldIndex];
return (
<div className="promotion-container carousel-container">
@@ -100,39 +220,6 @@
</div>
)}
</section>
-
- {/* 冷门资源轮播 */}
- <section className="carousel-section">
- <h2>冷门资源推荐</h2>
- {coldResources.length === 0 || !currentCold ? (
- <div className="empty-state">暂无冷门资源推荐</div>
- ) : (
- <div
- className="carousel"
- onMouseEnter={() => clearInterval(coldTimerRef.current)}
- onMouseLeave={() => {
- coldTimerRef.current = setInterval(() => {
- setColdIndex(prev => (prev + 1) % coldResources.length);
- }, 3000);
- }}
- >
- <button className="arrow left" onClick={prevCold}><</button>
- <div className="slide cold-slide">
- <img src={currentCold.poster} alt={currentCold.title} className="resource-poster" />
- <div className="resource-info">
- <div><strong>标题:</strong>{currentCold.title}</div>
- <div><strong>下载量:</strong>{currentCold.download_count ?? 0} | <strong>种子数:</strong>{currentCold.seed_count ?? 0}</div>
- <div><strong>激励:</strong>
- {currentCold.incentives?.download_exempt && <span className="incentive-badge">免下载量</span>}
- {currentCold.incentives?.extra_seed_bonus && <span className="incentive-badge">做种加成</span>}
- {!currentCold.incentives?.download_exempt && !currentCold.incentives?.extra_seed_bonus && '无'}
- </div>
- </div>
- </div>
- <button className="arrow right" onClick={nextCold}>></button>
- </div>
- )}
- </section>
</div>
);
};