import React from 'react' import { useNavigate } from 'react-router' import { useForm } from 'react-hook-form' import { useQuery, useMutation } from '@tanstack/react-query' import { UserAPI } from './api/index.ts' import type { User } from '../share/types.ts' import { useAuth } from './hooks.tsx' export default function ProfilePage() { const navigate = useNavigate() const { register, handleSubmit, formState: { errors }, setValue } = useForm<{ username: string nickname: string email: string phone?: string password?: string }>() // 获取当前用户信息 const { data: user } = useQuery({ queryKey: ['currentUser'], queryFn: async () => { const res = await UserAPI.getUsers({ limit: 1 }) if (res.data?.length > 0) { const userData = res.data[0] setValue('username', userData.username) setValue('nickname', userData.nickname || '') setValue('email', userData.email || '') setValue('phone', userData.phone || '') return userData } return null } }) // 更新用户信息 const { mutate: updateUser, isPending } = useMutation({ mutationFn: async (data: { nickname: string email: string phone?: string password?: string }) => { if (!user?.id) throw new Error('用户ID不存在') return UserAPI.updateUser(user.id, { nickname: data.nickname, email: data.email, phone: data.phone, ...(data.password ? { password: data.password } : {}) }) }, onSuccess: (updatedUser) => { alert('更新成功') }, onError: (error) => { console.error('更新失败:', error) alert('更新失败') } }) const onSubmit = (data: { username: string nickname: string email: string phone?: string password?: string }) => { updateUser({ nickname: data.nickname, email: data.email, phone: data.phone, password: data.password }) } const { logout } = useAuth() const handleLogout = async () => { if (confirm('确定要退出登录吗?')) { await logout() navigate('/mobile/login') } } return (

我的

{user?.avatar ? ( {user?.nickname ) : ( 用户 )}

{user?.nickname || user?.username || '未登录用户'}

个人信息

设置
账号安全
通知设置
隐私
关于
) }