合并stash修改:优化用户认证和API接口配置
Change-Id: Ied7da456956b0c9e5d8db43aa22e0adba690ea65
diff --git a/Merge/front/src/api/search_jwlll.js b/Merge/front/src/api/search_jwlll.js
index 7568f1d..085507b 100644
--- a/Merge/front/src/api/search_jwlll.js
+++ b/Merge/front/src/api/search_jwlll.js
@@ -1,7 +1,11 @@
// 搜索推荐算法相关的API接口
-// 对应 JWLLL 后端服务
+// 仅首页推荐和搜索使用 JWLLL 后端服务
+// 其他功能使用对应的后端服务
-const BASE_URL = 'http://10.126.59.25:5717'
+// JWLLL 后端服务 - 仅用于推荐和搜索
+const JWLLL_BASE_URL = 'http://10.126.59.25:5717'
+// WZY 后端服务 - 用于帖子详情、点赞、评论等
+const WZY_BASE_URL = 'http://10.126.59.25:5714'
// 通用请求函数
const request = async (url, options = {}) => {
@@ -20,11 +24,13 @@
}
}
-// 搜索API
+// 搜索推荐API
export const searchAPI = {
+ // ===== 推荐和搜索功能 - 使用 JWLLL 服务 =====
+
// 搜索内容
search: async (keyword, category = undefined) => {
- return await request(`${BASE_URL}/search`, {
+ return await request(`${JWLLL_BASE_URL}/search`, {
method: 'POST',
body: JSON.stringify({ keyword, category })
})
@@ -32,12 +38,12 @@
// 获取用户标签
getUserTags: async (userId) => {
- return await request(`${BASE_URL}/user_tags?user_id=${userId}`)
+ return await request(`${JWLLL_BASE_URL}/user_tags?user_id=${userId}`)
},
// 标签推荐
recommendByTags: async (userId, tags) => {
- return await request(`${BASE_URL}/recommend_tags`, {
+ return await request(`${JWLLL_BASE_URL}/recommend_tags`, {
method: 'POST',
body: JSON.stringify({ user_id: userId, tags })
})
@@ -45,52 +51,73 @@
// 协同过滤推荐
userBasedRecommend: async (userId, topN = 20) => {
- return await request(`${BASE_URL}/user_based_recommend`, {
+ 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(`${BASE_URL}/post/${postId}`)
+ return await request(`${WZY_BASE_URL}/posts/${postId}`)
},
-
// 点赞帖子
likePost: async (postId, userId) => {
- return await request(`${BASE_URL}/like`, {
+ return await request(`${WZY_BASE_URL}/posts/${postId}/like`, {
method: 'POST',
- body: JSON.stringify({ post_id: postId, user_id: userId })
+ body: JSON.stringify({ user_id: userId })
})
},
// 取消点赞
unlikePost: async (postId, userId) => {
- return await request(`${BASE_URL}/unlike`, {
- method: 'POST',
- body: JSON.stringify({ post_id: postId, user_id: 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(`${BASE_URL}/comment`, {
+ return await request(`${WZY_BASE_URL}/posts/${postId}/comments`, {
method: 'POST',
- body: JSON.stringify({ post_id: postId, user_id: userId, content })
+ body: JSON.stringify({ user_id: userId, content })
})
- },
-
- // 获取评论
+ }, // 获取评论
getComments: async (postId) => {
- return await request(`${BASE_URL}/comments/${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) => {
- return await request(`${BASE_URL}/upload`, {
+ // 转换数据格式以适配 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(postData)
+ body: JSON.stringify(payload)
})
+
+ // 注意:WZY 服务目前不支持标签功能,tags 会被忽略
+ // 如果需要标签功能,需要额外的API调用或使用支持标签的服务
+
+ return result
}
}
diff --git a/Merge/front/src/components/HomeFeed.jsx b/Merge/front/src/components/HomeFeed.jsx
index e32a2eb..cc37642 100644
--- a/Merge/front/src/components/HomeFeed.jsx
+++ b/Merge/front/src/components/HomeFeed.jsx
@@ -5,6 +5,7 @@
import { ThumbsUp } from 'lucide-react'
import { fetchPosts, fetchPost } from '../api/posts_wzy'
import { searchAPI } from '../api/search_jwlll'
+import { getUserInfo } from '../utils/auth'
import '../style/HomeFeed.css'
const categories = [
@@ -17,11 +18,17 @@
{ label: '协同过滤推荐', value: 'cf' }
]
-const DEFAULT_USER_ID = '3' // 确保数据库有此用户
+const DEFAULT_USER_ID = '3' // 确保数据库有此用户(作为回退值)
const DEFAULT_TAGS = ['美食','影视','穿搭'] // 可根据实际数据库调整
export default function HomeFeed() {
const navigate = useNavigate()
+
+ // 获取当前用户ID,如果未登录则使用默认值
+ const getCurrentUserId = () => {
+ const userInfo = getUserInfo()
+ return userInfo?.id ? String(userInfo.id) : DEFAULT_USER_ID
+ }
const [activeCat, setActiveCat] = useState('推荐')
const [items, setItems] = useState([])
const [loading, setLoading] = useState(true)
@@ -55,13 +62,13 @@
}
setLoading(false)
}, [activeCat])
-
// 标签推荐
const fetchTagRecommend = useCallback(async (tags) => {
setLoading(true)
setError(null)
try {
- const data = await searchAPI.recommendByTags(DEFAULT_USER_ID, tags)
+ const currentUserId = getCurrentUserId()
+ const data = await searchAPI.recommendByTags(currentUserId, tags)
const formattedItems = (data.recommendations || []).map(item => ({
id: item.id,
title: item.title,
@@ -79,13 +86,13 @@
}
setLoading(false)
}, [])
-
// 协同过滤推荐
const fetchCFRecommend = useCallback(async (topN = recCFNum) => {
setLoading(true)
setError(null)
try {
- const data = await searchAPI.userBasedRecommend(DEFAULT_USER_ID, topN)
+ const currentUserId = getCurrentUserId()
+ const data = await searchAPI.userBasedRecommend(currentUserId, topN)
const formattedItems = (data.recommendations || []).map(item => ({
id: item.id,
title: item.title,
@@ -103,14 +110,14 @@
}
setLoading(false)
}, [recCFNum])
-
// 获取用户兴趣标签后再推荐
const fetchUserTagsAndRecommend = useCallback(async () => {
setLoading(true)
setError(null)
let tags = []
try {
- const data = await searchAPI.getUserTags(DEFAULT_USER_ID)
+ const currentUserId = getCurrentUserId()
+ const data = await searchAPI.getUserTags(currentUserId)
tags = Array.isArray(data.tags) && data.tags.length > 0 ? data.tags : DEFAULT_TAGS
} catch {
tags = DEFAULT_TAGS
diff --git a/Merge/front/src/components/PostDetailJWLLL.jsx b/Merge/front/src/components/PostDetailJWLLL.jsx
index 0dc7289..009ba6c 100644
--- a/Merge/front/src/components/PostDetailJWLLL.jsx
+++ b/Merge/front/src/components/PostDetailJWLLL.jsx
@@ -1,7 +1,8 @@
-import React, { useState, useEffect } from 'react'
+import React, { useState, useEffect, useCallback } from 'react'
import { useParams, useNavigate } from 'react-router-dom'
import { ArrowLeft, ThumbsUp, MessageCircle, Share2, BookmarkPlus, Heart, Eye } from 'lucide-react'
import { searchAPI } from '../api/search_jwlll'
+import { getUserInfo } from '../utils/auth'
import '../style/PostDetail.css'
export default function PostDetail() {
@@ -16,15 +17,13 @@
const [comments, setComments] = useState([])
const [newComment, setNewComment] = useState('')
const [showComments, setShowComments] = useState(false)
+ // 获取当前用户ID
+ const getCurrentUserId = () => {
+ const userInfo = getUserInfo()
+ return userInfo?.id || '3' // 如果未登录或无用户信息,使用默认值3
+ }
- const DEFAULT_USER_ID = '3' // 默认用户ID
-
- useEffect(() => {
- fetchPostDetail()
- fetchComments()
- }, [id])
-
- const fetchPostDetail = async () => {
+ const fetchPostDetail = useCallback(async () => {
setLoading(true)
setError(null)
try {
@@ -37,28 +36,33 @@
} finally {
setLoading(false)
}
- }
+ }, [id])
- const fetchComments = async () => {
+ const fetchComments = useCallback(async () => {
try {
const data = await searchAPI.getComments(id)
setComments(data.comments || [])
} catch (error) {
console.error('获取评论失败:', error)
}
- }
+ }, [id])
+
+ useEffect(() => {
+ fetchPostDetail()
+ fetchComments()
+ }, [fetchPostDetail, fetchComments])
const handleBack = () => {
navigate(-1)
}
-
const handleLike = async () => {
try {
+ const currentUserId = getCurrentUserId()
const newLiked = !liked
if (newLiked) {
- await searchAPI.likePost(id, DEFAULT_USER_ID)
+ await searchAPI.likePost(id, currentUserId)
} else {
- await searchAPI.unlikePost(id, DEFAULT_USER_ID)
+ await searchAPI.unlikePost(id, currentUserId)
}
setLiked(newLiked)
setLikeCount(prev => newLiked ? prev + 1 : prev - 1)
@@ -89,13 +93,13 @@
alert('链接已复制到剪贴板')
}
}
-
const handleAddComment = async (e) => {
e.preventDefault()
if (!newComment.trim()) return
try {
- await searchAPI.addComment(id, DEFAULT_USER_ID, newComment)
+ const currentUserId = getCurrentUserId()
+ await searchAPI.addComment(id, currentUserId, newComment)
setNewComment('')
fetchComments() // 刷新评论列表
} catch (error) {
diff --git a/Merge/front/src/components/UploadPageJWLLL.jsx b/Merge/front/src/components/UploadPageJWLLL.jsx
index 2d9ee7d..475fe2f 100644
--- a/Merge/front/src/components/UploadPageJWLLL.jsx
+++ b/Merge/front/src/components/UploadPageJWLLL.jsx
@@ -1,6 +1,7 @@
import React, { useState } from 'react'
import { Image, Video, Send } from 'lucide-react'
import { searchAPI } from '../api/search_jwlll'
+import { getUserInfo } from '../utils/auth'
import '../style/UploadPage.css'
const categories = [
@@ -14,15 +15,18 @@
const [isUploading, setIsUploading] = useState(false)
const [uploadedFiles, setUploadedFiles] = useState([])
const [uploadProgress, setUploadProgress] = useState(0)
-
- // 新增表单字段
+ // 新增表单字段
const [title, setTitle] = useState('')
const [content, setContent] = useState('')
const [tags, setTags] = useState('')
const [category, setCategory] = useState(categories[0])
const [isPublishing, setIsPublishing] = useState(false)
- const DEFAULT_USER_ID = '3' // 默认用户ID
+ // 获取当前用户ID
+ const getCurrentUserId = () => {
+ const userInfo = getUserInfo()
+ return userInfo?.id || 3 // 如果未登录或无用户信息,使用默认值3(注意这里是数字类型)
+ }
const validateFiles = files => {
const imgTypes = ['image/jpeg','image/jpg','image/png','image/webp']
@@ -91,12 +95,11 @@
if (!content.trim()) {
alert('请输入内容')
return
- }
-
- setIsPublishing(true)
+ } setIsPublishing(true)
try {
+ const currentUserId = getCurrentUserId()
const postData = {
- user_id: DEFAULT_USER_ID,
+ user_id: currentUserId,
title: title.trim(),
content: content.trim(),
tags: tags.split(',').map(t => t.trim()).filter(t => t),