pages_register.tsx 8.6 KB

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