blob: 9aa47a1d7ee4620fd6d1f1f39ef7118c4b4f5d96 [file] [log] [blame]
刘嘉昕9cdeca22025-06-03 16:54:30 +08001const BASE_URL = 'http://localhost:8080/post';
2
3/**
4 * 创建帖子(带图片)
5 * @param {FormData} formData 包含 userid、post_title、post_content、tags、rannge、is_pinned、photo
6 */
7export 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 */
18export 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 */
28export 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 */
42export 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 */
51export 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 */
61export 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 */
71export 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 */
81export 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 */
91export const findPostsByUserId = (userid) => {
92 return fetch(`${BASE_URL}/findByUserid?userid=${userid}`)
93 .then(res => res.json());
94};
95
96/**
97 * 获取所有置顶帖子
98 */
99export const findPinnedPosts = () => {
100 return fetch(`${BASE_URL}/findPinned`)
101 .then(res => res.json());
102};
103
104/**
105 * 获取所有帖子(排序后)
106 */
107export const getAllPostsSorted = () => {
108 return fetch(`${BASE_URL}/all`)
109 .then(res => res.json());
110};