blob: d9015000b3dd68749d6e66ea6aca144223e47bb8 [file] [log] [blame]
TRM-codingd1cbf672025-06-18 15:15:08 +08001// src/api/posts.js
wu2f28f672025-06-19 14:29:30 +08002const BASE = 'http://10.126.59.25:5714' // 如果有代理可以留空,否则填完整域名,如 'http://localhost:3000'
TRM-codingd1cbf672025-06-18 15:15:08 +08003
4/**
wu2f28f672025-06-19 14:29:30 +08005 * 获取帖子列表
6 * - GET /posts
7 * - GET /posts?user_id=123
8 *
9 * @param {number?} userId 可选,传了就加 ?user_id= 用户 ID
10 * @returns Promise<[{ id, title, status, heat, created_at }, …]>
TRM-codingd1cbf672025-06-18 15:15:08 +080011 */
wu2f28f672025-06-19 14:29:30 +080012export async function fetchPosts(userId) {
13 // 自动拼接 query
14 const url = userId != null
15 ? `${BASE}/posts?user_id=${encodeURIComponent(userId)}`
16 : `${BASE}/posts`
17
18 const res = await fetch(url)
19 if (!res.ok) {
20 throw new Error(`fetchPosts${userId != null ? `(user ${userId})` : ''}: ${res.status}`)
21 }
22 return res.json()
TRM-codingd1cbf672025-06-18 15:15:08 +080023}
24
25/**
26 * 查看单个帖子详情
27 * GET /posts/{postId}
28 */
29export async function fetchPost(postId) {
30 const res = await fetch(`${BASE}/posts/${postId}`)
31 if (!res.ok) throw new Error(`fetchPost(${postId}): ${res.status}`)
32 return res.json() // 返回完整的帖子对象
33}
34
35/**
36 * 发布新帖
37 * POST /posts
38 */
39export async function createPost(payload) {
40 const res = await fetch(`${BASE}/posts`, {
41 method: 'POST',
42 headers: { 'Content-Type': 'application/json' },
43 body: JSON.stringify(payload)
44 })
45 if (!res.ok) {
46 const err = await res.json().catch(() => null)
47 throw new Error(err?.error || `createPost: ${res.status}`)
48 }
49 return res.json() // { id }
50}
51
52/**
53 * 修改帖子
54 * PUT /posts/{postId}
55 */
56export async function updatePost(postId, payload) {
57 const res = await fetch(`${BASE}/posts/${postId}`, {
58 method: 'PUT',
59 headers: { 'Content-Type': 'application/json' },
60 body: JSON.stringify(payload)
61 })
62 if (!res.ok) throw new Error(`updatePost(${postId}): ${res.status}`)
63 // 204 No Content
64}
65
66/**
67 * 删除帖子
68 * DELETE /posts/{postId}
69 */
70export async function deletePost(postId) {
71 const res = await fetch(`${BASE}/posts/${postId}`, {
72 method: 'DELETE'
73 })
74 if (!res.ok) throw new Error(`deletePost(${postId}): ${res.status}`)
75}
76
77/**
78 * 点赞
79 * POST /posts/{postId}/like
80 */
81export async function likePost(postId, userId) {
82 const res = await fetch(`${BASE}/posts/${postId}/like`, {
83 method: 'POST',
84 headers: { 'Content-Type': 'application/json' },
85 body: JSON.stringify({ user_id: userId })
86 })
87 if (!res.ok) {
88 const err = await res.json().catch(() => null)
89 throw new Error(err?.error || `likePost: ${res.status}`)
90 }
91}
92
93/**
94 * 取消点赞
95 * DELETE /posts/{postId}/like
96 */
97export async function unlikePost(postId, userId) {
98 const res = await fetch(`${BASE}/posts/${postId}/like`, {
99 method: 'DELETE',
100 headers: { 'Content-Type': 'application/json' },
101 body: JSON.stringify({ user_id: userId })
102 })
103 if (!res.ok) {
104 const err = await res.json().catch(() => null)
105 throw new Error(err?.error || `unlikePost: ${res.status}`)
106 }
107}
108
109/**
110 * 收藏、取消收藏、浏览、分享 等接口:
111 * POST /posts/{postId}/favorite
112 * DELETE /posts/{postId}/favorite
113 * POST /posts/{postId}/view
114 * POST /posts/{postId}/share
115 * 用法同上,替换路径即可
116 */
117
118/**
119 * 添加评论
120 * POST /posts/{postId}/comments
121 */
122export async function addComment(postId, payload) {
123 const res = await fetch(`${BASE}/posts/${postId}/comments`, {
124 method: 'POST',
125 headers: { 'Content-Type': 'application/json' },
126 body: JSON.stringify(payload)
127 })
128 if (!res.ok) throw new Error(`addComment: ${res.status}`)
129 return res.json() // { id }
130}
131
132/**
133 * 获取评论列表
134 * GET /posts/{postId}/comments
135 */
136export async function fetchComments(postId) {
137 const res = await fetch(`${BASE}/posts/${postId}/comments`)
138 if (!res.ok) throw new Error(`fetchComments: ${res.status}`)
139 return res.json()
140}