import { useState, useEffect } from "react"; import { Box, Flex, TextInput, Button, FormControl, Note, IconButton, } from "@contentful/f36-components"; import { EyeIcon, EyeClosedIcon } from "@contentful/f36-icons"; import { useNavigate } from "react-router-dom"; import { authApi } from "@/services/modules/system"; import { useAuth } from "@/contexts/AuthContext"; import { useLoginAuth } from "./hooks/useAuth"; import { debounce } from "@/utils/lodash"; import styles from "./index.module.css"; export default function LoginPage() { const [account, setAccount] = useState("内容中心管理员"); const [password, setPassword] = useState("zyh931015$"); const [captcha, setCaptcha] = useState(""); const [captchaKey, setCaptchaKey] = useState(""); const [captchaImage, setCaptchaImage] = useState(""); const [errorMessage, setErrorMessage] = useState(""); const [showPassword, setShowPassword] = useState(false); const navigate = useNavigate(); const { refreshAuth } = useAuth(); const { login, loading } = useLoginAuth(); // 获取验证码 const fetchCaptcha = async () => { const checkKey = new Date().getTime() + Math.random().toString(36).slice(-4); // 1629428467008; setCaptchaKey(checkKey); try { const result = await authApi.getCaptcha(checkKey); setCaptchaImage(result); setCaptcha(""); // 清空验证码输入 } catch (error) { console.error("获取验证码失败:", error); } }; // 页面加载时获取验证码 useEffect(() => { fetchCaptcha(); }, []); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); // 清空之前的错误 setErrorMessage(""); // 验证表单 - 只检查必填 if (!account.trim() || !password.trim() || !captcha.trim()) { setErrorMessage("账号、密码或验证码不能为空"); return; } try { // 调用登录(使用 hook 统一处理) await login({ username: account, password, captcha, checkKey: captchaKey, }); console.log("登录成功"); // 刷新权限数据(加载菜单) await refreshAuth(); // 跳转到首页 navigate("/content-model"); } catch (error: any) { console.error("登录失败:", error); setErrorMessage(error.message || "登录失败,请稍后重试"); // 刷新验证码 fetchCaptcha(); } }; return ( {/* 标题 */}

欢迎回来

登录你的账号

{/* 登录表单 */}
{/* 错误提示 */} {errorMessage && {errorMessage}} {/* account */} 账号 { setAccount(e.target.value); // 清除错误提示 if (errorMessage) { setErrorMessage(""); } }} placeholder="请输入你的账号" /> {/* Password */} 密码 { setPassword(e.target.value); // 清除错误提示 if (errorMessage) { setErrorMessage(""); } }} placeholder="请输入你的密码" /> : } variant="transparent" size="small" onClick={() => setShowPassword(!showPassword)} className={styles.passwordToggle} style={{ opacity: showPassword ? 0.5 : 1, transform: showPassword ? 'scale(0.9)' : 'scale(1)' }} /> {/* Captcha */} 验证码 { setCaptcha(e.target.value); // 清除错误提示 if (errorMessage) { setErrorMessage(""); } }} placeholder="请输入验证码" /> {captchaImage ? ( 验证码 ) : ( 点击刷新 )} {/* Remember me */} {/* setRememberMe(!rememberMe)} > Remember me */} {/* Login Button */}
{/* Footer Links */} {/* | | | */}
); }