修改密码、管理员删帖、促销、退出登录
Change-Id: I2cc0e211ac5a04f9e89d0736fadd25541a5fccb9
diff --git a/src/pages/UserCenter/UserDynamics.css b/src/pages/UserCenter/UserDynamics.css
index e69de29..40a11b4 100644
--- a/src/pages/UserCenter/UserDynamics.css
+++ b/src/pages/UserCenter/UserDynamics.css
@@ -0,0 +1,79 @@
+.user-dynamics-container {
+ padding: 40px 10% 40px 5%;
+ max-width: 900px;
+}
+
+.user-dynamics-title {
+ font-size: 24px;
+ font-weight: bold;
+ margin-bottom: 20px;
+}
+
+.user-dynamics-loading,
+.user-dynamics-empty {
+ font-size: 16px;
+ color: #888;
+ margin-top: 20px;
+}
+
+.dynamic-card {
+ background: #fff;
+ border: 1px solid #eee;
+ border-radius: 12px;
+ padding: 20px;
+ margin-bottom: 20px;
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
+}
+
+.dynamic-header {
+ display: flex;
+ align-items: center;
+ margin-bottom: 10px;
+}
+
+.dynamic-avatar {
+ width: 48px;
+ height: 48px;
+ border-radius: 50%;
+ margin-right: 12px;
+}
+
+.dynamic-userinfo {
+ display: flex;
+ flex-direction: column;
+}
+
+.dynamic-username {
+ font-weight: bold;
+ font-size: 16px;
+}
+
+.dynamic-time {
+ font-size: 12px;
+ color: #999;
+}
+
+.dynamic-title {
+ font-size: 18px;
+ margin: 10px 0 5px;
+}
+
+.dynamic-content p {
+ font-size: 15px;
+ line-height: 1.5;
+}
+
+.dynamic-images {
+ display: flex;
+ flex-wrap: wrap;
+ margin-top: 10px;
+ gap: 10px;
+}
+
+.dynamic-images img {
+ width: 120px;
+ height: 120px;
+ object-fit: cover;
+ border-radius: 8px;
+ border: 1px solid #ddd;
+}
diff --git a/src/pages/UserCenter/UserDynamics.jsx b/src/pages/UserCenter/UserDynamics.jsx
index e69de29..05d9046 100644
--- a/src/pages/UserCenter/UserDynamics.jsx
+++ b/src/pages/UserCenter/UserDynamics.jsx
@@ -0,0 +1,88 @@
+import React, { useEffect, useState } from 'react';
+import { useUser } from '../../context/UserContext';
+import './UserDynamics.css';
+
+const UserDynamics = () => {
+ const { user } = useUser();
+ const [dynamics, setDynamics] = useState([]);
+ const [loading, setLoading] = useState(true);
+
+ useEffect(() => {
+ if (!user?.userId) return;
+
+ const fetchDynamics = async () => {
+ try {
+ const res = await fetch(`/echo/dynamic/${user.userId}/getAdynamic`);
+ const data = await res.json();
+ setDynamics(data.dynamic || []);
+ } catch (err) {
+ console.error('获取动态失败:', err);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ fetchDynamics();
+ }, [user]);
+
+ if (loading) return <div className="user-dynamics-loading">加载中...</div>;
+
+ return (
+ <div className="user-dynamics-container">
+ <h2 className="user-dynamics-title">我的动态</h2>
+ {dynamics.length === 0 ? (
+ <div className="user-dynamics-empty">暂无动态</div>
+ ) : (
+ dynamics.map((item) => (
+ <div key={item.dynamic_id} className="dynamic-card">
+ <div className="dynamic-header">
+ <img
+ className="dynamic-avatar"
+ src={item.avatar_url}
+ alt={item.username}
+ />
+ <div className="dynamic-userinfo">
+ <span className="dynamic-username">{item.username}</span>
+ <span className="dynamic-time">{new Date(item.time).toLocaleString()}</span>
+ </div>
+ </div>
+ <div className="dynamic-content">
+ {item.title && <h4 className="dynamic-title">{item.title}</h4>}
+ <p>{item.content}</p>
+ {/* {item.images && (
+ <div className="dynamic-images">
+ {JSON.parse(item.images).map((img, index) => (
+ <img key={index} src={img} alt={`图${index + 1}`} />
+ ))}
+ </div>
+ )} */}
+ {item.images && (
+ <div className="dynamic-images">
+ {(() => {
+ let imageList = [];
+ try {
+ if (item.images.startsWith('[')) {
+ imageList = JSON.parse(item.images);
+ } else {
+ imageList = [item.images];
+ }
+ } catch (e) {
+ console.error('解析 images 出错:', e);
+ }
+
+ return imageList.map((img, index) => (
+ <img key={index} src={img} alt={`图${index + 1}`} />
+ ));
+ })()}
+ </div>
+ )}
+
+ </div>
+ </div>
+ ))
+ )}
+ </div>
+ );
+};
+
+export default UserDynamics;
diff --git a/src/pages/UserCenter/UserProfile.css b/src/pages/UserCenter/UserProfile.css
index de2ecac..b230c1e 100644
--- a/src/pages/UserCenter/UserProfile.css
+++ b/src/pages/UserCenter/UserProfile.css
@@ -52,9 +52,11 @@
border-radius: 16px;
margin: 0 auto;
margin-top: 40px;
- padding: 10% 20%;
- margin-left: 5%;
- margin-right: 5%;
+ width: 80%;
+ padding-top: 10%;
+ padding-right: 15%;
+ padding-bottom: 10%;
+ padding-left: 10%;
}
.avatar-wrapper {
position: relative;
@@ -208,7 +210,7 @@
}
.task-btn:hover {
- background-color: #357abd;
+ background-color: #bd7035;
}
.task-btn-group {
@@ -218,10 +220,69 @@
gap: 8px;
}
-.loading {
- text-align: center;
- font-size: 18px;
- color: #333;
+
+.exp-bar-wrapper {
+ width: 100%;
+ height: 14px;
+ background-color: #fbfafa;
+ border-radius: 8px;
+ margin: 10px 0;
+ overflow: hidden;
}
+.exp-bar {
+ height: 100%;
+ background-color: #0eb813;
+ transition: width 0.3s ease;
+}
+.exp-progress-text {
+ font-size: 0.9em;
+ color: #555;
+ margin-bottom: 10px;
+}
+
+.profile-actions {
+ margin-top: 1em;
+}
+
+.profile-actions button {
+ padding: 8px 16px;
+ background-color: #4677f5;
+ color: white;
+ border: none;
+ border-radius: 6px;
+ cursor: pointer;
+}
+
+.modal {
+ position: fixed;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ background-color: rgba(0, 0, 0, 0.4);
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ z-index: 99;
+}
+
+.modal-content {
+ background-color: white;
+ padding: 20px;
+ border-radius: 8px;
+ width: 300px;
+}
+
+.modal-content input {
+ display: block;
+ width: 100%;
+ margin: 10px 0;
+ padding: 8px;
+}
+
+.modal-buttons {
+ display: flex;
+ justify-content: space-between;
+}
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;
+