import React, { useState, useEffect } from 'react'; import { Card, Form, Input, Button, Upload, message, Tabs, Avatar, Row, Col, Divider, Typography, Space, } from 'antd'; import { UserOutlined, LockOutlined, UploadOutlined, PhoneOutlined, MailOutlined, SafetyOutlined, EditOutlined, } 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'; const { TabPane } = Tabs; const { Title } = Typography; const Profile: React.FC = () => { 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 fetchProfile = async () => { try { const res: any = await getUserProfile(); if (res.code === 200) { setProfileData(res.data); form.setFieldsValue(res.data); } } catch (error) { message.error('获取个人信息失败'); } }; useEffect(() => { fetchProfile(); }, []); const handleUpdateProfile = async (values: any) => { setLoading(true); try { const res: any = await updateUserProfile(values); if (res.code === 200) { message.success('修改成功'); const newUserInfo = { ...userInfo, ...values }; dispatch(setUserInfo(newUserInfo)); fetchProfile(); } else { message.error(res.msg || '修改失败'); } } catch (error) { message.error('请求失败'); } finally { setLoading(false); } }; const handleUpdatePwd = async (values: any) => { if (values.newPassword !== values.confirmPassword) { message.error('两次输入的密码不一致'); return; } setLoading(true); try { const res: any = await updateUserPwd(values.oldPassword, values.newPassword); if (res.code === 200) { message.success('密码修改成功'); pwdForm.resetFields(); } else { message.error(res.msg || '修改失败'); } } catch (error) { message.error('请求失败'); } 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('头像上传成功'); const newUserInfo = { ...userInfo, avatar: res.imgUrl }; dispatch(setUserInfo(newUserInfo)); fetchProfile(); } else { message.error(res.msg || '上传失败'); } } catch (error) { message.error('上传失败'); } }; const avatarUrl = profileData?.avatar ? profileData.avatar.startsWith('http') ? profileData.avatar : `${envConfig.baseURL}${profileData.avatar}` : null; return (
个人中心 {/* 左侧信息卡片 */}
} style={{ border: '6px solid #fff', boxShadow: '0 4px 16px rgba(0,0,0,0.08)', }} />
用户名称: {profileData?.userName}
手机号码: {profileData?.phonenumber || '未设置'}
用户邮箱: {profileData?.email || '未设置'}
所属角色: {profileData?.roles?.map((r: any) => r.roleName).join(', ') || '未设置'}
{/* 右侧表单卡片 */} {/* 基本资料 */}
} placeholder="请输入用户昵称" style={{ borderRadius: 6 }} /> } placeholder="请输入手机号码" style={{ borderRadius: 6 }} /> } placeholder="请输入邮箱" style={{ borderRadius: 6 }} />
{/* 修改密码 */}
} placeholder="请输入旧密码" style={{ borderRadius: 6 }} /> } placeholder="请输入新密码" style={{ borderRadius: 6 }} /> } placeholder="请再次输入新密码" style={{ borderRadius: 6 }} />
); }; export default Profile;