import React, { useState, useEffect } from 'react'; import { Card, Form, Input, Button, Upload, message, Tabs, Avatar, Row, Col, Divider, Typography, Space, Radio, } from 'antd'; import { UserOutlined, LockOutlined, UploadOutlined, PhoneOutlined, MailOutlined, SafetyOutlined, EditOutlined, ManOutlined, WomanOutlined, } from '@ant-design/icons'; import { useSelector } from 'react-redux'; import { RootState, useAppDispatch } from '../store'; import { setUserInfo } from '../store/userSlice'; import { getUserProfile, updateUserProfile, updateUserPwd, uploadAvatar } from '../api/user'; import envConfig from '../../env'; import utils from '../lib/utils'; import { useTranslation } from 'react-i18next'; const { TabPane } = Tabs; const { Title } = Typography; const Profile: React.FC = () => { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const [profileData, setProfileData] = useState(null); const { userInfo } = useSelector((state: RootState) => state.user); const dispatch = useAppDispatch(); const [form] = Form.useForm(); const [pwdForm] = Form.useForm(); const message = utils.message; // 使用全局 message const fetchProfile = async () => { try { const res: any = await getUserProfile(); if (res.code === 200) { setProfileData(res.data); form.setFieldsValue(res.data); } } catch (error) { message.error(t('profile.getProfileFailed')); } }; useEffect(() => { fetchProfile(); }, []); const handleUpdateProfile = async (values: any) => { setLoading(true); try { // 构建完整的个人信息对象,包含用户 ID 等必要字段 const updateData = { ...profileData, ...values, }; const res: any = await updateUserProfile(updateData); if (res.code === 200) { message.success(t('profile.modifySuccess')); const newUserInfo = { ...userInfo, ...values }; dispatch(setUserInfo(newUserInfo)); fetchProfile(); } else { message.error(res.msg || t('profile.modifyFailed')); } } catch (error) { message.error(t('profile.requestFailed')); } finally { setLoading(false); } }; const handleUpdatePwd = async (values: any) => { if (values.newPassword !== values.confirmPassword) { message.error(t('profile.passwordMismatch')); return; } setLoading(true); try { const res: any = await updateUserPwd(values.oldPassword, values.newPassword); if (res.code === 200) { message.success(t('profile.passwordChanged')); pwdForm.resetFields(); } else { message.error(res.msg || t('profile.modifyFailed')); } } catch (error) { message.error(t('profile.requestFailed')); } finally { setLoading(false); } }; const handleUpload = async (options: any) => { const { file } = options; const formData = new FormData(); formData.append('avatarfile', file); try { const res: any = await uploadAvatar(formData); if (res.code === 200) { message.success(t('profile.avatarUploadSuccess')); const newUserInfo = { ...userInfo, avatar: res.imgUrl }; dispatch(setUserInfo(newUserInfo)); fetchProfile(); } else { message.error(res.msg || t('profile.avatarUploadFailed')); } } catch (error) { message.error(t('profile.avatarUploadFailed')); } }; const avatarUrl = profileData?.avatar ? profileData.avatar.startsWith('http') ? profileData.avatar : `${envConfig.baseURL}${profileData.avatar}` : null; return (
{t('profile.title')} {/* 🔥 关键:给 Row 添加 align="stretch" 让两列自动等高 */} {/* 左侧信息卡片 */}
{/* 🔥 自动填充高度 */} } style={{ border: '6px solid #fff', boxShadow: '0 4px 16px rgba(0,0,0,0.08)', }} />
{t('profile.username')}: {profileData?.userName}
{t('profile.phone')}: {profileData?.phonenumber || t('profile.notSet')}
{t('profile.email')}: {profileData?.email || t('profile.notSet')}
{t('profile.role')}: {profileData?.roles?.map((r: any) => r.roleName).join(', ') || t('profile.notSet')}
{/* 右侧表单卡片 */} {/* 基本资料 */}
} disabled style={{ borderRadius: 6 }} /> } placeholder={t('profile.nicknamePlaceholder')} style={{ borderRadius: 6 }} /> {t('profile.male')} {t('profile.female')} } placeholder={t('profile.phonePlaceholder')} style={{ borderRadius: 6 }} /> } placeholder={t('profile.emailPlaceholder')} style={{ borderRadius: 6 }} />
{/* 修改密码 */}
} placeholder={t('profile.oldPasswordPlaceholder')} style={{ borderRadius: 6 }} /> } placeholder={t('profile.newPasswordPlaceholder')} style={{ borderRadius: 6 }} /> } placeholder={t('profile.confirmPasswordPlaceholder')} style={{ borderRadius: 6 }} />
); }; export default Profile;