import { useState, useEffect, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuthStore } from '../../store/authStore'; import { loginApi, getCaptchaApi } from '../../api/auth'; import { toast } from 'sonner'; import { Button } from '../../components/ui/button'; import { Input } from '../../components/ui/input'; import { Label } from '../../components/ui/label'; import { ShieldCheck, Disc3, ArrowRight, Heart, Sparkles, Waves } from 'lucide-react'; import { motion } from 'framer-motion'; export default function Login() { const navigate = useNavigate(); const setToken = useAuthStore((state) => state.setToken); const setUserInfo = useAuthStore((state) => state.setUserInfo); const [account, setAccount] = useState(''); const [password, setPassword] = useState(''); const [captcha, setCaptcha] = useState(''); const [captchaId, setCaptchaId] = useState(''); const [captchaImage, setCaptchaImage] = useState(''); const [loading, setLoading] = useState(false); const mounted = useRef(false); const fetchCaptcha = async () => { try { const res: any = await getCaptchaApi(); const b64 = res.captcha; if (b64 && !b64.startsWith('data:')) { setCaptchaImage(`data:image/png;base64,${b64}`); } else { setCaptchaImage(b64); } setCaptchaId(res.captchaId); } catch (error) { console.error('获取验证码失败', error); } }; useEffect(() => { if (!mounted.current) { fetchCaptcha(); mounted.current = true; } }, []); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); if (!account || !password) return toast.error('请输入账号和密码'); if (!captcha) return toast.error('请输入验证码'); try { setLoading(true); const res: any = await loginApi({ account, password, captcha, captchaId }); toast.success('欢迎回来,首席播音官'); setToken(res.token); setUserInfo(res.user); navigate('/'); } catch (error: any) { fetchCaptcha(); setCaptcha(''); } finally { setLoading(false); } }; return (
专业音频管理中枢,
在简约之中,掌控声音的力量。