| 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 | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 14 | |
| 15 | const Login: React.FC = () => { |
| 16 | const [email, setEmail] = useState(''); |
| 17 | const [password, setPassword] = useState(''); |
| 18 | const dispatch = useAppDispatch(); |
| 阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame^] | 19 | |
| 20 | const { refresh: postUserLoginRefresh } = useApi( |
| 21 | () => request.post(postUserLogin, { email, password}), false); |
| 22 | const { refresh: getUserInfoRefresh } = useApi( |
| 23 | () => request.get(getUserInfo), false); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 24 | |
| 25 | const nav = useNavigate(); |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 26 | const handleLogin = debounce(async () => { |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 27 | try { |
| 阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame^] | 28 | const res =await postUserLoginRefresh({email, password}); |
| 29 | console.log("res", res); |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 30 | 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 | } |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 51 | }, 1000) as () => void; |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 52 | |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 53 | const handleLogoClick = () => { |
| 54 | nav('/'); |
| 55 | } |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 56 | return ( |
| 57 | <div className={style.form}> |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 58 | <img className={style.logo} src={logo} alt="logo" onClick={handleLogoClick}></img> |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 59 | <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" /> |
| San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 61 | <button className={style.submit} onClick={() => handleLogin()}>登录</button> |
| San3yuan | 6f2ed69 | 2025-04-16 20:24:49 +0800 | [diff] [blame] | 62 | <button className={style.register}>注册</button> |
| 63 | <button className={style.forget}> 忘记密码</button> |
| 64 | </div> |
| 65 | ); |
| 66 | }; |
| 67 | |
| 68 | export default Login; |