Login_20250812141052.tsx 7.2 KB

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