Login_20250813115729.tsx 8.0 KB

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