修改密码、管理员删帖、促销、退出登录

Change-Id: I2cc0e211ac5a04f9e89d0736fadd25541a5fccb9
diff --git a/src/pages/UserCenter/UserProfile.jsx b/src/pages/UserCenter/UserProfile.jsx
index 9170652..c6e80e0 100644
--- a/src/pages/UserCenter/UserProfile.jsx
+++ b/src/pages/UserCenter/UserProfile.jsx
@@ -2,13 +2,25 @@
 import axios from 'axios';
 import './UserProfile.css';
 import { useUser } from '../../context/UserContext';
+import { useLocation } from 'wouter';
+
 const DEFAULT_AVATAR_URL = `${process.env.PUBLIC_URL}/default-avatar.png`;
 
 const UserProfile = () => {
-  const { user, loading } = useUser();
+  const { user, loading, logout } = useUser();
   const [userProfile, setUserProfile] = useState(null);
+  const [experienceInfo, setExperienceInfo] = useState(null);
   const [error, setError] = useState(null);
 
+  // 修改密码状态
+  const [showPwdModal, setShowPwdModal] = useState(false);
+  const [oldPassword, setOldPassword] = useState('');
+  const [newPassword, setNewPassword] = useState('');
+  const [confirmPassword, setConfirmPassword] = useState('');
+
+  // 退出登录
+  const [, setLocation] = useLocation();
+
   useEffect(() => {
     if (loading) return;
     if (!user || !user.userId) {
@@ -21,7 +33,6 @@
       try {
         setError(null);
         const { data: raw } = await axios.get(`/echo/user/${user.userId}/getProfile`);
-
         if (!raw) {
           setError('用户数据为空');
           setUserProfile(null);
@@ -29,9 +40,9 @@
         }
 
         const profile = {
-        avatarUrl: raw.avatarUrl
-          ? `${process.env.REACT_APP_AVATAR_BASE_URL}${raw.avatarUrl}`
-          : DEFAULT_AVATAR_URL,
+          avatarUrl: raw.avatarUrl
+            ? `${process.env.REACT_APP_AVATAR_BASE_URL}${raw.avatarUrl}`
+            : DEFAULT_AVATAR_URL,
           nickname: raw.username || '未知用户',
           email: raw.email || '未填写',
           gender: raw.gender || '保密',
@@ -52,7 +63,19 @@
       }
     };
 
+    const fetchExperienceInfo = async () => {
+      try {
+        const { data } = await axios.get('/echo/level/getExperience', {
+          params: { user_id: user.userId },
+        });
+        setExperienceInfo(data);
+      } catch (err) {
+        console.error('经验信息获取失败:', err);
+      }
+    };
+
     fetchUserProfile();
+    fetchExperienceInfo();
   }, [user, loading]);
 
   const handleAvatarUpload = async (e) => {
@@ -63,11 +86,11 @@
     formData.append('file', file);
 
     try {
-        const { data } = await axios.post(
-          `/echo/user/${user.userId}/uploadAvatar`,
-          formData,
-          { headers: { 'Content-Type': 'multipart/form-data' } }
-        );
+      const { data } = await axios.post(
+        `/echo/user/${user.userId}/uploadAvatar`,
+        formData,
+        { headers: { 'Content-Type': 'multipart/form-data' } }
+      );
 
       if (data?.avatarUrl) {
         setUserProfile((prev) => ({
@@ -84,6 +107,42 @@
     }
   };
 
+  const handleLogout = () => {
+    logout();
+    setLocation('/auth'); // 退出后跳转登录页
+    // window.location.reload(); // 或跳转登录页
+  };
+
+  const handleChangePassword = async () => {
+    if (!oldPassword || !newPassword || !confirmPassword) {
+      alert('请填写所有字段');
+      return;
+    }
+    if (newPassword !== confirmPassword) {
+      alert('两次输入的新密码不一致');
+      return;
+    }
+
+    try {
+      // await axios.post('/echo/user/password', {
+      //   user_id: user.userId,
+      //   oldPassword,
+      //   newPassword,
+      // });
+      await axios.post('/echo/user/password', {
+        user_id: user.userId,
+        old_password: oldPassword,
+        new_password: newPassword,
+        confirm_password: confirmPassword,
+      });
+      alert('密码修改成功,请重新登录');
+      logout();
+      window.location.reload();
+    } catch (err) {
+      alert(err.response?.data?.message || '密码修改失败,请检查原密码是否正确');
+    }
+  };
+
   if (loading) return <p>正在加载用户信息...</p>;
   if (error) return <p className="error">{error}</p>;
   if (!userProfile) return null;
@@ -103,6 +162,19 @@
     joinedDate,
   } = userProfile;
 
+  const progressPercent = experienceInfo
+    ? Math.min(
+        100,
+        ((experienceInfo.current_experience || 0) /
+          (experienceInfo.next_level_experience || 1)) *
+          100
+      ).toFixed(2)
+    : 0;
+
+  const expToNextLevel = experienceInfo
+    ? (experienceInfo.next_level_experience - experienceInfo.current_experience)
+    : null;
+
   return (
     <div className="common-card">
       <div className="right-content">
@@ -134,10 +206,58 @@
           <p><strong>下载量:</strong>{downloadAmount}</p>
           <p><strong>分享率:</strong>{(shareRate * 100).toFixed(2)}%</p>
           <p><strong>加入时间:</strong>{new Date(joinedDate).toLocaleDateString()}</p>
+
+          {experienceInfo && (
+            <>
+              <p><strong>距离下一等级还需:</strong>{expToNextLevel} 经验值</p>
+              <div className="exp-bar-wrapper">
+                <div className="exp-bar" style={{ width: `${progressPercent}%` }} />
+              </div>
+              <p className="exp-progress-text">{progressPercent}%</p>
+            </>
+          )}
+
+          {/* 修改密码与退出登录按钮 */}
+          <div className="profile-actions">
+            <button onClick={() => setShowPwdModal(true)}>修改密码</button>
+            <button onClick={handleLogout}>退出登录</button>
+          </div>
+
+          {/* 修改密码弹窗 */}
+          {showPwdModal && (
+            <div className="modal">
+              <div className="modal-content">
+                <h3>修改密码</h3>
+                <input
+                  type="password"
+                  placeholder="原密码"
+                  value={oldPassword}
+                  onChange={(e) => setOldPassword(e.target.value)}
+                />
+                <input
+                  type="password"
+                  placeholder="新密码"
+                  value={newPassword}
+                  onChange={(e) => setNewPassword(e.target.value)}
+                />
+                <input
+                  type="password"
+                  placeholder="确认新密码"
+                  value={confirmPassword}
+                  onChange={(e) => setConfirmPassword(e.target.value)}
+                />
+                <div className="modal-buttons">
+                  <button onClick={handleChangePassword}>确认修改</button>
+                  <button onClick={() => setShowPwdModal(false)}>取消</button>
+                </div>
+              </div>
+            </div>
+          )}
         </div>
       </div>
     </div>
   );
 };
 
-export default UserProfile; 
\ No newline at end of file
+export default UserProfile;
+