TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 1 | // src/api/posts.js |
wu | 90da17b | 2025-06-19 12:45:29 +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 | /** |
| 5 | * 获取所有已发布的帖子列表 |
| 6 | * GET /posts |
| 7 | */ |
| 8 | export async function fetchPosts() { |
| 9 | const res = await fetch(`${BASE}/posts`) |
| 10 | if (!res.ok) throw new Error(`fetchPosts: ${res.status}`) |
| 11 | console.log('fetchPosts response:', res) // debug: inspect response |
| 12 | return res.json() // 返回 [ { id, title, heat, created_at }, … ] |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * 查看单个帖子详情 |
| 17 | * GET /posts/{postId} |
| 18 | */ |
| 19 | export async function fetchPost(postId) { |
| 20 | const res = await fetch(`${BASE}/posts/${postId}`) |
| 21 | if (!res.ok) throw new Error(`fetchPost(${postId}): ${res.status}`) |
| 22 | return res.json() // 返回完整的帖子对象 |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * 发布新帖 |
| 27 | * POST /posts |
| 28 | */ |
| 29 | export async function createPost(payload) { |
| 30 | const res = await fetch(`${BASE}/posts`, { |
| 31 | method: 'POST', |
| 32 | headers: { 'Content-Type': 'application/json' }, |
| 33 | body: JSON.stringify(payload) |
| 34 | }) |
| 35 | if (!res.ok) { |
| 36 | const err = await res.json().catch(() => null) |
| 37 | throw new Error(err?.error || `createPost: ${res.status}`) |
| 38 | } |
| 39 | return res.json() // { id } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * 修改帖子 |
| 44 | * PUT /posts/{postId} |
| 45 | */ |
| 46 | export async function updatePost(postId, payload) { |
| 47 | const res = await fetch(`${BASE}/posts/${postId}`, { |
| 48 | method: 'PUT', |
| 49 | headers: { 'Content-Type': 'application/json' }, |
| 50 | body: JSON.stringify(payload) |
| 51 | }) |
| 52 | if (!res.ok) throw new Error(`updatePost(${postId}): ${res.status}`) |
| 53 | // 204 No Content |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * 删除帖子 |
| 58 | * DELETE /posts/{postId} |
| 59 | */ |
| 60 | export async function deletePost(postId) { |
| 61 | const res = await fetch(`${BASE}/posts/${postId}`, { |
| 62 | method: 'DELETE' |
| 63 | }) |
| 64 | if (!res.ok) throw new Error(`deletePost(${postId}): ${res.status}`) |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * 点赞 |
| 69 | * POST /posts/{postId}/like |
| 70 | */ |
| 71 | export async function likePost(postId, userId) { |
| 72 | const res = await fetch(`${BASE}/posts/${postId}/like`, { |
| 73 | method: 'POST', |
| 74 | headers: { 'Content-Type': 'application/json' }, |
| 75 | body: JSON.stringify({ user_id: userId }) |
| 76 | }) |
| 77 | if (!res.ok) { |
| 78 | const err = await res.json().catch(() => null) |
| 79 | throw new Error(err?.error || `likePost: ${res.status}`) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * 取消点赞 |
| 85 | * DELETE /posts/{postId}/like |
| 86 | */ |
| 87 | export async function unlikePost(postId, userId) { |
| 88 | const res = await fetch(`${BASE}/posts/${postId}/like`, { |
| 89 | method: 'DELETE', |
| 90 | headers: { 'Content-Type': 'application/json' }, |
| 91 | body: JSON.stringify({ user_id: userId }) |
| 92 | }) |
| 93 | if (!res.ok) { |
| 94 | const err = await res.json().catch(() => null) |
| 95 | throw new Error(err?.error || `unlikePost: ${res.status}`) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * 收藏、取消收藏、浏览、分享 等接口: |
| 101 | * POST /posts/{postId}/favorite |
| 102 | * DELETE /posts/{postId}/favorite |
| 103 | * POST /posts/{postId}/view |
| 104 | * POST /posts/{postId}/share |
| 105 | * 用法同上,替换路径即可 |
| 106 | */ |
| 107 | |
| 108 | /** |
| 109 | * 添加评论 |
| 110 | * POST /posts/{postId}/comments |
| 111 | */ |
| 112 | export async function addComment(postId, payload) { |
| 113 | const res = await fetch(`${BASE}/posts/${postId}/comments`, { |
| 114 | method: 'POST', |
| 115 | headers: { 'Content-Type': 'application/json' }, |
| 116 | body: JSON.stringify(payload) |
| 117 | }) |
| 118 | if (!res.ok) throw new Error(`addComment: ${res.status}`) |
| 119 | return res.json() // { id } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * 获取评论列表 |
| 124 | * GET /posts/{postId}/comments |
| 125 | */ |
| 126 | export async function fetchComments(postId) { |
| 127 | const res = await fetch(`${BASE}/posts/${postId}/comments`) |
| 128 | if (!res.ok) throw new Error(`fetchComments: ${res.status}`) |
| 129 | return res.json() |
| 130 | } |