blob: 96c54850ad9ebcbd9f42f49e89738cbf91c61a78 [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();
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080019
20 const { refresh: postUserLoginRefresh } = useApi(
21 () => request.post(postUserLogin, { email, password}), false);
22 const { refresh: getUserInfoRefresh } = useApi(
23 () => request.get(getUserInfo), false);
San3yuan6f2ed692025-04-16 20:24:49 +080024
25 const nav = useNavigate();
San3yuan03ab0642025-04-29 18:00:25 +080026 const handleLogin = debounce(async () => {
San3yuan6f2ed692025-04-16 20:24:49 +080027 try {
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080028 const res =await postUserLoginRefresh({email, password});
29 console.log("res", res);
San3yuan6f2ed692025-04-16 20:24:49 +080030 if (res==null ||(res as any).error) {
31 alert('Login failed. Please check your credentials.');
32 return;
33 }
34 dispatch({ type: "user/login", payload: res });
35
36 const userInfo = await getUserInfoRefresh();
37 if (userInfo==null || (userInfo as any).error) {
38 alert('Failed to fetch user information.');
39 return;
40 }
41 dispatch({ type: "user/getUserInfo", payload: userInfo });
42 nav('/');
43 } catch (error) {
44 alert('An unexpected error occurred. Please try again later.');
45 if (error instanceof Error) {
46 console.error(error.message); // 明确访问 message 属性
47 } else {
48 console.error('Unknown error occurred');
49 }
50 }
San3yuan03ab0642025-04-29 18:00:25 +080051 }, 1000) as () => void;
San3yuan6f2ed692025-04-16 20:24:49 +080052
San3yuan03ab0642025-04-29 18:00:25 +080053 const handleLogoClick = () => {
54 nav('/');
55 }
San3yuan6f2ed692025-04-16 20:24:49 +080056 return (
57 <div className={style.form}>
San3yuan03ab0642025-04-29 18:00:25 +080058 <img className={style.logo} src={logo} alt="logo" onClick={handleLogoClick}></img>
San3yuan6f2ed692025-04-16 20:24:49 +080059 <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} className={style.email} placeholder="Enter your email" />
60 <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} className={style.password} placeholder="Enter your password" />
San3yuan03ab0642025-04-29 18:00:25 +080061 <button className={style.submit} onClick={() => handleLogin()}>登录</button>
San3yuan6f2ed692025-04-16 20:24:49 +080062 <button className={style.register}>注册</button>
63 <button className={style.forget}> 忘记密码</button>
64 </div>
65 );
66};
67
68export default Login;