blob: d05f1485786ba640fe4ae1b7a8a0366616c85639 [file] [log] [blame]
Krishya7ec1dd02025-04-19 15:29:03 +08001import axios from 'axios';
2
3const API_BASE = process.env.REACT_APP_API_BASE;
4
5// 获取帖子详情
6export const getPostDetail = async (postId) => {
7 const response = await axios.get(`${API_BASE}/echo/forum/posts/${postId}/getPost`);
8 return response.data;
9};
10
11// 获取帖子评论
12export const getPostComments = async (postId) => {
13 const response = await axios.get(`${API_BASE}/echo/forum/posts/${postId}/getAllComments`);
14 return response.data;
15};
16
17// 点赞帖子
22301009237217b2025-04-20 15:15:25 +080018export const likePost = async (postId, userId) => {
19 try {
20 const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/like`, {
21 user_id: userId, // 用户 ID
22 });
23 return response.data;
24 } catch (error) {
25 return handleApiError(error);
26 }
Krishya7ec1dd02025-04-19 15:29:03 +080027};
28
29// 取消点赞帖子
30export const unlikePost = async (postId) => {
31 const response = await axios.delete(`${API_BASE}/echo/forum/posts/${postId}/unlike`);
32 return response.data;
33};
34
35// 添加评论
22301009237217b2025-04-20 15:15:25 +080036export const addCommentToPost = async (postId, userId, content, isAnonymous, comCommentId = null) => {
37 try {
38 const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/comments`, {
39 content,
40 user_id: userId,
41 is_anonymous: isAnonymous,
42 com_comment_id: comCommentId, // 如果是回复评论,传递 com_comment_id
43 });
44 return response.data;
45 } catch (error) {
46 return handleApiError(error);
47 }
Krishya7ec1dd02025-04-19 15:29:03 +080048};
49
50// 点赞评论
51export const likeComment = async (commentId) => {
52 const response = await axios.post(`${API_BASE}/echo/forum/comments/${commentId}/like`);
53 return response.data;
54};
55
56// 取消点赞评论
57export const unlikeComment = async (commentId) => {
58 const response = await axios.delete(`${API_BASE}/echo/forum/comments/${commentId}/unlike`);
59 return response.data;
60};
61
22301009237217b2025-04-20 15:15:25 +080062// 收藏帖子
63export const collectPost = async (postId, userId, action) => {
64 try {
65 const response = await axios.post(`${API_BASE}/echo/forum/posts/${postId}/collect`, {
66 user_id: userId,
67 action: action, // "collect" 或 "cancel"
68 });
69 return response.data;
70 } catch (error) {
71 return handleApiError(error);
72 }
73};
74
Krishya7ec1dd02025-04-19 15:29:03 +080075// 获取用户信息
76export const getUserInfo = async (userId) => {
77 const response = await axios.get(`${API_BASE}/user/${userId}/info`);
78 return response.data;
22301009237217b2025-04-20 15:15:25 +080079};
80
81// 错误处理
82const handleApiError = (error) => {
83 if (error.response) {
84 return {
85 success: false,
86 message: error.response.data.error || '请求失败',
87 };
88 } else if (error.request) {
89 return {
90 success: false,
91 message: '请求未得到响应,请稍后重试',
92 };
93 } else {
94 return {
95 success: false,
96 message: error.message || '发生了未知错误',
97 };
98 }
99};