blob: 562f178fdae76928006aebe631e762def3f82497 [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';
San3yuana2ee30b2025-06-05 21:20:17 +080014import {message} from 'antd';
15
San3yuan6f2ed692025-04-16 20:24:49 +080016
17const Login: React.FC = () => {
18 const [email, setEmail] = useState('');
19 const [password, setPassword] = useState('');
20 const dispatch = useAppDispatch();
San3yuana2ee30b2025-06-05 21:20:17 +080021 const { refresh: postUserLoginRefresh } = useApi(() => request.post(postUserLogin, {email, password}), false);
San3yuan03ab0642025-04-29 18:00:25 +080022 const { refresh: getUserInfoRefresh } = useApi(() => request.get(getUserInfo), false);
San3yuana2ee30b2025-06-05 21:20:17 +080023 const [messageApi, contextHolder] = message.useMessage();
San3yuan6f2ed692025-04-16 20:24:49 +080024
25 const nav = useNavigate();
San3yuana2ee30b2025-06-05 21:20:17 +080026 const showErrorMessage = async (message: string) => {
27 messageApi.error(message);
28 };
San3yuan03ab0642025-04-29 18:00:25 +080029 const handleLogin = debounce(async () => {
San3yuan6f2ed692025-04-16 20:24:49 +080030 try {
San3yuana2ee30b2025-06-05 21:20:17 +080031 const res = await postUserLoginRefresh();
32 console.log(res);
33 if (res == null || (res as any).error) {
34 throw new Error('登录失败');
San3yuan6f2ed692025-04-16 20:24:49 +080035 }
36 dispatch({ type: "user/login", payload: res });
San3yuana2ee30b2025-06-05 21:20:17 +080037
San3yuan6f2ed692025-04-16 20:24:49 +080038 const userInfo = await getUserInfoRefresh();
San3yuana2ee30b2025-06-05 21:20:17 +080039 if (userInfo == null || (userInfo as any).error) {
40 throw new Error('获取用户信息失败');
San3yuan6f2ed692025-04-16 20:24:49 +080041 }
42 dispatch({ type: "user/getUserInfo", payload: userInfo });
43 nav('/');
44 } catch (error) {
San3yuana2ee30b2025-06-05 21:20:17 +080045 // 将错误信息传递给一个异步函数
46 showErrorMessage('登录失败,请检查您的用户名和密码');
San3yuan6f2ed692025-04-16 20:24:49 +080047 }
San3yuan03ab0642025-04-29 18:00:25 +080048 }, 1000) as () => void;
San3yuan6f2ed692025-04-16 20:24:49 +080049
San3yuan03ab0642025-04-29 18:00:25 +080050 const handleLogoClick = () => {
51 nav('/');
52 }
San3yuan6f2ed692025-04-16 20:24:49 +080053 return (
54 <div className={style.form}>
San3yuana2ee30b2025-06-05 21:20:17 +080055 {contextHolder}
San3yuan03ab0642025-04-29 18:00:25 +080056 <img className={style.logo} src={logo} alt="logo" onClick={handleLogoClick}></img>
San3yuan6f2ed692025-04-16 20:24:49 +080057 <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} className={style.email} placeholder="Enter your email" />
58 <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} className={style.password} placeholder="Enter your password" />
San3yuan03ab0642025-04-29 18:00:25 +080059 <button className={style.submit} onClick={() => handleLogin()}>登录</button>
San3yuan6f2ed692025-04-16 20:24:49 +080060 <button className={style.register}>注册</button>
61 <button className={style.forget}> 忘记密码</button>
62 </div>
63 );
64};
65
66export default Login;