Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 1 | import axios from 'axios'; |
| 2 | |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 3 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 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) => { |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 8 | const response = await axios.get(`/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) => { |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 14 | const response = await axios.get(`/echo/forum/posts/${post_id}/getAllComments`); |
| 15 | return response.data; |
| 16 | }; |
| 17 | |
| 18 | // 添加评论 |
| 19 | export 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 | // 点赞评论 |
| 42 | export const likeComment = async (commentId) => { |
| 43 | const response = await axios.post(`/echo/forum/comments/${commentId}/like`); |
| 44 | return response.data; |
| 45 | }; |
| 46 | |
| 47 | // 取消点赞评论 |
| 48 | export const unlikeComment = async (commentId) => { |
| 49 | const response = await axios.delete(`/echo/forum/comments/${commentId}/unlike`); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 50 | return response.data; |
| 51 | }; |
| 52 | |
| 53 | // 点赞帖子 |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame] | 54 | export const likePost = async (post_id, userId) => { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 55 | try { |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 56 | const response = await axios.post(`/echo/forum/posts/${post_id}/like`, { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 57 | user_id: userId, |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 58 | }); |
| 59 | return response.data; |
| 60 | } catch (error) { |
| 61 | return handleApiError(error); |
| 62 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 65 | // 取消点赞帖子(改为 POST 请求,携带 user_id) |
| 66 | export 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 | } |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 77 | // 收藏帖子 |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 78 | export const collectPost = async (post_id, userId) => { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 79 | try { |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 80 | const response = await axios.post(`/echo/forum/posts/${post_id}/collect`, { |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 81 | user_id: userId, |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 82 | }); |
| 83 | return response.data; |
| 84 | } catch (error) { |
| 85 | return handleApiError(error); |
| 86 | } |
| 87 | }; |
| 88 | |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 89 | // 取消收藏帖子(使用 POST 请求) |
| 90 | export 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 | }; |
Krishya | 57cc17b | 2025-05-26 16:43:34 +0800 | [diff] [blame] | 100 | |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 101 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 102 | export const getUserInfo = async (userId) => { |
Krishya | 2283d88 | 2025-05-27 22:25:19 +0800 | [diff] [blame^] | 103 | const response = await axios.get(`/user/${userId}/info`); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 104 | return response.data; |
22301009 | 237217b | 2025-04-20 15:15:25 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // 错误处理 |
| 108 | const 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 | }; |