修复帖子点赞和收藏功能
Change-Id: If66f9065607d97fb9527f0905b3465e6cd5b5995
diff --git a/src/pages/Forum/posts-detail/api.js b/src/pages/Forum/posts-detail/api.js
index 924cba9..d05f148 100644
--- a/src/pages/Forum/posts-detail/api.js
+++ b/src/pages/Forum/posts-detail/api.js
@@ -15,9 +15,15 @@
};
// 点赞帖子
-export const likePost = async (postId) => {
- const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/like`);
- return response.data;
+export const likePost = async (postId, userId) => {
+ try {
+ const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/like`, {
+ user_id: userId, // 用户 ID
+ });
+ return response.data;
+ } catch (error) {
+ return handleApiError(error);
+ }
};
// 取消点赞帖子
@@ -27,15 +33,18 @@
};
// 添加评论
-export const addCommentToPost = async (postId, content) => {
- const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/comments`, { content });
- return response.data;
-};
-
-// 回复评论
-export const replyToComment = async (commentId, replyContent) => {
- const response = await axios.post(`${API_BASE}/echo/forum/comments/${commentId}/reply`, { content: replyContent });
- return response.data;
+export const addCommentToPost = async (postId, userId, content, isAnonymous, comCommentId = null) => {
+ try {
+ const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/comments`, {
+ content,
+ user_id: userId,
+ is_anonymous: isAnonymous,
+ com_comment_id: comCommentId, // 如果是回复评论,传递 com_comment_id
+ });
+ return response.data;
+ } catch (error) {
+ return handleApiError(error);
+ }
};
// 点赞评论
@@ -50,8 +59,41 @@
return response.data;
};
+// 收藏帖子
+export const collectPost = async (postId, userId, action) => {
+ try {
+ const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/collect`, {
+ user_id: userId,
+ action: action, // "collect" 或 "cancel"
+ });
+ return response.data;
+ } catch (error) {
+ return handleApiError(error);
+ }
+};
+
// 获取用户信息
export const getUserInfo = async (userId) => {
const response = await axios.get(`${API_BASE}/user/${userId}/info`);
return response.data;
-};
\ No newline at end of file
+};
+
+// 错误处理
+const handleApiError = (error) => {
+ if (error.response) {
+ return {
+ success: false,
+ message: error.response.data.error || '请求失败',
+ };
+ } else if (error.request) {
+ return {
+ success: false,
+ message: '请求未得到响应,请稍后重试',
+ };
+ } else {
+ return {
+ success: false,
+ message: error.message || '发生了未知错误',
+ };
+ }
+};