blob: 5891f99c52f9b2507cc627278afc56c099c99c40 [file] [log] [blame]
ym9234558e9f2025-06-09 20:16:16 +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 togglePinPost = (postid) => {
19 return fetch(`${BASE_URL}/togglePin/${postid}`, {
20 method: 'PUT',
21 }).then(res => res.json());
22};
23
24/**
25 * 删除帖子
26 * @param {number} postid 帖子 ID
27 */
28export const deletePost = (postid) => {
29 return fetch(`${BASE_URL}/delete/${postid}`, {
30 method: 'DELETE',
31 }).then(res => res.json());
32};
33
34/**
35 * 更新帖子(JSON 格式)
36 * @param {Object} post 帖子对象
37 */
38export const updatePost = (post) => {
39 return fetch(`${BASE_URL}/update`, {
40 method: 'PUT',
41 headers: {
42 'Content-Type': 'application/json',
43 },
44 body: JSON.stringify(post),
45 }).then(res => res.json());
46};
47
48/**
49 * 关键词搜索帖子
50 * @param {string} keyword 搜索关键词
51 */
52export const searchPosts = (keyword) => {
53 return fetch(`${BASE_URL}/search?keyword=${encodeURIComponent(keyword)}`)
54 .then(res => res.json());
55};
56
57/**
58 * 点赞帖子
59 * @param {number} postid 帖子 ID
60 */
61export const likePost = (postid) => {
62 return fetch(`${BASE_URL}/like/${postid}`, {
63 method: 'PUT',
64 }).then(res => res.json());
65};
66
67/**
68 * 取消点赞帖子
69 * @param {number} postid 帖子 ID
70 */
71export const unlikePost = (postid) => {
72 return fetch(`${BASE_URL}/unlike/${postid}`, {
73 method: 'PUT',
74 }).then(res => res.json());
75};
76
77/**
78 * 置顶帖子
79 * @param {number} postid 帖子 ID
80 */
81export const pinPost = (postid) => {
82 return fetch(`${BASE_URL}/pin/${postid}`, {
83 method: 'PUT',
84 }).then(res => res.json());
85};
86
87/**
88 * 取消置顶帖子
89 * @param {number} postid 帖子 ID
90 */
91export const unpinPost = (postid) => {
92 return fetch(`${BASE_URL}/unpin/${postid}`, {
93 method: 'PUT',
94 }).then(res => res.json());
95};
96
97/**
98 * 获取某用户所有帖子
99 * @param {number} userid 用户 ID
100 */
101export const findPostsByUserId = (userid) => {
102 return fetch(`${BASE_URL}/findByUserid?userid=${userid}`)
103 .then(res => res.json());
104};
105
106/**
107 * 获取所有置顶帖子
108 */
109export const findPinnedPosts = () => {
110 return fetch(`${BASE_URL}/findPinned`)
111 .then(res => res.json());
112};
113
114/**
115 * 获取所有帖子(排序后)
116 */
117export const getAllPostsSorted = () => {
118 return fetch(`${BASE_URL}/all`)
119 .then(res => res.json());
120};
121
122/**
123 * 根据 postid 获取帖子详情(若你后期需要)
124 */
125export const getPostById = (postid) => {
126 return fetch(`${BASE_URL}/get/${postid}`)
127 .then(res => res.json());
128};