合并stash修改:优化用户认证和API接口配置

Change-Id: Ied7da456956b0c9e5d8db43aa22e0adba690ea65
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),