22301008 | ce7cee8 | 2025-06-20 18:08:36 +0800 | [diff] [blame] | 1 | // 帖子相关API接口 |
| 2 | // 整合不同后端服务的帖子功能 |
| 3 | |
| 4 | // WZY 后端服务 - 帖子CRUD、点赞、评论 |
22301008 | e25b4b0 | 2025-06-20 22:15:31 +0800 | [diff] [blame] | 5 | const WZY_BASE_URL = 'http://192.168.5.235:5714' |
22301008 | ce7cee8 | 2025-06-20 18:08:36 +0800 | [diff] [blame] | 6 | // LJC 后端服务 - 用户相关功能 |
| 7 | const LJC_BASE_URL = 'http://10.126.59.25:5715' |
| 8 | |
| 9 | // 通用请求函数 |
| 10 | const request = async (url, options = {}) => { |
| 11 | try { |
| 12 | const response = await fetch(url, { |
| 13 | headers: { |
| 14 | 'Content-Type': 'application/json', |
| 15 | ...options.headers |
| 16 | }, |
| 17 | ...options |
| 18 | }) |
| 19 | return await response.json() |
| 20 | } catch (error) { |
| 21 | console.error('API请求错误:', error) |
| 22 | throw error |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // 帖子相关API |
| 27 | export const postsAPI = { |
| 28 | // ===== 帖子基础功能 - 使用 WZY 服务 ===== |
| 29 | |
| 30 | // 获取帖子列表 |
| 31 | fetchPosts: async (userId = null) => { |
| 32 | const url = userId ? `${WZY_BASE_URL}/posts?user_id=${userId}` : `${WZY_BASE_URL}/posts` |
| 33 | return await request(url) |
| 34 | }, |
| 35 | |
| 36 | // 获取帖子详情 |
| 37 | fetchPost: async (postId) => { |
| 38 | return await request(`${WZY_BASE_URL}/posts/${postId}`) |
| 39 | }, |
| 40 | |
| 41 | // 创建帖子 |
| 42 | createPost: async (postData) => { |
| 43 | return await request(`${WZY_BASE_URL}/posts`, { |
| 44 | method: 'POST', |
| 45 | body: JSON.stringify(postData) |
| 46 | }) |
| 47 | }, |
| 48 | |
| 49 | // 更新帖子 |
| 50 | updatePost: async (postId, postData) => { |
| 51 | return await request(`${WZY_BASE_URL}/posts/${postId}`, { |
| 52 | method: 'PUT', |
| 53 | body: JSON.stringify(postData) |
| 54 | }) |
| 55 | }, |
| 56 | |
| 57 | // 删除帖子 |
| 58 | deletePost: async (postId) => { |
| 59 | return await request(`${WZY_BASE_URL}/posts/${postId}`, { |
| 60 | method: 'DELETE' |
| 61 | }) |
| 62 | }, |
| 63 | |
| 64 | // ===== 互动功能 - 使用 WZY 服务 ===== |
| 65 | |
| 66 | // 点赞帖子 |
| 67 | likePost: async (postId, userId) => { |
| 68 | return await request(`${WZY_BASE_URL}/posts/${postId}/like`, { |
| 69 | method: 'POST', |
| 70 | body: JSON.stringify({ user_id: userId }) |
| 71 | }) |
| 72 | }, |
| 73 | |
| 74 | // 取消点赞 |
| 75 | unlikePost: async (postId, userId) => { |
| 76 | return await request(`${WZY_BASE_URL}/posts/${postId}/like`, { |
| 77 | method: 'DELETE', |
| 78 | body: JSON.stringify({ user_id: userId }) |
| 79 | }) |
| 80 | }, |
| 81 | |
| 82 | // 添加评论 |
| 83 | addComment: async (postId, userId, content, parentId = null) => { |
| 84 | return await request(`${WZY_BASE_URL}/posts/${postId}/comments`, { |
| 85 | method: 'POST', |
| 86 | body: JSON.stringify({ |
| 87 | user_id: userId, |
| 88 | content, |
| 89 | parent_id: parentId |
| 90 | }) |
| 91 | }) |
| 92 | }, |
| 93 | |
| 94 | // 获取评论列表 |
| 95 | fetchComments: async (postId) => { |
| 96 | return await request(`${WZY_BASE_URL}/posts/${postId}/comments`) |
| 97 | }, |
| 98 | |
| 99 | // ===== 用户相关功能 - 使用 LJC 服务 ===== |
| 100 | |
| 101 | // 获取用户信息 |
| 102 | getCurrentUser: async () => { |
| 103 | return await request(`${LJC_BASE_URL}/api/current-user`) |
| 104 | }, |
| 105 | |
| 106 | // 获取用户详情 |
| 107 | getUser: async (userId) => { |
| 108 | return await request(`${LJC_BASE_URL}/api/user/${userId}`) |
| 109 | }, |
| 110 | |
| 111 | // 获取用户帖子 |
| 112 | getUserPosts: async (userId) => { |
| 113 | return await request(`${LJC_BASE_URL}/api/user/${userId}/posts`) |
| 114 | }, |
| 115 | |
| 116 | // 关注用户 |
| 117 | followUser: async (followeeId) => { |
| 118 | return await request(`${LJC_BASE_URL}/api/follow/${followeeId}`, { |
| 119 | method: 'POST' |
| 120 | }) |
| 121 | }, |
| 122 | |
| 123 | // 取消关注 |
| 124 | unfollowUser: async (followeeId) => { |
| 125 | return await request(`${LJC_BASE_URL}/api/follow/${followeeId}`, { |
| 126 | method: 'DELETE' |
| 127 | }) |
| 128 | }, |
| 129 | |
| 130 | // 获取收藏列表 |
| 131 | getFavorites: async (userId) => { |
| 132 | return await request(`${LJC_BASE_URL}/api/user/${userId}/favorites`) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | export default postsAPI |