TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 1 | // src/api/posts.js |
wu | 2f28f67 | 2025-06-19 14:29:30 +0800 | [diff] [blame] | 2 | const BASE = 'http://10.126.59.25: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 | */ |
| 39 | export 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 | */ |
| 56 | export 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 | */ |
| 70 | export 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 | */ |
| 81 | export 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 | */ |
| 97 | export 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 | */ |
| 122 | export 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 | */ |
| 136 | export 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 | } |