Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 1 | import axios from 'axios'; |
| 2 | |
| 3 | const API_BASE = process.env.REACT_APP_API_BASE; |
| 4 | |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 5 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 6 | // 获取帖子详情 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 7 | export const getPostDetail = async (post_id) => { |
| 8 | const response = await axios.get(`${API_BASE}/echo/forum/posts/${post_id}/getPost`); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 9 | return response.data; |
| 10 | }; |
| 11 | |
| 12 | // 获取帖子评论 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 13 | export const getPostComments = async (post_id) => { |
| 14 | const response = await axios.get(`${API_BASE}/echo/forum/posts/${post_id}/getAllComments`); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 15 | return response.data; |
| 16 | }; |
| 17 | |
| 18 | // 点赞帖子 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 19 | export const likePost = async (post_id, userId) => { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 20 | try { |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 21 | const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/like`, { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 22 | user_id: userId, // 用户 ID |
| 23 | }); |
| 24 | return response.data; |
| 25 | } catch (error) { |
| 26 | return handleApiError(error); |
| 27 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | // 取消点赞帖子 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 31 | export const unlikePost = async (post_id) => { |
| 32 | const response = await axios.delete(`${API_BASE}/echo/forum/posts/${post_id}/unlike`); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 33 | return response.data; |
| 34 | }; |
| 35 | |
| 36 | // 添加评论 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 37 | export const addCommentToPost = async (post_id, userId, content, isAnonymous, comCommentId = null) => { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 38 | try { |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 39 | const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/comments`, { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 40 | 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 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // 点赞评论 |
| 52 | export const likeComment = async (commentId) => { |
| 53 | const response = await axios.post(`${API_BASE}/echo/forum/comments/${commentId}/like`); |
| 54 | return response.data; |
| 55 | }; |
| 56 | |
| 57 | // 取消点赞评论 |
| 58 | export const unlikeComment = async (commentId) => { |
| 59 | const response = await axios.delete(`${API_BASE}/echo/forum/comments/${commentId}/unlike`); |
| 60 | return response.data; |
| 61 | }; |
| 62 | |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 63 | // 收藏帖子 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 64 | export const collectPost = async (post_id, userId, action) => { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 65 | try { |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 66 | const response = await axios.post(`${API_BASE}/echo/forum/posts/${post_id}/collect`, { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 67 | user_id: userId, |
| 68 | action: action, // "collect" 或 "cancel" |
| 69 | }); |
| 70 | return response.data; |
| 71 | } catch (error) { |
| 72 | return handleApiError(error); |
| 73 | } |
| 74 | }; |
| 75 | |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame^] | 76 | // // 取消收藏帖子 |
| 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 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 84 | // 获取用户信息 |
| 85 | export const getUserInfo = async (userId) => { |
| 86 | const response = await axios.get(`${API_BASE}/user/${userId}/info`); |
| 87 | return response.data; |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // 错误处理 |
| 91 | const 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 | }; |