blob: 53dbfd31999d2f09d1f57e07cc7d588aa9cf0c5b [file] [log] [blame]
Krishya7ec1dd02025-04-19 15:29:03 +08001import axios from 'axios';
2
Krishya2283d882025-05-27 22:25:19 +08003
Krishya7ec1dd02025-04-19 15:29:03 +08004
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) => {
Krishya2283d882025-05-27 22:25:19 +08008 const response = await axios.get(`/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) => {
Krishya2283d882025-05-27 22:25:19 +080014 const response = await axios.get(`/echo/forum/posts/${post_id}/getAllComments`);
15 return response.data;
16};
17
18// 添加评论
19export const addCommentToPost = async (postId, commentPayload) => {
20 const res = await fetch(`/echo/forum/posts/${postId}/comments`, {
21 method: 'POST',
22 headers: { 'Content-Type': 'application/json' },
23 body: JSON.stringify(commentPayload),
24 });
25
26 if (!res.ok) {
27 throw new Error('请求失败,状态码:' + res.status);
28 }
29
30 // 试着解析 JSON,如果失败则返回 null(可能是空响应)
31 try {
32 const data = await res.json();
33 return data;
34 } catch {
35 return null;
36 }
37};
38
39
40
41// 点赞评论
42export const likeComment = async (commentId) => {
43 const response = await axios.post(`/echo/forum/comments/${commentId}/like`);
44 return response.data;
45};
46
47// 取消点赞评论
48export const unlikeComment = async (commentId) => {
49 const response = await axios.delete(`/echo/forum/comments/${commentId}/unlike`);
Krishya7ec1dd02025-04-19 15:29:03 +080050 return response.data;
51};
52
53// 点赞帖子
Krishya57cc17b2025-05-26 16:43:34 +080054export const likePost = async (post_id, userId) => {
22301009237217b2025-04-20 15:15:25 +080055 try {
Krishya2283d882025-05-27 22:25:19 +080056 const response = await axios.post(`/echo/forum/posts/${post_id}/like`, {
22301009237217b2025-04-20 15:15:25 +080057 user_id: userId,
22301009237217b2025-04-20 15:15:25 +080058 });
59 return response.data;
60 } catch (error) {
61 return handleApiError(error);
62 }
Krishya7ec1dd02025-04-19 15:29:03 +080063};
64
Krishya2283d882025-05-27 22:25:19 +080065// 取消点赞帖子(改为 POST 请求,携带 user_id)
66export const unlikePost = async (post_id, userId) => {
67 try {
68 const response = await axios.post(`/echo/forum/posts/${post_id}/unlike`, {
69 user_id: userId,
70 });
71 return response.data;
72 } catch (error) {
73 return handleApiError(error);
74 }
Krishya7ec1dd02025-04-19 15:29:03 +080075};
76
22301009237217b2025-04-20 15:15:25 +080077// 收藏帖子
Krishya2283d882025-05-27 22:25:19 +080078export const collectPost = async (post_id, userId) => {
22301009237217b2025-04-20 15:15:25 +080079 try {
Krishya2283d882025-05-27 22:25:19 +080080 const response = await axios.post(`/echo/forum/posts/${post_id}/collect`, {
22301009237217b2025-04-20 15:15:25 +080081 user_id: userId,
22301009237217b2025-04-20 15:15:25 +080082 });
83 return response.data;
84 } catch (error) {
85 return handleApiError(error);
86 }
87};
88
Krishya2283d882025-05-27 22:25:19 +080089// 取消收藏帖子(使用 POST 请求)
90export const uncollectPost = async (post_id, userId) => {
91 try {
92 const response = await axios.post(`/echo/forum/posts/${post_id}/uncollect`, {
93 user_id: userId,
94 });
95 return response.data;
96 } catch (error) {
97 return handleApiError(error);
98 }
99};
Krishya57cc17b2025-05-26 16:43:34 +0800100
Krishya2283d882025-05-27 22:25:19 +0800101
Krishya7ec1dd02025-04-19 15:29:03 +0800102export const getUserInfo = async (userId) => {
Krishya2283d882025-05-27 22:25:19 +0800103 const response = await axios.get(`/user/${userId}/info`);
Krishya7ec1dd02025-04-19 15:29:03 +0800104 return response.data;
22301009237217b2025-04-20 15:15:25 +0800105};
106
107// 错误处理
108const handleApiError = (error) => {
109 if (error.response) {
110 return {
111 success: false,
112 message: error.response.data.error || '请求失败',
113 };
114 } else if (error.request) {
115 return {
116 success: false,
117 message: '请求未得到响应,请稍后重试',
118 };
119 } else {
120 return {
121 success: false,
122 message: error.message || '发生了未知错误',
123 };
124 }
125};