Login.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. <Input
  189. htmlType="invitationCode"
  190. placeholder="请输入邀请码"
  191. value={invitationCode}
  192. onChange={(value) => {
  193. if (typeof value === 'string') {
  194. setInvitationCode(value);
  195. } else {
  196. setInvitationCode(String(value));
  197. }
  198. if (errors.invitationCode) {
  199. setErrors({ ...errors, invitationCode: undefined });
  200. }
  201. }}
  202. disabled={loading}
  203. />
  204. </Form.Item>
  205. <Row>
  206. <Col span={16}>
  207. <Form.Item
  208. label="验证码"
  209. name="code"
  210. required
  211. help={errors.code}
  212. validateState={errors.code ? 'error' : undefined}
  213. >
  214. <Input
  215. size="large"
  216. placeholder="请输入验证码"
  217. value={code}
  218. onChange={(value) => {
  219. if (typeof value === 'string') {
  220. setCode(value);
  221. } else {
  222. setCode(String(value));
  223. }
  224. if (errors.code) {
  225. setErrors({ ...errors, code: undefined });
  226. }
  227. }}
  228. disabled={loading}
  229. />
  230. </Form.Item>
  231. </Col>
  232. <Col span={8}>
  233. <img
  234. src={validImg}
  235. style={{
  236. width: 82,
  237. height: 28,
  238. marginTop: 28,
  239. cursor: 'pointer',
  240. }}
  241. onClick={evVerify}
  242. />
  243. </Col>
  244. </Row>
  245. <Form.Item>
  246. <Form.Submit
  247. type="primary"
  248. validate
  249. loading={loading}
  250. onClick={handleLogin}
  251. className="w-full"
  252. style={{ marginRight: 8 }}
  253. >
  254. {loading ? '登录中...' : '登录'}
  255. </Form.Submit>
  256. </Form.Item>
  257. </Form>
  258. <div className="mt-4 text-center text-sm text-gray-500">
  259. <p>忘记密码?请联系管理员</p>
  260. </div>
  261. </div>
  262. );
  263. }