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 | |
| 5 | // 获取帖子详情 |
| 6 | export const getPostDetail = async (postId) => { |
| 7 | const response = await axios.get(`${API_BASE}/echo/forum/posts/${postId}/getPost`); |
| 8 | return response.data; |
| 9 | }; |
| 10 | |
| 11 | // 获取帖子评论 |
| 12 | export const getPostComments = async (postId) => { |
| 13 | const response = await axios.get(`${API_BASE}/echo/forum/posts/${postId}/getAllComments`); |
| 14 | return response.data; |
| 15 | }; |
| 16 | |
| 17 | // 点赞帖子 |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 18 | export 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 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | // 取消点赞帖子 |
| 30 | export const unlikePost = async (postId) => { |
| 31 | const response = await axios.delete(`${API_BASE}/echo/forum/posts/${postId}/unlike`); |
| 32 | return response.data; |
| 33 | }; |
| 34 | |
| 35 | // 添加评论 |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 36 | export 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 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // 点赞评论 |
| 51 | export const likeComment = async (commentId) => { |
| 52 | const response = await axios.post(`${API_BASE}/echo/forum/comments/${commentId}/like`); |
| 53 | return response.data; |
| 54 | }; |
| 55 | |
| 56 | // 取消点赞评论 |
| 57 | export const unlikeComment = async (commentId) => { |
| 58 | const response = await axios.delete(`${API_BASE}/echo/forum/comments/${commentId}/unlike`); |
| 59 | return response.data; |
| 60 | }; |
| 61 | |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 62 | // 收藏帖子 |
| 63 | export 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 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 75 | // 获取用户信息 |
| 76 | export const getUserInfo = async (userId) => { |
| 77 | const response = await axios.get(`${API_BASE}/user/${userId}/info`); |
| 78 | return response.data; |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // 错误处理 |
| 82 | const 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 | }; |