刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame^] | 1 | import axios from 'axios'; |
| 2 | |
| 3 | const BASE_URL = 'http://localhost:8080/comment'; |
| 4 | |
| 5 | // 创建评论 |
| 6 | export const createComment = (commentData) => { |
| 7 | return axios.post(`${BASE_URL}/create`, commentData); |
| 8 | }; |
| 9 | |
| 10 | // 删除评论 |
| 11 | export const deleteComment = (commentId) => { |
| 12 | return axios.delete(`${BASE_URL}/delete/${commentId}`); |
| 13 | }; |
| 14 | |
| 15 | // 更新评论 |
| 16 | export const updateComment = (commentData) => { |
| 17 | return axios.put(`${BASE_URL}/update`, commentData); |
| 18 | }; |
| 19 | |
| 20 | // 获取某个帖子的所有评论 |
| 21 | // comment.js |
| 22 | export async function getCommentsByPostId(postid) { |
| 23 | try { |
| 24 | const response = await axios.get(`${BASE_URL}/post/${postid}`); |
| 25 | return Array.isArray(response.data) ? response.data : []; // 确保返回数据是数组 |
| 26 | } catch (error) { |
| 27 | console.error('获取评论失败', error); |
| 28 | return []; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // 点赞评论 |
| 33 | export const likeComment = (commentId) => { |
| 34 | return axios.post(`${BASE_URL}/like/${commentId}`); |
| 35 | }; |
| 36 | |
| 37 | // 取消点赞评论 |
| 38 | export const unlikeComment = (commentId) => { |
| 39 | return axios.post(`${BASE_URL}/unlike/${commentId}`); |
| 40 | }; |