import React, { useState, useEffect } from 'react'; import { Card, Typography, Form, Input, Button, Avatar, Space, Upload, message, Row, Col, Modal } from 'antd'; import { UserOutlined, LockOutlined, PhoneOutlined, MailOutlined, UploadOutlined } from '@ant-design/icons'; import { useAuthStore } from '../store/authStore'; const { Title, Paragraph } = Typography; const ProfilePage: React.FC = () => { const { user, setUser } = useAuthStore(); const [form] = Form.useForm(); const [passwordForm] = Form.useForm(); const [loading, setLoading] = useState(false); const [passwordModalVisible, setPasswordModalVisible] = useState(false); const [avatarUrl, setAvatarUrl] = useState(user?.avatar); const [passportUrl, setPassportUrl] = useState(user?.passport); const [driverLicenseUrl, setDriverLicenseUrl] = useState(user?.driverLicense); useEffect(() => { if (user) { form.setFieldsValue({ name: user.name, phone: user.phone, email: user.email }); setAvatarUrl(user.avatar); setPassportUrl(user.passport); setDriverLicenseUrl(user.driverLicense); } }, [user, form]); const handleSubmit = async () => { try { const values = await form.validateFields(); setLoading(true); // 调用API更新用户信息 const response = await fetch(`/api/users/${user?.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...values, avatar: avatarUrl, passport: passportUrl, driverLicense: driverLicenseUrl }) }); const data = await response.json(); if (data.success) { message.success('个人信息已更新'); // 更新authStore中的用户信息 if (user) { const updatedUser = { ...user, name: values.name, email: values.email, phone: values.phone, avatar: avatarUrl }; setUser(updatedUser); } } else { message.error(data.message || '更新失败'); } } catch (error) { console.error('提交失败:', error); message.error('更新失败,请重试'); } finally { setLoading(false); } }; const handlePasswordSubmit = async () => { try { const values = await passwordForm.validateFields(); // 调用API更新密码 const response = await fetch(`/api/users/${user?.id}/password`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ currentPassword: values.currentPassword, newPassword: values.newPassword }) }); const data = await response.json(); if (data.success) { message.success('密码已更新'); setPasswordModalVisible(false); passwordForm.resetFields(); } else { message.error(data.message || '密码更新失败'); } } catch (error) { console.error('提交失败:', error); message.error('密码更新失败,请重试'); } }; const handleAvatarChange = (info: any) => { if (info.file.status === 'done') { setAvatarUrl(URL.createObjectURL(info.file.originFileObj)); message.success('头像上传成功'); } else if (info.file.status === 'error') { message.error('头像上传失败'); } }; const handlePassportChange = (info: any) => { if (info.file.status === 'done') { setPassportUrl(URL.createObjectURL(info.file.originFileObj)); message.success('护照上传成功'); } else if (info.file.status === 'error') { message.error('护照上传失败'); } }; const handleDriverLicenseChange = (info: any) => { if (info.file.status === 'done') { setDriverLicenseUrl(URL.createObjectURL(info.file.originFileObj)); message.success('驾照上传成功'); } else if (info.file.status === 'error') { message.error('驾照上传失败'); } }; return (
个人信息 管理个人账号信息
{avatarUrl ? ( ) : ( } /> )} 点击更换头像 {user?.name || user?.username} {user?.role === 'admin' ? '管理员' : user?.role === 'manager' ? '经理' : '普通用户'}
} /> } />
证件上传
{passportUrl ? ( 护照 ) : ( 点击上传护照 )} {driverLicenseUrl ? ( 驾照 ) : ( 点击上传驾照 )}
setPasswordModalVisible(false)} onOk={handlePasswordSubmit} width={400} >
} /> } /> ({ validator(_, value) { if (!value || getFieldValue('newPassword') === value) { return Promise.resolve(); } return Promise.reject(new Error('两次输入的密码不一致')); } })]}> } />
); }; export default ProfilePage;