Login_20250813115338.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (!validateForm()) return
  50. try {
  51. setLoading(true);
  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. if (typeof value === 'string') {
  126. setUsername(value);
  127. } else {
  128. setUsername(String(value));
  129. }
  130. if (errors.username) {
  131. setErrors({ ...errors, username: undefined });
  132. }
  133. }}
  134. disabled={loading}
  135. />
  136. </Form.Item>
  137. <Form.Item
  138. label="密码"
  139. name="password"
  140. required
  141. help={errors.password}
  142. validateState={errors.password ? 'error' : undefined}
  143. >
  144. <Input
  145. htmlType="password"
  146. placeholder="请输入密码"
  147. value={password}
  148. onChange={(value) => {
  149. if (typeof value === 'string') {
  150. setPassword(value);
  151. } else {
  152. setPassword(String(value));
  153. }
  154. if (errors.password) {
  155. setErrors({ ...errors, password: undefined });
  156. }
  157. }}
  158. disabled={loading}
  159. />
  160. </Form.Item>
  161. <Row>
  162. <Col span={16}>
  163. <Form.Item
  164. label="验证码"
  165. name="code"
  166. required
  167. help={errors.code}
  168. validateState={errors.code ? 'error' : undefined}
  169. >
  170. <Input
  171. size="large"
  172. placeholder="请输入验证码"
  173. />
  174. </Form.Item>
  175. </Col>
  176. <Col span={8}>
  177. <img
  178. src={validImg}
  179. style={{
  180. width: 82,
  181. height: 28,
  182. marginTop: 28,
  183. cursor: 'pointer',
  184. }}
  185. onClick={evVerify}
  186. />
  187. </Col>
  188. </Row>
  189. <Form.Item>
  190. <Form.Submit
  191. type="primary"
  192. validate
  193. loading={loading}
  194. onClick={handleLogin}
  195. className="w-full"
  196. style={{ marginRight: 8 }}
  197. >
  198. {loading ? '登录中...' : '登录'}
  199. </Form.Submit>
  200. </Form.Item>
  201. </Form>
  202. <div className="mt-4 text-center text-sm text-gray-500">
  203. <p>忘记密码?请联系管理员</p>
  204. </div>
  205. </div>
  206. );
  207. }