Login.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // entrypoints/popup/pages/Login.tsx
  2. import React, { useState, useEffect, useRef } from 'react'; // 添加 useRef
  3. import { useNavigate } from 'react-router-dom';
  4. import { Input, Loading, Form, Grid } from '@alifd/next';
  5. const { Row, Col } = Grid
  6. import { CustomMessage } from '@/components/CustomMessage'
  7. import { useLoading } from '@/components/LoadingContext';
  8. export default function Login({ setIsLoggedIn }: { setIsLoggedIn: (loggedIn: boolean) => void }) {
  9. const formRef = useRef<any>(null);
  10. const [username, setUsername] = useState('');
  11. const [password, setPassword] = useState('');
  12. const [invitationCode, setInvitationCode] = useState('');
  13. const [loading, setLoading] = useState(false);
  14. const [errors, setErrors] = useState<{ username?: string, password?: string, code?: string, invitationCode?: string }>({});
  15. const [keys, setKeys] = useState('');
  16. const [validImg, setValidImg] = useState('');
  17. const { showLoading, hideLoading } = useLoading();
  18. const navigate = useNavigate();
  19. // 添加验证码状态
  20. const [code, setCode] = useState('');
  21. useEffect(() => {
  22. evVerify()
  23. }, [])
  24. // 添加键盘事件监听
  25. useEffect(() => {
  26. const handleKeyDown = (e: KeyboardEvent) => {
  27. if (e.key === 'Enter' && !loading) {
  28. handleLogin({ username, password, code, invitationCode });
  29. }
  30. };
  31. document.addEventListener('keydown', handleKeyDown);
  32. return () => {
  33. document.removeEventListener('keydown', handleKeyDown);
  34. };
  35. // 添加依赖项,确保每次状态更新时都重新绑定事件监听器
  36. }, [username, password, code, invitationCode, loading]);
  37. const validateForm = () => {
  38. const newErrors: { username?: string, password?: string, code?: string, invitationCode?: string } = {};
  39. if (!username.trim()) {
  40. newErrors.username = '请输入用户名';
  41. }
  42. if (!password) {
  43. newErrors.password = '请输入密码';
  44. } else if (password.length < 6) {
  45. newErrors.password = '密码长度至少为6位';
  46. }
  47. // 添加验证码校验
  48. if (!code.trim()) {
  49. newErrors.code = '请输入验证码';
  50. }
  51. // 添加邀请码校验
  52. // if (!invitationCode.trim()) {
  53. // newErrors.invitationCode = '请输入邀请码';
  54. // }
  55. setErrors(newErrors);
  56. return Object.keys(newErrors).length === 0;
  57. };
  58. const evVerify = () => {
  59. fetch('https://user.landwu.com/api/user/verify', {
  60. method: 'POST',
  61. headers: {
  62. 'Content-Type': 'application/json',
  63. }
  64. }).then(resjson => {
  65. return resjson.json();
  66. }).then(res => {
  67. const { data = {} } = res;
  68. const { data: info = {} } = data;
  69. const { key = "", img = "" } = info;
  70. setKeys(key)
  71. setValidImg(img)
  72. })
  73. }
  74. const handleLogin = (values: any) => {
  75. if (!validateForm()) return
  76. try {
  77. // setLoading(true);
  78. showLoading('登录中...');
  79. const params = {
  80. ...values,
  81. key: keys
  82. }
  83. // 发送登录请求
  84. fetch('https://user.landwu.com/api/user/login', {
  85. method: 'POST',
  86. headers: {
  87. 'Content-Type': 'application/json',
  88. },
  89. body: JSON.stringify(params),
  90. }).then(response => response.json()).then(res => {
  91. const { msg = "", token = "", code } = res;
  92. if (!code) {
  93. // setLoading(false);
  94. hideLoading();
  95. return false;
  96. }
  97. if (code == -1) {
  98. evVerify()
  99. CustomMessage.error(msg);
  100. // setLoading(false);
  101. hideLoading();
  102. return false;
  103. }
  104. fetch('https://user.landwu.com/api/user/getProfile', {
  105. method: 'POST',
  106. headers: {
  107. 'Content-Type': 'application/json',
  108. },
  109. body: JSON.stringify({ api_token: token }),
  110. }).then(response => response.json()).then(res => {
  111. if (res.code == 1) {
  112. const { data = {} } = res;
  113. const { userinfo = {} } = data;
  114. const userinfo_str = JSON.stringify(userinfo);
  115. localStorage.setItem('userinfo', userinfo_str);
  116. CustomMessage.success(msg);
  117. localStorage.setItem("access_token", token);
  118. localStorage.setItem('isLoggedIn', 'true');
  119. setTimeout(() => {
  120. // setLoading(false);
  121. hideLoading();
  122. setIsLoggedIn(true);
  123. // 跳转到首页
  124. navigate('/');
  125. }, 100)
  126. }
  127. }).catch(err => {
  128. // setLoading(false);
  129. hideLoading();
  130. })
  131. }).catch((e) => {
  132. console.log("catch")
  133. // setLoading(false);
  134. hideLoading();
  135. });
  136. } catch (error) {
  137. console.error('登录请求失败:', error);
  138. CustomMessage.error('网络错误,请稍后重试');
  139. hideLoading();
  140. }
  141. };
  142. return (
  143. <div className="bg-gray-50 p-4">
  144. <div className="text-center mb-6">
  145. <h1 className="text-2xl font-bold text-gray-800">登录</h1>
  146. </div>
  147. <Form>
  148. <Form.Item
  149. label="用户名"
  150. name="username"
  151. required
  152. help={errors.username}
  153. validateState={errors.username ? 'error' : undefined}
  154. >
  155. <Input
  156. placeholder="请输入用户名"
  157. value={username}
  158. onChange={(value) => {
  159. if (typeof value === 'string') {
  160. setUsername(value);
  161. } else {
  162. setUsername(String(value));
  163. }
  164. if (errors.username) {
  165. setErrors({ ...errors, username: undefined });
  166. }
  167. }}
  168. disabled={loading}
  169. />
  170. </Form.Item>
  171. <Form.Item
  172. label="密码"
  173. name="password"
  174. required
  175. help={errors.password}
  176. validateState={errors.password ? 'error' : undefined}
  177. >
  178. <Input
  179. htmlType="password"
  180. placeholder="请输入密码"
  181. value={password}
  182. onChange={(value) => {
  183. if (typeof value === 'string') {
  184. setPassword(value);
  185. } else {
  186. setPassword(String(value));
  187. }
  188. if (errors.password) {
  189. setErrors({ ...errors, password: undefined });
  190. }
  191. }}
  192. disabled={loading}
  193. />
  194. </Form.Item>
  195. <Form.Item
  196. label="邀请码"
  197. name="invitationCode"
  198. // required
  199. // help={errors.invitationCode}
  200. // validateState={errors.invitationCode ? 'error' : undefined}
  201. >
  202. <Input
  203. htmlType="invitationCode"
  204. placeholder="请输入邀请码"
  205. value={invitationCode}
  206. onChange={(value) => {
  207. if (typeof value === 'string') {
  208. setInvitationCode(value);
  209. } else {
  210. setInvitationCode(String(value));
  211. }
  212. // if (errors.invitationCode) {
  213. // setErrors({ ...errors, invitationCode: undefined });
  214. // }
  215. }}
  216. disabled={loading}
  217. />
  218. </Form.Item>
  219. <Row>
  220. <Col span={16}>
  221. <Form.Item
  222. label="验证码"
  223. name="code"
  224. required
  225. help={errors.code}
  226. validateState={errors.code ? 'error' : undefined}
  227. >
  228. <Input
  229. size="large"
  230. placeholder="请输入验证码"
  231. value={code}
  232. onChange={(value) => {
  233. if (typeof value === 'string') {
  234. setCode(value);
  235. } else {
  236. setCode(String(value));
  237. }
  238. if (errors.code) {
  239. setErrors({ ...errors, code: undefined });
  240. }
  241. }}
  242. disabled={loading}
  243. />
  244. </Form.Item>
  245. </Col>
  246. <Col span={8}>
  247. <img
  248. src={validImg}
  249. style={{
  250. width: 82,
  251. height: 28,
  252. marginTop: 28,
  253. cursor: 'pointer',
  254. }}
  255. onClick={evVerify}
  256. />
  257. </Col>
  258. </Row>
  259. <Form.Item>
  260. <Form.Submit
  261. type="primary"
  262. validate
  263. loading={loading}
  264. onClick={handleLogin}
  265. className="w-full"
  266. style={{ marginRight: 8 }}
  267. >
  268. {loading ? '登录中...' : '登录'}
  269. </Form.Submit>
  270. </Form.Item>
  271. </Form>
  272. <div className="mt-4 text-center text-sm text-gray-500">
  273. <p>忘记密码?请联系管理员</p>
  274. </div>
  275. </div>
  276. );
  277. }