Login_20250812145951.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // entrypoints/popup/pages/Login.tsx
  2. import React, { useState, useEffect, useRef } from 'react';
  3. import { useNavigate } from 'react-router-dom';
  4. import { Button, Input, Message, Grid } from '@alifd/next';
  5. const { Row, Col } = Grid;
  6. export default function Login({ setIsLoggedIn }: { setIsLoggedIn: (loggedIn: boolean) => void }) {
  7. const [username, setUsername] = useState('');
  8. const [password, setPassword] = useState('');
  9. const [code, setCode] = useState('');
  10. const [loading, setLoading] = useState(false);
  11. const [errors, setErrors] = useState<{ username?: string, password?: string, code?: 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, code?: 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. if (!code.trim()) {
  29. newErrors.code = '请输入验证码';
  30. }
  31. setErrors(newErrors);
  32. return Object.keys(newErrors).length === 0;
  33. };
  34. const evVerify = () => {
  35. fetch('https://user.landwu.com/api/user/verify', {
  36. method: 'POST',
  37. headers: {
  38. 'Content-Type': 'application/json',
  39. }
  40. }).then(resjson => {
  41. return resjson.json();
  42. }).then(res => {
  43. const { data = {} } = res;
  44. const { data: info = {} } = data;
  45. const { key = "", img = "" } = info;
  46. setKeys(key)
  47. setValidImg(img)
  48. })
  49. }
  50. const handleLogin = () => {
  51. if (!validateForm()) {
  52. return;
  53. }
  54. setLoading(true);
  55. try {
  56. const params = {
  57. username,
  58. password,
  59. code,
  60. key: keys
  61. }
  62. fetch('https://user.landwu.com/api/user/login', {
  63. method: 'POST',
  64. headers: {
  65. 'Content-Type': 'application/json',
  66. },
  67. body: JSON.stringify(params),
  68. }).then(response => response.json()).then(res => {
  69. const { msg = "", token = "", code } = res;
  70. if (!code) {
  71. setLoading(false);
  72. return false;
  73. }
  74. if (code == -1) {
  75. evVerify()
  76. Message.error(msg);
  77. setLoading(false);
  78. return false;
  79. }
  80. Message.success(msg);
  81. localStorage.setItem("access_token", token);
  82. localStorage.setItem('isLoggedIn', 'true');
  83. setIsLoggedIn(true);
  84. navigate('/');
  85. }).catch((e) => {
  86. Message.error('网络错误,请稍后重试');
  87. setLoading(false);
  88. });
  89. } catch (error) {
  90. console.error('登录请求失败:', error);
  91. Message.error('网络错误,请稍后重试');
  92. } finally {
  93. setLoading(false);
  94. }
  95. };
  96. const handleInputChange = (field: string, value: string) => {
  97. if (field === 'username') {
  98. setUsername(value);
  99. if (errors.username) {
  100. setErrors({ ...errors, username: undefined });
  101. }
  102. } else if (field === 'password') {
  103. setPassword(value);
  104. if (errors.password) {
  105. setErrors({ ...errors, password: undefined });
  106. }
  107. } else if (field === 'code') {
  108. setCode(value);
  109. if (errors.code) {
  110. setErrors({ ...errors, code: undefined });
  111. }
  112. }
  113. };
  114. return (
  115. <div className="bg-gray-50 p-4">
  116. <div className="text-center mb-6">
  117. <h1 className="text-2xl font-bold text-gray-800">登录</h1>
  118. </div>
  119. <div className="login-form">
  120. <div className="form-item mb-4">
  121. <label className="block text-sm font-medium text-gray-700 mb-1">用户名</label>
  122. <Input
  123. placeholder="请输入用户名"
  124. value={username}
  125. onChange={(value) => handleInputChange('username', value)}
  126. disabled={loading}
  127. />
  128. {errors.username && <div className="text-red-500 text-sm mt-1">{errors.username}</div>}
  129. </div>
  130. <div className="form-item mb-4">
  131. <label className="block text-sm font-medium text-gray-700 mb-1">密码</label>
  132. <Input
  133. htmlType="password"
  134. placeholder="请输入密码"
  135. value={password}
  136. onChange={(value) => handleInputChange('password', value)}
  137. disabled={loading}
  138. />
  139. {errors.password && <div className="text-red-500 text-sm mt-1">{errors.password}</div>}
  140. </div>
  141. <Row>
  142. <Col span={16}>
  143. <div className="form-item">
  144. <label className="block text-sm font-medium text-gray-700 mb-1">验证码</label>
  145. <Input
  146. size="large"
  147. placeholder="请输入验证码"
  148. value={code}
  149. onChange={(value) => handleInputChange('code', value)}
  150. disabled={loading}
  151. />
  152. {errors.code && <div className="text-red-500 text-sm mt-1">{errors.code}</div>}
  153. </div>
  154. </Col>
  155. <Col span={8}>
  156. <img
  157. src={validImg}
  158. style={{
  159. width: 82,
  160. height: 30,
  161. marginTop: 28,
  162. cursor: 'pointer'
  163. }}
  164. onClick={evVerify}
  165. alt="验证码"
  166. />
  167. </Col>
  168. </Row>
  169. <div className="form-item mt-6">
  170. <Button
  171. type="primary"
  172. loading={loading}
  173. onClick={handleLogin}
  174. className="w-full"
  175. >
  176. {loading ? '登录中...' : '登录'}
  177. </Button>
  178. </div>
  179. </div>
  180. <div className="mt-4 text-center text-sm text-gray-500">
  181. <p>忘记密码?请联系管理员</p>
  182. </div>
  183. </div>
  184. );
  185. }