blob: 27c3a18ba4556ec7a69a8cc8395974007c714d59 [file] [log] [blame]
Krishya7ec1dd02025-04-19 15:29:03 +08001import axios from 'axios';
2
3const API_BASE = process.env.REACT_APP_API_BASE;
4
Krishya57cc17b2025-05-26 16:43:34 +08005
Krishya7ec1dd02025-04-19 15:29:03 +08006// 获取帖子详情
Krishya57cc17b2025-05-26 16:43:34 +08007export const getPostDetail = async (post_id) => {
8 const response = await axios.get(`${API_BASE}/echo/forum/posts/${post_id}/getPost`);
Krishya7ec1dd02025-04-19 15:29:03 +08009 return response.data;
10};
11
12// 获取帖子评论
Krishya57cc17b2025-05-26 16:43:34 +080013export const getPostComments = async (post_id) => {
14 const response = await axios.get(`${API_BASE}/echo/forum/posts/${post_id}/getAllComments`);
Krishya7ec1dd02025-04-19 15:29:03 +080015 return response.data;
16};
17
18// 点赞帖子
Krishya57cc17b2025-05-26 16:43:34 +080019export const likePost = async (post_id, userId) => {
22301009237217b2025-04-20 15:15:25 +080020 try {
Krishya57cc17b2025-05-26 16:43:34 +080021 const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/like`, {
22301009237217b2025-04-20 15:15:25 +080022 user_id: userId, // 用户 ID
23 });
24 return response.data;
25 } catch (error) {
26 return handleApiError(error);
27 }
Krishya7ec1dd02025-04-19 15:29:03 +080028};
29
30// 取消点赞帖子
Krishya57cc17b2025-05-26 16:43:34 +080031export const unlikePost = async (post_id) => {
32 const response = await axios.delete(`${API_BASE}/echo/forum/posts/${post_id}/unlike`);
Krishya7ec1dd02025-04-19 15:29:03 +080033 return response.data;
34};
35
36// 添加评论
Krishya57cc17b2025-05-26 16:43:34 +080037export const addCommentToPost = async (post_id, userId, content, isAnonymous, comCommentId = null) => {
22301009237217b2025-04-20 15:15:25 +080038 try {
Krishya57cc17b2025-05-26 16:43:34 +080039 const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/comments`, {
22301009237217b2025-04-20 15:15:25 +080040 content,
41 user_id: userId,
42 is_anonymous: isAnonymous,
43 com_comment_id: comCommentId, // 如果是回复评论,传递 com_comment_id
44 });
45 return response.data;
46 } catch (error) {
47 return handleApiError(error);
48 }
Krishya7ec1dd02025-04-19 15:29:03 +080049};
50
51// 点赞评论
52export const likeComment = async (commentId) => {
53 const response = await axios.post(`${API_BASE}/echo/forum/comments/${commentId}/like`);
54 return response.data;
55};
56
57// 取消点赞评论
58export const unlikeComment = async (commentId) => {
59 const response = await axios.delete(`${API_BASE}/echo/forum/comments/${commentId}/unlike`);
60 return response.data;
61};
62
22301009237217b2025-04-20 15:15:25 +080063// 收藏帖子
Krishya57cc17b2025-05-26 16:43:34 +080064export const collectPost = async (post_id, userId, action) => {
22301009237217b2025-04-20 15:15:25 +080065 try {
Krishya57cc17b2025-05-26 16:43:34 +080066 const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/collect`, {
22301009237217b2025-04-20 15:15:25 +080067 user_id: userId,
68 action: action, // "collect" 或 "cancel"
69 });
70 return response.data;
71 } catch (error) {
72 return handleApiError(error);
73 }
74};
75
Krishya57cc17b2025-05-26 16:43:34 +080076// // 取消收藏帖子
77// export const uncollectPost = async (post_id, userId) => {
78// const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/uncollect`, {
79// user_id: userId, // 用户 ID
80// });
81// return response.data;
82// };
83
Krishya7ec1dd02025-04-19 15:29:03 +080084// 获取用户信息
85export const getUserInfo = async (userId) => {
86 const response = await axios.get(`${API_BASE}/user/${userId}/info`);
87 return response.data;
22301009237217b2025-04-20 15:15:25 +080088};
89
90// 错误处理
91const handleApiError = (error) => {
92 if (error.response) {
93 return {
94 success: false,
95 message: error.response.data.error || '请求失败',
96 };
97 } else if (error.request) {
98 return {
99 success: false,
100 message: '请求未得到响应,请稍后重试',
101 };
102 } else {
103 return {
104 success: false,
105 message: error.message || '发生了未知错误',
106 };
107 }
108};