Login.tsx 11 KB

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