2
0

pages_register.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React, { useState } from 'react';
  2. import { useNavigate } from 'react-router';
  3. import { ArrowRightIcon, EnvelopeIcon, LockClosedIcon, UserIcon } from '@heroicons/react/24/outline';
  4. import { AuthAPI } from './api.ts';
  5. import { handleApiError } from './utils.ts';
  6. const RegisterPage: React.FC = () => {
  7. const navigate = useNavigate();
  8. const [username, setUsername] = useState('');
  9. const [email, setEmail] = useState('');
  10. const [password, setPassword] = useState('');
  11. const [confirmPassword, setConfirmPassword] = useState('');
  12. const [loading, setLoading] = useState(false);
  13. const [error, setError] = useState<string | null>(null);
  14. const handleRegister = async (e: React.FormEvent) => {
  15. e.preventDefault();
  16. // 表单验证
  17. if (!username.trim()) {
  18. setError('用户名不能为空');
  19. return;
  20. }
  21. if (!email.trim()) {
  22. setError('邮箱不能为空');
  23. return;
  24. }
  25. if (!password.trim()) {
  26. setError('密码不能为空');
  27. return;
  28. }
  29. if (password !== confirmPassword) {
  30. setError('两次输入的密码不一致');
  31. return;
  32. }
  33. setLoading(true);
  34. setError(null);
  35. try {
  36. await AuthAPI.register(username, email, password);
  37. // 注册成功后跳转到登录页
  38. navigate('/mobile/login');
  39. } catch (err) {
  40. setError(handleApiError(err));
  41. } finally {
  42. setLoading(false);
  43. }
  44. };
  45. return (
  46. <div className="min-h-screen flex flex-col bg-gradient-to-b from-blue-500 to-blue-700 p-6">
  47. {/* 顶部Logo和标题 */}
  48. <div className="flex flex-col items-center justify-center mt-10 mb-8">
  49. <div className="w-20 h-20 bg-white rounded-2xl flex items-center justify-center shadow-lg mb-4">
  50. <svg className="w-12 h-12 text-blue-600" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  51. <path d="M12 2L2 7L12 12L22 7L12 2Z" fill="currentColor" />
  52. <path d="M2 17L12 22L22 17" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
  53. <path d="M2 12L12 17L22 12" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
  54. </svg>
  55. </div>
  56. <h1 className="text-3xl font-bold text-white">
  57. {window.CONFIG?.APP_NAME || '移动应用'}
  58. </h1>
  59. <p className="text-blue-100 mt-2">创建您的账户</p>
  60. </div>
  61. {/* 注册表单 */}
  62. <div className="bg-white rounded-xl shadow-xl p-6 w-full">
  63. {error && (
  64. <div className="bg-red-50 text-red-700 p-3 rounded-lg mb-4 text-sm">
  65. {error}
  66. </div>
  67. )}
  68. <form onSubmit={handleRegister}>
  69. <div className="mb-4">
  70. <label className="block text-gray-700 text-sm font-medium mb-2" htmlFor="username">
  71. 用户名
  72. </label>
  73. <div className="relative">
  74. <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
  75. <UserIcon className="h-5 w-5 text-gray-400" />
  76. </div>
  77. <input
  78. id="username"
  79. type="text"
  80. value={username}
  81. onChange={(e) => setUsername(e.target.value)}
  82. className="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
  83. placeholder="请输入用户名"
  84. />
  85. </div>
  86. </div>
  87. <div className="mb-4">
  88. <label className="block text-gray-700 text-sm font-medium mb-2" htmlFor="email">
  89. 邮箱
  90. </label>
  91. <div className="relative">
  92. <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
  93. <EnvelopeIcon className="h-5 w-5 text-gray-400" />
  94. </div>
  95. <input
  96. id="email"
  97. type="email"
  98. value={email}
  99. onChange={(e) => setEmail(e.target.value)}
  100. className="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
  101. placeholder="请输入邮箱"
  102. />
  103. </div>
  104. </div>
  105. <div className="mb-4">
  106. <label className="block text-gray-700 text-sm font-medium mb-2" htmlFor="password">
  107. 密码
  108. </label>
  109. <div className="relative">
  110. <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
  111. <LockClosedIcon className="h-5 w-5 text-gray-400" />
  112. </div>
  113. <input
  114. id="password"
  115. type="password"
  116. value={password}
  117. onChange={(e) => setPassword(e.target.value)}
  118. className="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
  119. placeholder="请输入密码"
  120. />
  121. </div>
  122. </div>
  123. <div className="mb-6">
  124. <label className="block text-gray-700 text-sm font-medium mb-2" htmlFor="confirmPassword">
  125. 确认密码
  126. </label>
  127. <div className="relative">
  128. <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
  129. <LockClosedIcon className="h-5 w-5 text-gray-400" />
  130. </div>
  131. <input
  132. id="confirmPassword"
  133. type="password"
  134. value={confirmPassword}
  135. onChange={(e) => setConfirmPassword(e.target.value)}
  136. className="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
  137. placeholder="请再次输入密码"
  138. />
  139. </div>
  140. </div>
  141. <button
  142. type="submit"
  143. disabled={loading}
  144. className="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 flex items-center justify-center"
  145. >
  146. {loading ? (
  147. <svg className="animate-spin -ml-1 mr-2 h-5 w-5 text-white" fill="none" viewBox="0 0 24 24">
  148. <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
  149. <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
  150. </svg>
  151. ) : (
  152. <ArrowRightIcon className="h-5 w-5 mr-2" />
  153. )}
  154. {loading ? '注册中...' : '注册'}
  155. </button>
  156. </form>
  157. <div className="mt-6 text-center">
  158. <button
  159. type="button"
  160. className="text-sm text-blue-600 hover:text-blue-700"
  161. onClick={() => navigate('/mobile/login')}
  162. >
  163. 已有账号?立即登录
  164. </button>
  165. </div>
  166. </div>
  167. {/* 底部文本 */}
  168. <div className="mt-auto pt-8 text-center text-blue-100 text-sm">
  169. &copy; {new Date().getFullYear()} {window.CONFIG?.APP_NAME || '移动应用'}
  170. <p className="mt-1">保留所有权利</p>
  171. </div>
  172. </div>
  173. );
  174. };
  175. export default RegisterPage;