Login_20250814103233.tsx 8.0 KB

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