个人中心页面

This commit is contained in:
mmc
2026-05-13 11:12:17 +08:00
parent d349903ed6
commit 38ceb37926
6 changed files with 568 additions and 84 deletions

359
src/pages/Profile.tsx Normal file
View File

@@ -0,0 +1,359 @@
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<any>(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 (
<div style={{ padding: '32px 40px', background: '#f5f7fa', minHeight: '100vh' }}>
<Title level={4} style={{ marginBottom: 24, fontWeight: 600 }}>
个人中心
</Title>
<Row gutter={24}>
{/* 左侧信息卡片 */}
<Col xs={24} sm={24} md={8} lg={7} xl={6}>
<Card
bordered={false}
style={{
borderRadius: 12,
boxShadow: '0 2px 12px rgba(0,0,0,0.06)',
textAlign: 'center',
overflow: 'hidden',
}}
>
<div style={{ padding: '20px 0' }}>
<Avatar
size={120}
src={avatarUrl}
icon={<UserOutlined style={{ fontSize: 50 }} />}
style={{
border: '6px solid #fff',
boxShadow: '0 4px 16px rgba(0,0,0,0.08)',
}}
/>
<div style={{ marginTop: 20 }}>
<Upload customRequest={handleUpload} showUploadList={false} accept="image/*">
<Button
icon={<UploadOutlined />}
style={{
borderRadius: 6,
borderColor: '#d9d9d9',
}}
>
更换头像
</Button>
</Upload>
</div>
</div>
<Divider style={{ margin: '12px 0' }} />
<div style={{ textAlign: 'left', padding: '8px 12px' }}>
<Space direction="vertical" size="middle" style={{ width: '100%' }}>
<div style={{ display: 'flex', alignItems: 'center', fontSize: 14 }}>
<UserOutlined style={{ marginRight: 12, color: '#165DFF', fontSize: 16 }} />
<span style={{ color: '#666' }}>用户名称:</span>
<strong style={{ marginLeft: 4, color: '#333' }}>{profileData?.userName}</strong>
</div>
<div style={{ display: 'flex', alignItems: 'center', fontSize: 14 }}>
<PhoneOutlined style={{ marginRight: 12, color: '#165DFF', fontSize: 16 }} />
<span style={{ color: '#666' }}>手机号码:</span>
<strong style={{ marginLeft: 4, color: '#333' }}>{profileData?.phonenumber || '未设置'}</strong>
</div>
<div style={{ display: 'flex', alignItems: 'center', fontSize: 14 }}>
<MailOutlined style={{ marginRight: 12, color: '#165DFF', fontSize: 16 }} />
<span style={{ color: '#666' }}>用户邮箱:</span>
<strong style={{ marginLeft: 4, color: '#333' }}>{profileData?.email || '未设置'}</strong>
</div>
<div style={{ display: 'flex', alignItems: 'center', fontSize: 14 }}>
<SafetyOutlined style={{ marginRight: 12, color: '#165DFF', fontSize: 16 }} />
<span style={{ color: '#666' }}>所属角色:</span>
<strong style={{ marginLeft: 4, color: '#333' }}>
{profileData?.roles?.map((r: any) => r.roleName).join(', ') || '未设置'}
</strong>
</div>
</Space>
</div>
</Card>
</Col>
{/* 右侧表单卡片 */}
<Col xs={24} sm={24} md={16} lg={17} xl={18}>
<Card
bordered={false}
style={{
borderRadius: 12,
boxShadow: '0 2px 12px rgba(0,0,0,0.06)',
overflow: 'hidden',
}}
>
<Tabs
defaultActiveKey="1"
type="card"
style={{
marginTop: -8,
}}
>
{/* 基本资料 */}
<TabPane tab="基本资料" key="1">
<div style={{ maxWidth: 500, margin: '0 auto', padding: '12px 0' }}>
<Form
form={form}
layout="vertical"
onFinish={handleUpdateProfile}
initialValues={profileData}
size="large"
>
<Form.Item
label="用户昵称"
name="nickName"
rules={[{ required: true, message: '请输入用户昵称' }]}
>
<Input
prefix={<UserOutlined />}
placeholder="请输入用户昵称"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item
label="手机号码"
name="phonenumber"
rules={[{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }]}
>
<Input
prefix={<PhoneOutlined />}
placeholder="请输入手机号码"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item
label="邮箱"
name="email"
rules={[{ type: 'email', message: '请输入正确的邮箱格式' }]}
>
<Input
prefix={<MailOutlined />}
placeholder="请输入邮箱"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item style={{ marginTop: 28 }}>
<Button
type="primary"
htmlType="submit"
loading={loading}
icon={<EditOutlined />}
style={{
borderRadius: 6,
height: 42,
width: 160,
}}
>
保存修改
</Button>
</Form.Item>
</Form>
</div>
</TabPane>
{/* 修改密码 */}
<TabPane tab="修改密码" key="2">
<div style={{ maxWidth: 500, margin: '0 auto', padding: '12px 0' }}>
<Form
form={pwdForm}
layout="vertical"
onFinish={handleUpdatePwd}
size="large"
>
<Form.Item
label="旧密码"
name="oldPassword"
rules={[{ required: true, message: '请输入旧密码' }]}
>
<Input.Password
prefix={<LockOutlined />}
placeholder="请输入旧密码"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item
label="新密码"
name="newPassword"
rules={[
{ required: true, message: '请输入新密码' },
{ min: 6, message: '密码长度不能小于6位' },
]}
>
<Input.Password
prefix={<LockOutlined />}
placeholder="请输入新密码"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item
label="确认密码"
name="confirmPassword"
rules={[{ required: true, message: '请确认新密码' }]}
>
<Input.Password
prefix={<LockOutlined />}
placeholder="请再次输入新密码"
style={{ borderRadius: 6 }}
/>
</Form.Item>
<Form.Item style={{ marginTop: 28 }}>
<Button
type="primary"
htmlType="submit"
loading={loading}
icon={<EditOutlined />}
style={{
borderRadius: 6,
height: 42,
width: 160,
}}
>
确认修改
</Button>
</Form.Item>
</Form>
</div>
</TabPane>
</Tabs>
</Card>
</Col>
</Row>
</div>
);
};
export default Profile;