| // 帖子相关API接口 |
| // 整合不同后端服务的帖子功能 |
| |
| // WZY 后端服务 - 帖子CRUD、点赞、评论 |
| const WZY_BASE_URL = 'http://192.168.5.235:5714' |
| // LJC 后端服务 - 用户相关功能 |
| const LJC_BASE_URL = 'http://10.126.59.25:5715' |
| |
| // 通用请求函数 |
| const request = async (url, options = {}) => { |
| try { |
| const response = await fetch(url, { |
| headers: { |
| 'Content-Type': 'application/json', |
| ...options.headers |
| }, |
| ...options |
| }) |
| return await response.json() |
| } catch (error) { |
| console.error('API请求错误:', error) |
| throw error |
| } |
| } |
| |
| // 帖子相关API |
| export const postsAPI = { |
| // ===== 帖子基础功能 - 使用 WZY 服务 ===== |
| |
| // 获取帖子列表 |
| fetchPosts: async (userId = null) => { |
| const url = userId ? `${WZY_BASE_URL}/posts?user_id=${userId}` : `${WZY_BASE_URL}/posts` |
| return await request(url) |
| }, |
| |
| // 获取帖子详情 |
| fetchPost: async (postId) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}`) |
| }, |
| |
| // 创建帖子 |
| createPost: async (postData) => { |
| return await request(`${WZY_BASE_URL}/posts`, { |
| method: 'POST', |
| body: JSON.stringify(postData) |
| }) |
| }, |
| |
| // 更新帖子 |
| updatePost: async (postId, postData) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}`, { |
| method: 'PUT', |
| body: JSON.stringify(postData) |
| }) |
| }, |
| |
| // 删除帖子 |
| deletePost: async (postId) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}`, { |
| method: 'DELETE' |
| }) |
| }, |
| |
| // ===== 互动功能 - 使用 WZY 服务 ===== |
| |
| // 点赞帖子 |
| likePost: async (postId, userId) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}/like`, { |
| method: 'POST', |
| body: JSON.stringify({ user_id: userId }) |
| }) |
| }, |
| |
| // 取消点赞 |
| unlikePost: async (postId, userId) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}/like`, { |
| method: 'DELETE', |
| body: JSON.stringify({ user_id: userId }) |
| }) |
| }, |
| |
| // 添加评论 |
| addComment: async (postId, userId, content, parentId = null) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}/comments`, { |
| method: 'POST', |
| body: JSON.stringify({ |
| user_id: userId, |
| content, |
| parent_id: parentId |
| }) |
| }) |
| }, |
| |
| // 获取评论列表 |
| fetchComments: async (postId) => { |
| return await request(`${WZY_BASE_URL}/posts/${postId}/comments`) |
| }, |
| |
| // ===== 用户相关功能 - 使用 LJC 服务 ===== |
| |
| // 获取用户信息 |
| getCurrentUser: async () => { |
| return await request(`${LJC_BASE_URL}/api/current-user`) |
| }, |
| |
| // 获取用户详情 |
| getUser: async (userId) => { |
| return await request(`${LJC_BASE_URL}/api/user/${userId}`) |
| }, |
| |
| // 获取用户帖子 |
| getUserPosts: async (userId) => { |
| return await request(`${LJC_BASE_URL}/api/user/${userId}/posts`) |
| }, |
| |
| // 关注用户 |
| followUser: async (followeeId) => { |
| return await request(`${LJC_BASE_URL}/api/follow/${followeeId}`, { |
| method: 'POST' |
| }) |
| }, |
| |
| // 取消关注 |
| unfollowUser: async (followeeId) => { |
| return await request(`${LJC_BASE_URL}/api/follow/${followeeId}`, { |
| method: 'DELETE' |
| }) |
| }, |
| |
| // 获取收藏列表 |
| getFavorites: async (userId) => { |
| return await request(`${LJC_BASE_URL}/api/user/${userId}/favorites`) |
| } |
| } |
| |
| export default postsAPI |