pages_profile.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React from 'react'
  2. import { useNavigate } from 'react-router'
  3. import { useForm } from 'react-hook-form'
  4. import { useQuery, useMutation } from '@tanstack/react-query'
  5. import { UserAPI } from './api.ts'
  6. import type { User } from '../share/types.ts'
  7. import { useAuth } from './hooks.tsx'
  8. export default function ProfilePage() {
  9. const navigate = useNavigate()
  10. const { register, handleSubmit, formState: { errors }, setValue } = useForm<{
  11. username: string
  12. nickname: string
  13. email: string
  14. phone?: string
  15. password?: string
  16. }>()
  17. // 获取当前用户信息
  18. const { data: user } = useQuery({
  19. queryKey: ['currentUser'],
  20. queryFn: async () => {
  21. const res = await UserAPI.getUsers({ limit: 1 })
  22. if (res.data?.length > 0) {
  23. const userData = res.data[0]
  24. setValue('username', userData.username)
  25. setValue('nickname', userData.nickname || '')
  26. setValue('email', userData.email || '')
  27. setValue('phone', userData.phone || '')
  28. return userData
  29. }
  30. return null
  31. }
  32. })
  33. // 更新用户信息
  34. const { mutate: updateUser, isPending } = useMutation({
  35. mutationFn: async (data: {
  36. nickname: string
  37. email: string
  38. phone?: string
  39. password?: string
  40. }) => {
  41. if (!user?.id) throw new Error('用户ID不存在')
  42. return UserAPI.updateUser(user.id, {
  43. nickname: data.nickname,
  44. email: data.email,
  45. phone: data.phone,
  46. ...(data.password ? { password: data.password } : {})
  47. })
  48. },
  49. onSuccess: (updatedUser) => {
  50. alert('更新成功')
  51. },
  52. onError: (error) => {
  53. console.error('更新失败:', error)
  54. alert('更新失败')
  55. }
  56. })
  57. const onSubmit = (data: {
  58. username: string
  59. nickname: string
  60. email: string
  61. phone?: string
  62. password?: string
  63. }) => {
  64. updateUser({
  65. nickname: data.nickname,
  66. email: data.email,
  67. phone: data.phone,
  68. password: data.password
  69. })
  70. }
  71. const { logout } = useAuth()
  72. const handleLogout = async () => {
  73. if (confirm('确定要退出登录吗?')) {
  74. await logout()
  75. navigate('/mobile/login')
  76. }
  77. }
  78. return (
  79. <div className="p-4">
  80. <h1 className="text-2xl font-bold mb-4">我的</h1>
  81. <div className="bg-white rounded-lg shadow p-4 mb-4">
  82. <div className="flex items-center mb-4">
  83. <div className="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mr-4">
  84. {user?.avatar ? (
  85. <img
  86. src={user.avatar}
  87. alt={user?.nickname || user?.username || '用户'}
  88. className="w-16 h-16 rounded-full object-cover"
  89. />
  90. ) : (
  91. <span className="text-2xl text-blue-600">用户</span>
  92. )}
  93. </div>
  94. <div>
  95. <h2 className="text-xl font-semibold">{user?.nickname || user?.username || '未登录用户'}</h2>
  96. <p className="text-gray-500">个人信息</p>
  97. </div>
  98. </div>
  99. </div>
  100. <div className="bg-white rounded-lg shadow p-4 mb-4">
  101. <h2 className="text-lg font-semibold mb-4">编辑信息</h2>
  102. <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
  103. <div>
  104. <label className="block text-sm font-medium text-gray-700 mb-1">用户名</label>
  105. <input
  106. {...register('username')}
  107. disabled
  108. className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 bg-gray-100"
  109. />
  110. </div>
  111. <div>
  112. <label className="block text-sm font-medium text-gray-700 mb-1">昵称</label>
  113. <input
  114. {...register('nickname', { required: '请输入昵称' })}
  115. className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
  116. />
  117. {errors.nickname && <p className="mt-1 text-sm text-red-600">{errors.nickname.message}</p>}
  118. </div>
  119. <div>
  120. <label className="block text-sm font-medium text-gray-700 mb-1">邮箱</label>
  121. <input
  122. {...register('email', {
  123. required: '请输入邮箱',
  124. pattern: {
  125. value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
  126. message: '请输入有效的邮箱地址'
  127. }
  128. })}
  129. type="email"
  130. className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
  131. />
  132. {errors.email && <p className="mt-1 text-sm text-red-600">{errors.email.message}</p>}
  133. </div>
  134. <div>
  135. <label className="block text-sm font-medium text-gray-700 mb-1">手机号</label>
  136. <input
  137. {...register('phone')}
  138. className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
  139. />
  140. </div>
  141. <div>
  142. <label className="block text-sm font-medium text-gray-700 mb-1">新密码</label>
  143. <input
  144. {...register('password')}
  145. type="password"
  146. placeholder="留空则不修改密码"
  147. className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
  148. />
  149. </div>
  150. <div className="flex space-x-3 pt-4">
  151. <button
  152. type="submit"
  153. disabled={isPending}
  154. className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50"
  155. >
  156. {isPending ? '保存中...' : '保存'}
  157. </button>
  158. <button
  159. type="button"
  160. onClick={() => navigate(-1)}
  161. className="px-4 py-2 bg-gray-200 text-gray-800 rounded-md hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2"
  162. >
  163. 返回
  164. </button>
  165. </div>
  166. </form>
  167. </div>
  168. <div className="bg-white rounded-lg shadow mb-4">
  169. <div className="p-4 border-b">
  170. <span className="font-medium">设置</span>
  171. </div>
  172. <div className="divide-y">
  173. <div className="p-4 flex justify-between items-center">
  174. <span>账号安全</span>
  175. <span className="text-gray-400">›</span>
  176. </div>
  177. <div className="p-4 flex justify-between items-center">
  178. <span>通知设置</span>
  179. <span className="text-gray-400">›</span>
  180. </div>
  181. <div className="p-4 flex justify-between items-center">
  182. <span>隐私</span>
  183. <span className="text-gray-400">›</span>
  184. </div>
  185. <div className="p-4 flex justify-between items-center">
  186. <span>关于</span>
  187. <span className="text-gray-400">›</span>
  188. </div>
  189. </div>
  190. </div>
  191. <button
  192. onClick={handleLogout}
  193. className="w-full py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
  194. >
  195. 退出登录
  196. </button>
  197. </div>
  198. )
  199. }