刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame^] | 1 | const BASE_URL = 'http://localhost:8080/post'; |
| 2 | |
| 3 | /** |
| 4 | * 创建帖子(带图片) |
| 5 | * @param {FormData} formData 包含 userid、post_title、post_content、tags、rannge、is_pinned、photo |
| 6 | */ |
| 7 | export const createPost = (formData) => { |
| 8 | return fetch(`${BASE_URL}/create`, { |
| 9 | method: 'POST', |
| 10 | body: formData, |
| 11 | }).then(res => res.json()); |
| 12 | }; |
| 13 | |
| 14 | /** |
| 15 | * 删除帖子 |
| 16 | * @param {number} postid 帖子 ID |
| 17 | */ |
| 18 | export const deletePost = (postid) => { |
| 19 | return fetch(`${BASE_URL}/delete/${postid}`, { |
| 20 | method: 'DELETE', |
| 21 | }).then(res => res.json()); |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * 更新帖子(JSON 格式) |
| 26 | * @param {Object} post 帖子对象 |
| 27 | */ |
| 28 | export const updatePost = (post) => { |
| 29 | return fetch(`${BASE_URL}/update`, { |
| 30 | method: 'PUT', |
| 31 | headers: { |
| 32 | 'Content-Type': 'application/json', |
| 33 | }, |
| 34 | body: JSON.stringify(post), |
| 35 | }).then(res => res.json()); |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * 关键词搜索帖子 |
| 40 | * @param {string} keyword 搜索关键词 |
| 41 | */ |
| 42 | export const searchPosts = (keyword) => { |
| 43 | return fetch(`${BASE_URL}/search?keyword=${encodeURIComponent(keyword)}`) |
| 44 | .then(res => res.json()); |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * 点赞帖子 |
| 49 | * @param {number} postid 帖子 ID |
| 50 | */ |
| 51 | export const likePost = (postid) => { |
| 52 | return fetch(`${BASE_URL}/like/${postid}`, { |
| 53 | method: 'PUT', |
| 54 | }).then(res => res.json()); |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * 取消点赞帖子 |
| 59 | * @param {number} postid 帖子 ID |
| 60 | */ |
| 61 | export const unlikePost = (postid) => { |
| 62 | return fetch(`${BASE_URL}/unlike/${postid}`, { |
| 63 | method: 'PUT', |
| 64 | }).then(res => res.json()); |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * 置顶帖子 |
| 69 | * @param {number} postid 帖子 ID |
| 70 | */ |
| 71 | export const pinPost = (postid) => { |
| 72 | return fetch(`${BASE_URL}/pin/${postid}`, { |
| 73 | method: 'PUT', |
| 74 | }).then(res => res.json()); |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * 取消置顶帖子 |
| 79 | * @param {number} postid 帖子 ID |
| 80 | */ |
| 81 | export const unpinPost = (postid) => { |
| 82 | return fetch(`${BASE_URL}/unpin/${postid}`, { |
| 83 | method: 'PUT', |
| 84 | }).then(res => res.json()); |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * 获取某用户所有帖子 |
| 89 | * @param {number} userid 用户 ID |
| 90 | */ |
| 91 | export const findPostsByUserId = (userid) => { |
| 92 | return fetch(`${BASE_URL}/findByUserid?userid=${userid}`) |
| 93 | .then(res => res.json()); |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * 获取所有置顶帖子 |
| 98 | */ |
| 99 | export const findPinnedPosts = () => { |
| 100 | return fetch(`${BASE_URL}/findPinned`) |
| 101 | .then(res => res.json()); |
| 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * 获取所有帖子(排序后) |
| 106 | */ |
| 107 | export const getAllPostsSorted = () => { |
| 108 | return fetch(`${BASE_URL}/all`) |
| 109 | .then(res => res.json()); |
| 110 | }; |