TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 1 | // src/api/posts.js |
trm | 36915ed | 2025-06-20 09:48:51 +0000 | [diff] [blame^] | 2 | const BASE = 'http://192.168.5.235:5714' // 如果有代理可以留空,否则填完整域名,如 'http://localhost:3000' |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 3 | |
| 4 | /** |
wu | 2f28f67 | 2025-06-19 14:29:30 +0800 | [diff] [blame] | 5 | * 获取帖子列表 |
| 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-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 11 | */ |
wu | 2f28f67 | 2025-06-19 14:29:30 +0800 | [diff] [blame] | 12 | export 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-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /** |
| 26 | * 查看单个帖子详情 |
| 27 | * GET /posts/{postId} |
| 28 | */ |
| 29 | export 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 | */ |
TRM-coding | f55d237 | 2025-06-20 16:22:37 +0800 | [diff] [blame] | 39 | export async function createPost(formData) { |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 40 | const res = await fetch(`${BASE}/posts`, { |
| 41 | method: 'POST', |
TRM-coding | f55d237 | 2025-06-20 16:22:37 +0800 | [diff] [blame] | 42 | // 不设置Content-Type,让浏览器自动设置multipart/form-data |
| 43 | body: formData |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 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 | */ |
TRM-coding | f55d237 | 2025-06-20 16:22:37 +0800 | [diff] [blame] | 56 | export async function updatePost(postId, formData) { |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 57 | const res = await fetch(`${BASE}/posts/${postId}`, { |
| 58 | method: 'PUT', |
TRM-coding | f55d237 | 2025-06-20 16:22:37 +0800 | [diff] [blame] | 59 | // 如果是FormData则不设置Content-Type,否则设置JSON |
| 60 | headers: formData instanceof FormData ? {} : { 'Content-Type': 'application/json' }, |
| 61 | body: formData instanceof FormData ? formData : JSON.stringify(formData) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 62 | }) |
| 63 | if (!res.ok) throw new Error(`updatePost(${postId}): ${res.status}`) |
| 64 | // 204 No Content |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * 删除帖子 |
| 69 | * DELETE /posts/{postId} |
| 70 | */ |
| 71 | export async function deletePost(postId) { |
| 72 | const res = await fetch(`${BASE}/posts/${postId}`, { |
| 73 | method: 'DELETE' |
| 74 | }) |
| 75 | if (!res.ok) throw new Error(`deletePost(${postId}): ${res.status}`) |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * 点赞 |
| 80 | * POST /posts/{postId}/like |
| 81 | */ |
| 82 | export async function likePost(postId, userId) { |
| 83 | const res = await fetch(`${BASE}/posts/${postId}/like`, { |
| 84 | method: 'POST', |
| 85 | headers: { 'Content-Type': 'application/json' }, |
| 86 | body: JSON.stringify({ user_id: userId }) |
| 87 | }) |
| 88 | if (!res.ok) { |
| 89 | const err = await res.json().catch(() => null) |
| 90 | throw new Error(err?.error || `likePost: ${res.status}`) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * 取消点赞 |
| 96 | * DELETE /posts/{postId}/like |
| 97 | */ |
| 98 | export async function unlikePost(postId, userId) { |
| 99 | const res = await fetch(`${BASE}/posts/${postId}/like`, { |
| 100 | method: 'DELETE', |
| 101 | headers: { 'Content-Type': 'application/json' }, |
| 102 | body: JSON.stringify({ user_id: userId }) |
| 103 | }) |
| 104 | if (!res.ok) { |
| 105 | const err = await res.json().catch(() => null) |
| 106 | throw new Error(err?.error || `unlikePost: ${res.status}`) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * 收藏、取消收藏、浏览、分享 等接口: |
| 112 | * POST /posts/{postId}/favorite |
| 113 | * DELETE /posts/{postId}/favorite |
| 114 | * POST /posts/{postId}/view |
| 115 | * POST /posts/{postId}/share |
| 116 | * 用法同上,替换路径即可 |
| 117 | */ |
| 118 | |
| 119 | /** |
| 120 | * 添加评论 |
| 121 | * POST /posts/{postId}/comments |
| 122 | */ |
| 123 | export async function addComment(postId, payload) { |
| 124 | const res = await fetch(`${BASE}/posts/${postId}/comments`, { |
| 125 | method: 'POST', |
| 126 | headers: { 'Content-Type': 'application/json' }, |
| 127 | body: JSON.stringify(payload) |
| 128 | }) |
| 129 | if (!res.ok) throw new Error(`addComment: ${res.status}`) |
| 130 | return res.json() // { id } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * 获取评论列表 |
| 135 | * GET /posts/{postId}/comments |
| 136 | */ |
| 137 | export async function fetchComments(postId) { |
| 138 | const res = await fetch(`${BASE}/posts/${postId}/comments`) |
| 139 | if (!res.ok) throw new Error(`fetchComments: ${res.status}`) |
| 140 | return res.json() |
| 141 | } |