956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 1 | // 搜索推荐算法相关的API接口 |
| 2 | // 对应 JWLLL 后端服务 |
| 3 | |
trm | 36915ed | 2025-06-20 09:48:51 +0000 | [diff] [blame^] | 4 | const BASE_URL = 'http://10.126.59.25:5717' |
956303669 | 80c1f27 | 2025-06-20 14:08:54 +0800 | [diff] [blame] | 5 | |
| 6 | // 通用请求函数 |
| 7 | const request = async (url, options = {}) => { |
| 8 | try { |
| 9 | const response = await fetch(url, { |
| 10 | headers: { |
| 11 | 'Content-Type': 'application/json', |
| 12 | ...options.headers |
| 13 | }, |
| 14 | ...options |
| 15 | }) |
| 16 | return await response.json() |
| 17 | } catch (error) { |
| 18 | console.error('API请求错误:', error) |
| 19 | throw error |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // 搜索API |
| 24 | export const searchAPI = { |
| 25 | // 搜索内容 |
| 26 | search: async (keyword, category = undefined) => { |
| 27 | return await request(`${BASE_URL}/search`, { |
| 28 | method: 'POST', |
| 29 | body: JSON.stringify({ keyword, category }) |
| 30 | }) |
| 31 | }, |
| 32 | |
| 33 | // 获取用户标签 |
| 34 | getUserTags: async (userId) => { |
| 35 | return await request(`${BASE_URL}/user_tags?user_id=${userId}`) |
| 36 | }, |
| 37 | |
| 38 | // 标签推荐 |
| 39 | recommendByTags: async (userId, tags) => { |
| 40 | return await request(`${BASE_URL}/recommend_tags`, { |
| 41 | method: 'POST', |
| 42 | body: JSON.stringify({ user_id: userId, tags }) |
| 43 | }) |
| 44 | }, |
| 45 | |
| 46 | // 协同过滤推荐 |
| 47 | userBasedRecommend: async (userId, topN = 20) => { |
| 48 | return await request(`${BASE_URL}/user_based_recommend`, { |
| 49 | method: 'POST', |
| 50 | body: JSON.stringify({ user_id: userId, top_n: topN }) |
| 51 | }) |
| 52 | }, |
| 53 | |
| 54 | // 获取帖子详情 |
| 55 | getPostDetail: async (postId) => { |
| 56 | return await request(`${BASE_URL}/post/${postId}`) |
| 57 | }, |
| 58 | |
| 59 | // 点赞帖子 |
| 60 | likePost: async (postId, userId) => { |
| 61 | return await request(`${BASE_URL}/like`, { |
| 62 | method: 'POST', |
| 63 | body: JSON.stringify({ post_id: postId, user_id: userId }) |
| 64 | }) |
| 65 | }, |
| 66 | |
| 67 | // 取消点赞 |
| 68 | unlikePost: async (postId, userId) => { |
| 69 | return await request(`${BASE_URL}/unlike`, { |
| 70 | method: 'POST', |
| 71 | body: JSON.stringify({ post_id: postId, user_id: userId }) |
| 72 | }) |
| 73 | }, |
| 74 | |
| 75 | // 添加评论 |
| 76 | addComment: async (postId, userId, content) => { |
| 77 | return await request(`${BASE_URL}/comment`, { |
| 78 | method: 'POST', |
| 79 | body: JSON.stringify({ post_id: postId, user_id: userId, content }) |
| 80 | }) |
| 81 | }, |
| 82 | |
| 83 | // 获取评论 |
| 84 | getComments: async (postId) => { |
| 85 | return await request(`${BASE_URL}/comments/${postId}`) |
| 86 | }, |
| 87 | |
| 88 | // 上传帖子 |
| 89 | uploadPost: async (postData) => { |
| 90 | return await request(`${BASE_URL}/upload`, { |
| 91 | method: 'POST', |
| 92 | body: JSON.stringify(postData) |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export default searchAPI |