| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 1 | import React, { useEffect } from 'react'; |
| 2 | import { useApi } from '@/hooks/request'; |
| 3 | import request from '@/utils/request'; |
| 4 | import { postUserLogin} from '@/api/auth'; |
| 5 | import { useAppDispatch } from '@/hooks/store'; |
| 6 | import { RootState } from '@/store'; |
| 7 | import style from './login.module.css'; |
| 8 | import { useState } from 'react'; |
| 9 | import { useSelector } from 'react-redux'; |
| 10 | import { useNavigate } from 'react-router'; |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 11 | import logo from '&/assets/logo.png'; |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 12 | import { getUserInfo } from '@/api/user'; |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 13 | import debounce from 'lodash/debounce'; |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 14 | import {message} from 'antd'; |
| 15 | |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 16 | |
| 17 | const Login: React.FC = () => { |
| 18 | const [email, setEmail] = useState(''); |
| 19 | const [password, setPassword] = useState(''); |
| 20 | const dispatch = useAppDispatch(); |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 21 | const { refresh: postUserLoginRefresh } = useApi(() => request.post(postUserLogin, {email, password}), false); |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 22 | const { refresh: getUserInfoRefresh } = useApi(() => request.get(getUserInfo), false); |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 23 | const [messageApi, contextHolder] = message.useMessage(); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 24 | |
| 25 | const nav = useNavigate(); |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 26 | const showErrorMessage = async (message: string) => { |
| 27 | messageApi.error(message); |
| 28 | }; |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 29 | const handleLogin = debounce(async () => { |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 30 | try { |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 31 | const res = await postUserLoginRefresh(); |
| 32 | console.log(res); |
| 33 | if (res == null || (res as any).error) { |
| 34 | throw new Error('登录失败'); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 35 | } |
| 36 | dispatch({ type: "user/login", payload: res }); |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 37 | |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 38 | const userInfo = await getUserInfoRefresh(); |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 39 | if (userInfo == null || (userInfo as any).error) { |
| 40 | throw new Error('获取用户信息失败'); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 41 | } |
| 42 | dispatch({ type: "user/getUserInfo", payload: userInfo }); |
| 43 | nav('/'); |
| 44 | } catch (error) { |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 45 | // 将错误信息传递给一个异步函数 |
| 46 | showErrorMessage('登录失败,请检查您的用户名和密码'); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 47 | } |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 48 | }, 1000) as () => void; |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 49 | |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 50 | const handleLogoClick = () => { |
| 51 | nav('/'); |
| 52 | } |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 53 | return ( |
| 54 | <div className={style.form}> |
| San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 55 | {contextHolder} |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 56 | <img className={style.logo} src={logo} alt="logo" onClick={handleLogoClick}></img> |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 57 | <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" /> |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 59 | <button className={style.submit} onClick={() => handleLogin()}>登录</button> |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 60 | <button className={style.register}>注册</button> |
| 61 | <button className={style.forget}> 忘记密码</button> |
| 62 | </div> |
| 63 | ); |
| 64 | }; |
| 65 | |
| 66 | export default Login; |