67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
import { useState } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import Input from '../../components/Input';
|
|
import Button from '../../components/Button';
|
|
import { authApi, errMsg } from '../../api';
|
|
import { IconLogo } from '../../components/Icons';
|
|
|
|
export default function Login() {
|
|
const [username, setUsername] = useState('admin');
|
|
const [password, setPassword] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const navigate = useNavigate();
|
|
|
|
const submit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!username || !password) {
|
|
setError('请输入用户名和密码');
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
setError('');
|
|
try {
|
|
const r = await authApi.login(username, password);
|
|
localStorage.setItem('volunteer_admin_token', r.token);
|
|
localStorage.setItem('volunteer_admin_username', r.username);
|
|
navigate('/admin/activities');
|
|
} catch (e) {
|
|
setError(errMsg(e, '登录失败'));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
|
|
<form onSubmit={submit} className="w-full max-w-md bg-white border-2 border-black p-6 md:p-8">
|
|
<div className="text-center mb-6 flex flex-col items-center">
|
|
<div className="mb-2 text-black"><IconLogo size={48} /></div>
|
|
<h1 className="text-2xl font-black tracking-wide">义工时长管理系统</h1>
|
|
<p className="text-gray-500 text-sm mt-1">管理台登录</p>
|
|
</div>
|
|
|
|
<div className="space-y-4">
|
|
<div>
|
|
<label className="block text-sm font-bold mb-1.5">用户名</label>
|
|
<Input value={username} onChange={(e) => setUsername(e.target.value)} autoFocus />
|
|
</div>
|
|
<div>
|
|
<label className="block text-sm font-bold mb-1.5">密码</label>
|
|
<Input type="password" value={password} onChange={(e) => setPassword(e.target.value)} />
|
|
</div>
|
|
{error && <div className="text-sm text-red-600">{error}</div>}
|
|
<Button type="submit" size="lg" className="w-full" disabled={loading}>
|
|
{loading ? '登录中...' : '登 录'}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="mt-6 text-center text-sm text-gray-500">
|
|
<p>默认账号:admin / admin123</p>
|
|
<a href="/u" className="inline-block mt-2 underline underline-offset-2 hover:text-black">← 返回用户端</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|