"use client"; import { useState, useEffect } from "react"; import { useToast } from "@/contexts/ToastContext"; import * as Form from "@radix-ui/react-form"; import useFetch from "@/hooks/useFetch"; import GoogleLogin from "./google-login"; import { RadioGroup, Radio } from "@headlessui/react"; import { useRouter } from "next/navigation"; import useUserStore from "@/store/userStore"; import { useTranslations } from "next-intl"; import Link from "next/link"; interface LoginFormProps { toRegister: () => void; } type LoginMethod = "password" | "code"; export default function LoginForm({ toRegister }: LoginFormProps) { const t = useTranslations("loginForm"); const tTerms = useTranslations("terms"); // 用于获取用户协议的标题 const tGlobal = useTranslations(); // 用于获取全局翻译,如 "agreeTo" 和 "agreeToTerms" const [loginMethod, setLoginMethod] = useState("password"); const [username, setUsername] = useState(""); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [captcha, setCaptcha] = useState(""); const [captchaTimer, setCaptchaTimer] = useState(0); const [agreed, setAgreed] = useState(true); // 默认勾选 const router = useRouter(); const { addToast } = useToast(); const setUser = useUserStore((state) => state.setUser); const { fetchData: login, loading: loginLoading, error: loginError, data: loginData, } = useFetch({ url: "/api/login/", method: "POST", }); const { fetchData: fetchCaptcha, loading: captchaLoading, error: captchaError, data: captchaData, } = useFetch({ url: "/api/send-verification-sms/", method: "POST", }); const { fetchData: GetUserinfo, loading: userinfoLoading, data: userinfoData, } = useFetch({ url: "/api/profile/", method: "POST", }); useEffect(() => { let timer: NodeJS.Timeout; if (captchaTimer > 0) { timer = setInterval(() => { setCaptchaTimer((prev) => prev - 1); }, 1000); } return () => clearInterval(timer); }, [captchaTimer]); const handleGetCaptcha = async () => { if (!phone) { addToast(t("enterPhoneNumber"), "error"); return; } await fetchCaptcha({ phone_number: phone }); setCaptchaTimer(60); }; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!agreed) { addToast(tGlobal("agreeToTerms"), "error"); return; } // 校验非空字段 if (loginMethod === "password" && !username) { addToast(t("enterUsername"), "error"); return; } if (loginMethod === "code" && !phone) { addToast(t("enterPhone"), "error"); return; } if (loginMethod === "password" && !password) { addToast(t("enterPassword"), "error"); return; } if (loginMethod === "code" && !captcha) { addToast(t("enterCaptcha"), "error"); return; } const loginData = { login_type: loginMethod, ...(loginMethod === "password" && { username, password }), ...(loginMethod === "code" && { phone, code: captcha }), }; // 调用登录函数发送登录请求 login(loginData).then(() => { addToast(t("loginSuccess"), "success"); GetUserinfo().then((res) => { console.log("userinfo", res); setUser(res); }); router.replace("/create"); }); }; return (
{t("orUseLoginMethod")}
{t("passwordLogin")}
{t("codeLogin")}
{loginMethod === "password" && ( <> setUsername(e.target.value) } className="h-10 input border-gray-600 bg-gray-800 outline-none focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder:text-gray-500" placeholder={t("emailOrPhone")} /> setPassword(e.target.value) } className="h-10 input border-gray-600 bg-gray-800 outline-none focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder:text-gray-500" placeholder={t("yourPassword")} /> )} {loginMethod === "code" && ( <> setPhone(e.target.value) } className="h-10 input border-gray-600 bg-gray-800 outline-none focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder:text-gray-500" placeholder={t("phone")} />
setCaptcha(e.target.value) } className="h-10 input border-gray-600 bg-gray-800 outline-none focus:outline-none w-full rounded-lg text-sm text-gray-300 placeholder:text-gray-500" placeholder={t("captcha")} />
)} {/* 用户协议复选框 */}
{loginLoading ? ( ) : ( {t("login")} )}
); }