blob: 7568f1dc5e3122e52319e4250f2a8842904fb9ec [file] [log] [blame]
// 搜索推荐算法相关的API接口
// 对应 JWLLL 后端服务
const BASE_URL = 'http://10.126.59.25:5717'
// 通用请求函数
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 = {
// 搜索内容
search: async (keyword, category = undefined) => {
return await request(`${BASE_URL}/search`, {
method: 'POST',
body: JSON.stringify({ keyword, category })
})
},
// 获取用户标签
getUserTags: async (userId) => {
return await request(`${BASE_URL}/user_tags?user_id=${userId}`)
},
// 标签推荐
recommendByTags: async (userId, tags) => {
return await request(`${BASE_URL}/recommend_tags`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, tags })
})
},
// 协同过滤推荐
userBasedRecommend: async (userId, topN = 20) => {
return await request(`${BASE_URL}/user_based_recommend`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, top_n: topN })
})
},
// 获取帖子详情
getPostDetail: async (postId) => {
return await request(`${BASE_URL}/post/${postId}`)
},
// 点赞帖子
likePost: async (postId, userId) => {
return await request(`${BASE_URL}/like`, {
method: 'POST',
body: JSON.stringify({ post_id: postId, user_id: userId })
})
},
// 取消点赞
unlikePost: async (postId, userId) => {
return await request(`${BASE_URL}/unlike`, {
method: 'POST',
body: JSON.stringify({ post_id: postId, user_id: userId })
})
},
// 添加评论
addComment: async (postId, userId, content) => {
return await request(`${BASE_URL}/comment`, {
method: 'POST',
body: JSON.stringify({ post_id: postId, user_id: userId, content })
})
},
// 获取评论
getComments: async (postId) => {
return await request(`${BASE_URL}/comments/${postId}`)
},
// 上传帖子
uploadPost: async (postData) => {
return await request(`${BASE_URL}/upload`, {
method: 'POST',
body: JSON.stringify(postData)
})
}
}
export default searchAPI