fix-profile

Change-Id: I2f01f3dcbc65b3a80d0893dbfc8ca94415ed82dc
diff --git a/src/context/UserContext.js b/src/context/UserContext.js
index 39ab6cd..a76b638 100644
--- a/src/context/UserContext.js
+++ b/src/context/UserContext.js
@@ -9,18 +9,12 @@
   useEffect(() => {
     const storedUser = localStorage.getItem('user');
     if (storedUser) {
-      try {
-        const parsedUser = JSON.parse(storedUser);
-        // 验证用户对象是否有效
-        if (parsedUser && parsedUser.userId) {
-          setUser(parsedUser);
-        } else {
-          localStorage.removeItem('user');
-        }
-      } catch (error) {
-        console.error('解析用户信息失败:', error);
-        localStorage.removeItem('user');
-      }
+      setUser(JSON.parse(storedUser));
+    } else {
+      // 设置默认用户
+      const defaultUser = { userId: 1, username: '测试用户' };
+      localStorage.setItem('user', JSON.stringify(defaultUser));
+      setUser(defaultUser);
     }
     setLoading(false);
   }, []);
diff --git a/src/pages/UserCenter/UserDynamics.css b/src/pages/UserCenter/UserDynamics.css
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserDynamics.css
diff --git a/src/pages/UserCenter/UserDynamics.jsx b/src/pages/UserCenter/UserDynamics.jsx
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserDynamics.jsx
diff --git a/src/pages/UserCenter/UserFriends.css b/src/pages/UserCenter/UserFriends.css
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserFriends.css
diff --git a/src/pages/UserCenter/UserFriends.jsx b/src/pages/UserCenter/UserFriends.jsx
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserFriends.jsx
diff --git a/src/pages/UserCenter/UserGroup.css b/src/pages/UserCenter/UserGroup.css
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserGroup.css
diff --git a/src/pages/UserCenter/UserGroup.jsx b/src/pages/UserCenter/UserGroup.jsx
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/pages/UserCenter/UserGroup.jsx
diff --git a/src/pages/UserCenter/UserProfile.jsx b/src/pages/UserCenter/UserProfile.jsx
index 6f9694c..a10657c 100644
--- a/src/pages/UserCenter/UserProfile.jsx
+++ b/src/pages/UserCenter/UserProfile.jsx
@@ -1,90 +1,108 @@
 import React, { useEffect, useState } from 'react';
 import axios from 'axios';
 import './UserProfile.css';
-import UserNav from './UserNav'; // 导入 UserNav 组件
+import UserNav from './UserNav';
 import Header from '../../components/Header';
-
-
-const API_BASE = 'http://127.0.0.1:4523/m1/6139971-5831803-default'; 
+import { useUser } from '../../context/UserContext'; 
 
 const UserProfile = () => {
+  const { user, loading } = useUser(); // 从上下文拿用户和加载状态
   const [userProfile, setUserProfile] = useState(null);
-  const [loading, setLoading] = useState(true);
   const [error, setError] = useState(null);
 
-  // 假设用户ID为1,实际应用中应通过认证信息获取
-  const userId = 1;
-
   useEffect(() => {
-    // 获取用户个人资料
+    if (loading) return; // 用户信息还没加载完,先不请求
+    console.log('用户:', user, '加载:', loading);
+
+    if (!user || !user.userId) {
+      setError('未登录或用户信息缺失');
+      setUserProfile(null);
+      return;
+    }
+
     const fetchUserProfile = async () => {
       try {
-        setLoading(true);
-        const response = await axios.get(`${API_BASE}/echo/user/profile`, {
-          params: { user_id: userId }
-        });
+        setError(null);
+        const response = await axios.get(`/echo/user/${user.userId}/getProfile`);
 
-        if (response.data.status === 'success') {
-          setUserProfile(response.data);
-        } else {
-          setError('用户不存在');
+        console.log('响应数据:', response); // 调试用
+        const raw = response.data;
+        console.log('raw:', raw); // 调试用
+
+        if (!raw) {
+          setError('用户数据为空');
+          setUserProfile(null);
+          return;
         }
+
+        const profile = {
+          avatar_url: raw.avatarUrl || 'https://example.com/default-avatar.jpg',
+          nickname: raw.username || '未知用户',
+          email: raw.email || '未填写',
+          gender: raw.gender || '保密',
+          bio: raw.description || '无',
+          interests: raw.hobbies ? raw.hobbies.split(',') : [],
+          level: raw.level || '未知',
+          experience: raw.experience ?? 0,
+          upload_amount: raw.uploadCount ?? 0,
+          download_amount: raw.downloadCount ?? 0,
+          share_rate: raw.shareRate ?? 0,
+          joined_date: raw.registrationTime,
+        };
+
+        setUserProfile(profile);
       } catch (err) {
-        setError('请求失败,请稍后再试');
-      } finally {
-        setLoading(false);
+        if (err.response?.status === 404) {
+          setError('用户不存在');
+        } else {
+          setError('请求失败,请稍后再试');
+        }
+        setUserProfile(null);
       }
     };
 
     fetchUserProfile();
-  }, [userId]);
+  }, [user, loading]);
 
-  if (loading) {
-    return <p>加载中...</p>;
-  }
-
-  if (error) {
-    return <p className="error">{error}</p>;
-  }
+  if (loading) return <p>正在加载用户信息...</p>;
+  if (error) return <p className="error">{error}</p>;
+  if (!userProfile) return null;
 
   return (
     <div className="user-profile-container">
-      <Header /> {/* 引入 Header 组件 */}
-    <div className="user-center">
-      <div className="user-nav-container">
-        <UserNav /> {/* 引入导航栏 */}
-      </div>
-      <div className="common-card">
-      <div className="right-content">
-        <div className="profile-header">
-          <img
-            src={userProfile.avatar_url}
-            alt={userProfile.nickname}
-            className="avatar"
-          />
-          <h1>{userProfile.nickname}</h1>
+      <Header />
+      <div className="user-center">
+        <div className="user-nav-container">
+          <UserNav />
         </div>
+        <div className="common-card">
+          <div className="right-content">
+            <div className="profile-header">
+              <img
+                src={userProfile.avatar_url}
+                alt={userProfile.nickname}
+                className="avatar"
+              />
+              <h1>{userProfile.nickname}</h1>
+            </div>
 
-        <div className="profile-details">
-          {/* <h2>个人资料</h2> */}
-          <p><strong>邮箱:</strong>{userProfile.email}</p>
-          <p><strong>性别:</strong>{userProfile.gender}</p>
-          <p><strong>个人简介:</strong>{userProfile.bio}</p>
-          <p><strong>兴趣:</strong>{userProfile.interests.join(', ')}</p>
-          <p><strong>等级:</strong>{userProfile.level}</p>
-          <p><strong>经验:</strong>{userProfile.experience}</p>
-          <p><strong>上传量:</strong>{userProfile.upload_amount}</p>
-          <p><strong>下载量:</strong>{userProfile.download_amount}</p>
-          <p><strong>分享率:</strong>{(userProfile.share_rate * 100).toFixed(2)}%</p>
-          <p><strong>VIP:</strong>{userProfile.is_vip ? '是' : '否'}</p>
-          {userProfile.is_vip && <p><strong>VIP等级:</strong>{userProfile.vip_type}</p>}
-          <p><strong>加入时间:</strong>{new Date(userProfile.joined_date).toLocaleDateString()}</p>
+            <div className="profile-details">
+              <p><strong>邮箱:</strong>{userProfile.email}</p>
+              <p><strong>性别:</strong>{userProfile.gender}</p>
+              <p><strong>个人简介:</strong>{userProfile.bio}</p>
+              <p><strong>兴趣:</strong>{userProfile.interests.length > 0 ? userProfile.interests.join(', ') : '无'}</p>
+              <p><strong>等级:</strong>{userProfile.level}</p>
+              <p><strong>经验:</strong>{userProfile.experience}</p>
+              <p><strong>上传量:</strong>{userProfile.upload_amount}</p>
+              <p><strong>下载量:</strong>{userProfile.download_amount}</p>
+              <p><strong>分享率:</strong>{(userProfile.share_rate * 100).toFixed(2)}%</p>
+              <p><strong>加入时间:</strong>{new Date(userProfile.joined_date).toLocaleDateString()}</p>
+            </div>
+          </div>
         </div>
       </div>
-      </div>
-      </div>
     </div>
   );
 };
 
-export default UserProfile;    
+export default UserProfile;