Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | // src/api/helpPost.js
|
| 2 | import { api } from './auth'; // 复用已有的axios实例
|
| 3 |
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame^] | 4 | export const createPost = (title, content, authorId, selectedImage) => {
|
| 5 | // 创建 FormData 对象
|
| 6 | const formData = new FormData();
|
| 7 | formData.append('title', title);
|
| 8 | formData.append('content', content);
|
| 9 | formData.append('authorId', authorId);
|
| 10 |
|
| 11 | // 如果有图片,添加到 FormData
|
| 12 | if (selectedImage) {
|
| 13 | formData.append('image', selectedImage);
|
| 14 | }
|
| 15 |
|
| 16 | return api.post('/help/posts', formData);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 17 | };
|
| 18 |
|
| 19 | export const getPosts = (page = 1, size = 5) => {
|
| 20 | return api.get('/help/posts', {
|
| 21 | params: { page, size }
|
| 22 | });
|
| 23 | };
|
| 24 |
|
| 25 | export const getPostDetail = (postId) => {
|
| 26 | return api.get(`/help/posts/${postId}`);
|
| 27 | };
|
| 28 |
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame^] | 29 | export const likePost = (postId, data) => {
|
| 30 | return api.post(`/help/posts/${postId}/like`, null, {
|
| 31 | params: data
|
| 32 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 33 | };
|
| 34 |
|
| 35 | export const addPostComment = (postId, commentData) => {
|
Akane1217 | d1e9f71 | 2025-05-29 14:36:56 +0800 | [diff] [blame^] | 36 | // 创建FormData对象来处理文件上传
|
| 37 | const formData = new FormData();
|
| 38 | formData.append('authorId', commentData.authorId);
|
| 39 | formData.append('content', commentData.content);
|
| 40 |
|
| 41 | // 如果有图片,添加到formData
|
| 42 | if (commentData.commentImage) {
|
| 43 | formData.append('image', commentData.commentImage);
|
| 44 | }
|
| 45 |
|
| 46 | return api.post(`/help/posts/${postId}/comments`, formData);
|
| 47 | };
|
| 48 |
|
| 49 | export const deletePost = (postId, authorId) => {
|
| 50 | return api.delete(`/help/posts/${postId}`, {
|
| 51 | params: { authorId }
|
| 52 | });
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 53 | }; |