blob: 01b743ff47c3add899bd6230d400c9ea2f1c2770 [file] [log] [blame]
// 搜索推荐算法相关的API接口
// 仅首页推荐和搜索使用 JWLLL 后端服务
// 其他功能使用对应的后端服务
// JWLLL 后端服务 - 仅用于推荐和搜索
const JWLLL_BASE_URL = 'http://10.126.59.25:5717'
// WZY 后端服务 - 用于帖子详情、点赞、评论等
const WZY_BASE_URL = 'http://192.168.5.235:5714'
// 通用请求函数
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 searchAPI = {
// ===== 推荐和搜索功能 - 使用 JWLLL 服务 =====
// 搜索内容
search: async (keyword, category = undefined) => {
return await request(`${JWLLL_BASE_URL}/search`, {
method: 'POST',
body: JSON.stringify({ keyword, category })
})
},
// 获取用户标签
getUserTags: async (userId) => {
return await request(`${JWLLL_BASE_URL}/user_tags?user_id=${userId}`)
},
// 标签推荐
recommendByTags: async (userId, tags) => {
return await request(`${JWLLL_BASE_URL}/recommend_tags`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, tags })
})
},
// 协同过滤推荐
userBasedRecommend: async (userId, topN = 20) => {
return await request(`${JWLLL_BASE_URL}/user_based_recommend`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, top_n: topN })
})
},
// ===== 其他功能 - 使用 WZY 服务 =====
// 获取帖子详情
getPostDetail: async (postId) => {
return await request(`${WZY_BASE_URL}/posts/${postId}`)
},
// 点赞帖子
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) => {
return await request(`${WZY_BASE_URL}/posts/${postId}/comments`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, content })
})
}, // 获取评论
getComments: async (postId) => {
const comments = await request(`${WZY_BASE_URL}/posts/${postId}/comments`)
// 适配数据格式,WZY服务返回数组,而前端期望 {comments: [...]}
// 同时转换字段名以适配前端期望的格式
const adaptedComments = Array.isArray(comments) ? comments.map(comment => ({
...comment,
user_name: comment.user_name || `用户${comment.user_id}`, // 如果没有用户名,使用用户ID生成
create_time: comment.created_at // 将 created_at 映射为 create_time
})) : []
return { comments: adaptedComments }
},
// 上传帖子
uploadPost: async (postData) => {
// 转换数据格式以适配 WZY 服务
const payload = {
user_id: postData.user_id || 1, // 默认用户ID
title: postData.title,
content: postData.content,
type: postData.type || 'text', // 默认为文本类型
media_urls: postData.media_files || [],
status: 'published' // 直接发布,不需要审核
}
const result = await request(`${WZY_BASE_URL}/posts`, {
method: 'POST',
body: JSON.stringify(payload)
})
// 注意:WZY 服务目前不支持标签功能,tags 会被忽略
// 如果需要标签功能,需要额外的API调用或使用支持标签的服务
return result
}
}
export default searchAPI