blob: 085507b5034674b9513aca69122749c309981b6d [file] [log] [blame]
95630366980c1f272025-06-20 14:08:54 +08001// 搜索推荐算法相关的API接口
22301008ba662fe2025-06-20 18:10:20 +08002// 仅首页推荐和搜索使用 JWLLL 后端服务
3// 其他功能使用对应的后端服务
95630366980c1f272025-06-20 14:08:54 +08004
22301008ba662fe2025-06-20 18:10:20 +08005// JWLLL 后端服务 - 仅用于推荐和搜索
6const JWLLL_BASE_URL = 'http://10.126.59.25:5717'
7// WZY 后端服务 - 用于帖子详情、点赞、评论等
8const WZY_BASE_URL = 'http://10.126.59.25:5714'
95630366980c1f272025-06-20 14:08:54 +08009
10// 通用请求函数
11const request = async (url, options = {}) => {
12 try {
13 const response = await fetch(url, {
14 headers: {
15 'Content-Type': 'application/json',
16 ...options.headers
17 },
18 ...options
19 })
20 return await response.json()
21 } catch (error) {
22 console.error('API请求错误:', error)
23 throw error
24 }
25}
26
22301008ba662fe2025-06-20 18:10:20 +080027// 搜索推荐API
95630366980c1f272025-06-20 14:08:54 +080028export const searchAPI = {
22301008ba662fe2025-06-20 18:10:20 +080029 // ===== 推荐和搜索功能 - 使用 JWLLL 服务 =====
30
95630366980c1f272025-06-20 14:08:54 +080031 // 搜索内容
32 search: async (keyword, category = undefined) => {
22301008ba662fe2025-06-20 18:10:20 +080033 return await request(`${JWLLL_BASE_URL}/search`, {
95630366980c1f272025-06-20 14:08:54 +080034 method: 'POST',
35 body: JSON.stringify({ keyword, category })
36 })
37 },
38
39 // 获取用户标签
40 getUserTags: async (userId) => {
22301008ba662fe2025-06-20 18:10:20 +080041 return await request(`${JWLLL_BASE_URL}/user_tags?user_id=${userId}`)
95630366980c1f272025-06-20 14:08:54 +080042 },
43
44 // 标签推荐
45 recommendByTags: async (userId, tags) => {
22301008ba662fe2025-06-20 18:10:20 +080046 return await request(`${JWLLL_BASE_URL}/recommend_tags`, {
95630366980c1f272025-06-20 14:08:54 +080047 method: 'POST',
48 body: JSON.stringify({ user_id: userId, tags })
49 })
50 },
51
52 // 协同过滤推荐
53 userBasedRecommend: async (userId, topN = 20) => {
22301008ba662fe2025-06-20 18:10:20 +080054 return await request(`${JWLLL_BASE_URL}/user_based_recommend`, {
95630366980c1f272025-06-20 14:08:54 +080055 method: 'POST',
56 body: JSON.stringify({ user_id: userId, top_n: topN })
57 })
58 },
59
22301008ba662fe2025-06-20 18:10:20 +080060 // ===== 其他功能 - 使用 WZY 服务 =====
61
95630366980c1f272025-06-20 14:08:54 +080062 // 获取帖子详情
63 getPostDetail: async (postId) => {
22301008ba662fe2025-06-20 18:10:20 +080064 return await request(`${WZY_BASE_URL}/posts/${postId}`)
95630366980c1f272025-06-20 14:08:54 +080065 },
95630366980c1f272025-06-20 14:08:54 +080066 // 点赞帖子
67 likePost: async (postId, userId) => {
22301008ba662fe2025-06-20 18:10:20 +080068 return await request(`${WZY_BASE_URL}/posts/${postId}/like`, {
95630366980c1f272025-06-20 14:08:54 +080069 method: 'POST',
22301008ba662fe2025-06-20 18:10:20 +080070 body: JSON.stringify({ user_id: userId })
95630366980c1f272025-06-20 14:08:54 +080071 })
72 },
73
74 // 取消点赞
75 unlikePost: async (postId, userId) => {
22301008ba662fe2025-06-20 18:10:20 +080076 return await request(`${WZY_BASE_URL}/posts/${postId}/like`, {
77 method: 'DELETE',
78 body: JSON.stringify({ user_id: userId })
95630366980c1f272025-06-20 14:08:54 +080079 })
80 },
81
82 // 添加评论
83 addComment: async (postId, userId, content) => {
22301008ba662fe2025-06-20 18:10:20 +080084 return await request(`${WZY_BASE_URL}/posts/${postId}/comments`, {
95630366980c1f272025-06-20 14:08:54 +080085 method: 'POST',
22301008ba662fe2025-06-20 18:10:20 +080086 body: JSON.stringify({ user_id: userId, content })
95630366980c1f272025-06-20 14:08:54 +080087 })
22301008ba662fe2025-06-20 18:10:20 +080088 }, // 获取评论
95630366980c1f272025-06-20 14:08:54 +080089 getComments: async (postId) => {
22301008ba662fe2025-06-20 18:10:20 +080090 const comments = await request(`${WZY_BASE_URL}/posts/${postId}/comments`)
91 // 适配数据格式,WZY服务返回数组,而前端期望 {comments: [...]}
92 // 同时转换字段名以适配前端期望的格式
93 const adaptedComments = Array.isArray(comments) ? comments.map(comment => ({
94 ...comment,
95 user_name: comment.user_name || `用户${comment.user_id}`, // 如果没有用户名,使用用户ID生成
96 create_time: comment.created_at // 将 created_at 映射为 create_time
97 })) : []
98 return { comments: adaptedComments }
95630366980c1f272025-06-20 14:08:54 +080099 },
95630366980c1f272025-06-20 14:08:54 +0800100 // 上传帖子
101 uploadPost: async (postData) => {
22301008ba662fe2025-06-20 18:10:20 +0800102 // 转换数据格式以适配 WZY 服务
103 const payload = {
104 user_id: postData.user_id || 1, // 默认用户ID
105 title: postData.title,
106 content: postData.content,
107 type: postData.type || 'text', // 默认为文本类型
108 media_urls: postData.media_files || [],
109 status: 'published' // 直接发布,不需要审核
110 }
111
112 const result = await request(`${WZY_BASE_URL}/posts`, {
95630366980c1f272025-06-20 14:08:54 +0800113 method: 'POST',
22301008ba662fe2025-06-20 18:10:20 +0800114 body: JSON.stringify(payload)
95630366980c1f272025-06-20 14:08:54 +0800115 })
22301008ba662fe2025-06-20 18:10:20 +0800116
117 // 注意:WZY 服务目前不支持标签功能,tags 会被忽略
118 // 如果需要标签功能,需要额外的API调用或使用支持标签的服务
119
120 return result
95630366980c1f272025-06-20 14:08:54 +0800121 }
122}
123
124export default searchAPI