补充
Change-Id: I058b37511bf75593587009fabaebaa7b11ad7aed
diff --git a/Merge/front/src/components/HomeFeed.jsx b/Merge/front/src/components/HomeFeed.jsx
index cc37642..1fb12a5 100644
--- a/Merge/front/src/components/HomeFeed.jsx
+++ b/Merge/front/src/components/HomeFeed.jsx
@@ -6,6 +6,7 @@
import { fetchPosts, fetchPost } from '../api/posts_wzy'
import { searchAPI } from '../api/search_jwlll'
import { getUserInfo } from '../utils/auth'
+import { deepRecommend } from '../api/recommend_rhj'
import '../style/HomeFeed.css'
const categories = [
@@ -15,7 +16,8 @@
const recommendModes = [
{ label: '标签推荐', value: 'tag' },
- { label: '协同过滤推荐', value: 'cf' }
+ { label: '协同过滤推荐', value: 'cf' },
+ { label: '深度推荐', value: 'deep' } // 新增
]
const DEFAULT_USER_ID = '3' // 确保数据库有此用户(作为回退值)
@@ -69,16 +71,35 @@
try {
const currentUserId = getCurrentUserId()
const data = await searchAPI.recommendByTags(currentUserId, tags)
- const formattedItems = (data.recommendations || []).map(item => ({
- id: item.id,
- title: item.title,
- author: item.author || '佚名',
- avatar: `https://i.pravatar.cc/40?img=${item.id}`,
- img: item.img || '',
- likes: item.heat || 0,
- content: item.content
- }))
- setItems(formattedItems)
+ // 新增:拉取详情,保证和原始数据一致
+ const detailed = await Promise.all(
+ (data.recommendations || []).map(async item => {
+ try {
+ const d = await fetchPost(item.id)
+ return {
+ id: d.id,
+ title: d.title,
+ author: `作者 ${d.user_id}`,
+ avatar: `https://i.pravatar.cc/40?img=${d.user_id}`,
+ img: d.media_urls?.[0] || '',
+ likes: d.heat,
+ content: d.content || ''
+ }
+ } catch {
+ // 拉详情失败时兜底
+ return {
+ id: item.id,
+ title: item.title,
+ author: item.author || '佚名',
+ avatar: `https://i.pravatar.cc/40?img=${item.id}`,
+ img: item.img || '',
+ likes: item.heat || 0,
+ content: item.content || ''
+ }
+ }
+ })
+ )
+ setItems(detailed)
} catch (e) {
console.error('标签推荐失败:', e)
setError('标签推荐失败')
@@ -93,16 +114,35 @@
try {
const currentUserId = getCurrentUserId()
const data = await searchAPI.userBasedRecommend(currentUserId, topN)
- const formattedItems = (data.recommendations || []).map(item => ({
- id: item.id,
- title: item.title,
- author: item.author || '佚名',
- avatar: `https://i.pravatar.cc/40?img=${item.id}`,
- img: item.img || '',
- likes: item.heat || 0,
- content: item.content
- }))
- setItems(formattedItems)
+ // 新增:拉取详情,保证和原始数据一致
+ const detailed = await Promise.all(
+ (data.recommendations || []).map(async item => {
+ try {
+ const d = await fetchPost(item.id)
+ return {
+ id: d.id,
+ title: d.title,
+ author: `作者 ${d.user_id}`,
+ avatar: `https://i.pravatar.cc/40?img=${d.user_id}`,
+ img: d.media_urls?.[0] || '',
+ likes: d.heat,
+ content: d.content || ''
+ }
+ } catch {
+ // 拉详情失败时兜底
+ return {
+ id: item.id,
+ title: item.title,
+ author: item.author || '佚名',
+ avatar: `https://i.pravatar.cc/40?img=${item.id}`,
+ img: item.img || '',
+ likes: item.heat || 0,
+ content: item.content || ''
+ }
+ }
+ })
+ )
+ setItems(detailed)
} catch (e) {
console.error('协同过滤推荐失败:', e)
setError('协同过滤推荐失败')
@@ -110,6 +150,47 @@
}
setLoading(false)
}, [recCFNum])
+ // 深度推荐
+ const fetchDeepRecommend = useCallback(async (topN = 20) => {
+ setLoading(true)
+ setError(null)
+ try {
+ const currentUserId = getCurrentUserId()
+ const recs = await deepRecommend(currentUserId, topN)
+ // 拉取详情,保证和原始数据一致
+ const detailed = await Promise.all(
+ (recs || []).map(async item => {
+ try {
+ const d = await fetchPost(item.id)
+ return {
+ id: d.id,
+ title: d.title,
+ author: `作者 ${d.user_id}`,
+ avatar: `https://i.pravatar.cc/40?img=${d.user_id}`,
+ img: d.media_urls?.[0] || '',
+ likes: d.heat,
+ content: d.content || ''
+ }
+ } catch {
+ return {
+ id: item.id,
+ title: item.title,
+ author: item.author || '佚名',
+ avatar: `https://i.pravatar.cc/40?img=${item.id}`,
+ img: item.img || '',
+ likes: item.heat || 0,
+ content: item.content || ''
+ }
+ }
+ })
+ )
+ setItems(detailed)
+ } catch (e) {
+ setError('深度推荐失败')
+ setItems([])
+ }
+ setLoading(false)
+ }, [])
// 获取用户兴趣标签后再推荐
const fetchUserTagsAndRecommend = useCallback(async () => {
setLoading(true)
@@ -124,11 +205,13 @@
}
if (recMode === 'tag') {
await fetchTagRecommend(tags)
- } else {
+ } else if (recMode === 'cf') {
await fetchCFRecommend()
+ } else if (recMode === 'deep') {
+ await fetchDeepRecommend()
}
setLoading(false)
- }, [recMode, fetchTagRecommend, fetchCFRecommend])
+ }, [recMode, fetchTagRecommend, fetchCFRecommend, fetchDeepRecommend])
useEffect(() => {
// 原始数据加载函数