Login_20250812102529.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // entrypoints/popup/pages/Login.tsx
  2. import React, { useState } from 'react';
  3. import { useNavigate } from 'react-router-dom';
  4. import { Button, Input, Message, Form } from '@alifd/next';
  5. export default function Login({ setIsLoggedIn }: { setIsLoggedIn: (loggedIn: boolean) => void }) {
  6. const [username, setUsername] = useState('');
  7. const [password, setPassword] = useState('');
  8. const [loading, setLoading] = useState(false);
  9. const [errors, setErrors] = useState<{ username?: string, password?: string }>({});
  10. const navigate = useNavigate();
  11. const validateForm = () => {
  12. const newErrors: { username?: string, password?: string } = {};
  13. if (!username.trim()) {
  14. newErrors.username = '请输入用户名';
  15. }
  16. if (!password) {
  17. newErrors.password = '请输入密码';
  18. } else if (password.length < 6) {
  19. newErrors.password = '密码长度至少为6位';
  20. }
  21. setErrors(newErrors);
  22. return Object.keys(newErrors).length === 0;
  23. };
  24. const evVerify = () => {
  25. fetch('//user.landwu.com/api/user/verify', {
  26. method: 'POST',
  27. headers: {
  28. 'Content-Type': 'application/json',
  29. },
  30. // body: JSON.stringify({
  31. // username,
  32. // password,
  33. // code
  34. // }),
  35. }).then(resjson => {
  36. if (!resjson.ok) {
  37. throw new Error('登录失败');
  38. }
  39. return resjson.json(); // 正确解析 Response 为 JSON 数据
  40. }).then(res => {
  41. const { data = {} } = res;
  42. const { data: info = {} } = data;
  43. const { key = "", img = "" } = info;
  44. })
  45. }
  46. const handleLogin = async (e: React.FormEvent) => {
  47. e.preventDefault();
  48. if (!validateForm()) {
  49. return;
  50. }
  51. setLoading(true);
  52. try {
  53. // 发送登录请求
  54. const response = await fetch('/api/login', {
  55. method: 'POST',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. },
  59. body: JSON.stringify({
  60. username,
  61. password,
  62. code
  63. }),
  64. });
  65. const data = await response.json();
  66. if (response.ok && data.success) {
  67. // 登录成功
  68. localStorage.setItem('isLoggedIn', 'true');
  69. // 如果后端返回token,也可以存储token
  70. // localStorage.setItem('token', data.token);
  71. setIsLoggedIn(true);
  72. navigate('/');
  73. Message.success('登录成功');
  74. } else {
  75. // 登录失败
  76. Message.error(data.message || '登录失败,请检查用户名和密码');
  77. }
  78. } catch (error) {
  79. console.error('登录请求失败:', error);
  80. Message.error('网络错误,请稍后重试');
  81. } finally {
  82. setLoading(false);
  83. }
  84. // 模拟登录请求
  85. // setTimeout(() => {
  86. // // 这里添加实际的登录逻辑
  87. // if (username && password) {
  88. // // 模拟登录成功
  89. // localStorage.setItem('isLoggedIn', 'true');
  90. // setIsLoggedIn(true);
  91. // // 跳转到首页
  92. // navigate('/');
  93. // Message.success('登录成功');
  94. // } else {
  95. // Message.error('登录失败,请检查用户名和密码');
  96. // }
  97. // setLoading(false);
  98. // }, 1000);
  99. };
  100. return (
  101. <div className="bg-gray-50 p-4">
  102. <div className="text-center mb-6">
  103. <h1 className="text-2xl font-bold text-gray-800">登录</h1>
  104. </div>
  105. <Form onSubmit={handleLogin}>
  106. <Form.Item
  107. label="用户名"
  108. required
  109. help={errors.username}
  110. validateState={errors.username ? 'error' : undefined}
  111. >
  112. <Input
  113. placeholder="请输入用户名"
  114. value={username}
  115. onChange={(value) => {
  116. setUsername(value);
  117. if (errors.username) {
  118. setErrors({ ...errors, username: undefined });
  119. }
  120. }}
  121. disabled={loading}
  122. />
  123. </Form.Item>
  124. <Form.Item
  125. label="密码"
  126. required
  127. help={errors.password}
  128. validateState={errors.password ? 'error' : undefined}
  129. >
  130. <Input
  131. htmlType="password"
  132. placeholder="请输入密码"
  133. value={password}
  134. onChange={(value) => {
  135. setPassword(value);
  136. if (errors.password) {
  137. setErrors({ ...errors, password: undefined });
  138. }
  139. }}
  140. disabled={loading}
  141. />
  142. </Form.Item>
  143. <Form.Item
  144. label="验证码"
  145. name="code"
  146. required
  147. >
  148. <Input
  149. size="large"
  150. addonAfter={
  151. <img
  152. src={""}
  153. style={{ width: 70, height: 38 }}
  154. onClick={evVerify}
  155. />
  156. }
  157. autoComplete="off"
  158. />
  159. </Form.Item>
  160. <Form.Item>
  161. <Button
  162. type="primary"
  163. htmlType="submit"
  164. loading={loading}
  165. className="w-full"
  166. >
  167. {loading ? '登录中...' : '登录'}
  168. </Button>
  169. </Form.Item>
  170. </Form>
  171. <div className="mt-4 text-center text-sm text-gray-500">
  172. <p>忘记密码?请联系管理员</p>
  173. </div>
  174. </div>
  175. );
  176. }