blob: bd429c75f2f899311b15148578b3edfbbbe83045 [file] [log] [blame]
San3yuan6f2ed692025-04-16 20:24:49 +08001import React, { useEffect } from 'react';
2import { useApi } from '@/hooks/request';
3import request from '@/utils/request';
4import { postUserLogin} from '@/api/auth';
5import { useAppDispatch } from '@/hooks/store';
6import { RootState } from '@/store';
7import style from './login.module.css';
8import { useState } from 'react';
9import { useSelector } from 'react-redux';
10import { useNavigate } from 'react-router';
San3yuan03ab0642025-04-29 18:00:25 +080011import logo from '&/assets/logo.png';
San3yuan6f2ed692025-04-16 20:24:49 +080012import { getUserInfo } from '@/api/user';
San3yuan03ab0642025-04-29 18:00:25 +080013import debounce from 'lodash/debounce';
San3yuan6f2ed692025-04-16 20:24:49 +080014
15const Login: React.FC = () => {
16 const [email, setEmail] = useState('');
17 const [password, setPassword] = useState('');
18 const dispatch = useAppDispatch();
19 const { refresh: postUserLoginRefresh } = useApi(() => request.post(postUserLogin, {}), false);
San3yuan03ab0642025-04-29 18:00:25 +080020 const { refresh: getUserInfoRefresh } = useApi(() => request.get(getUserInfo), false);
San3yuan6f2ed692025-04-16 20:24:49 +080021
22 const nav = useNavigate();
San3yuan03ab0642025-04-29 18:00:25 +080023 const handleLogin = debounce(async () => {
San3yuan6f2ed692025-04-16 20:24:49 +080024 try {
San3yuan03ab0642025-04-29 18:00:25 +080025 const res =await postUserLoginRefresh();
San3yuan6f2ed692025-04-16 20:24:49 +080026 if (res==null ||(res as any).error) {
27 alert('Login failed. Please check your credentials.');
28 return;
29 }
30 dispatch({ type: "user/login", payload: res });
31
32 const userInfo = await getUserInfoRefresh();
33 if (userInfo==null || (userInfo as any).error) {
34 alert('Failed to fetch user information.');
35 return;
36 }
37 dispatch({ type: "user/getUserInfo", payload: userInfo });
38 nav('/');
39 } catch (error) {
40 alert('An unexpected error occurred. Please try again later.');
41 if (error instanceof Error) {
42 console.error(error.message); // 明确访问 message 属性
43 } else {
44 console.error('Unknown error occurred');
45 }
46 }
San3yuan03ab0642025-04-29 18:00:25 +080047 }, 1000) as () => void;
San3yuan6f2ed692025-04-16 20:24:49 +080048
San3yuan03ab0642025-04-29 18:00:25 +080049 const handleLogoClick = () => {
50 nav('/');
51 }
San3yuan6f2ed692025-04-16 20:24:49 +080052 return (
53 <div className={style.form}>
San3yuan03ab0642025-04-29 18:00:25 +080054 <img className={style.logo} src={logo} alt="logo" onClick={handleLogoClick}></img>
San3yuan6f2ed692025-04-16 20:24:49 +080055 <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} className={style.email} placeholder="Enter your email" />
56 <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} className={style.password} placeholder="Enter your password" />
San3yuan03ab0642025-04-29 18:00:25 +080057 <button className={style.submit} onClick={() => handleLogin()}>登录</button>
San3yuan6f2ed692025-04-16 20:24:49 +080058 <button className={style.register}>注册</button>
59 <button className={style.forget}> 忘记密码</button>
60 </div>
61 );
62};
63
64export default Login;