import styles from "./login.module.scss"; import { IconButton } from "./button"; import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { Path, Login, BASE_URL } from "../constant"; import { useAccessStore } from "../store"; import Locale from "../locales"; import BotIcon from "../icons/bot.svg"; import { getClientConfig } from "../config/client"; import { showToast } from "./ui-lib"; export function AuthPage() { const navigate = useNavigate(); const accessStore = useAccessStore(); const [isLogin, setIsLogin] = useState(true); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const goHome = () => navigate(Path.Home); const goChat = () => navigate(Path.Chat); const resetAccessCode = () => { accessStore.update((access) => { access.openaiApiKey = ""; access.accessCode = ""; }); }; useEffect(() => { if (getClientConfig()?.isApp) { navigate(Path.Settings); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleLogin = async () => { try { const url = BASE_URL + Login.Login; const formData = new URLSearchParams(); formData.append("username", username); formData.append("password", password); const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", }, body: formData.toString(), }); const data = await response.json(); if (data.status.code === 0 && data.result === true) { // 登录成功逻辑 accessStore.update((access) => { access.accessCode = data.accessCode; // 如果需要从响应中获取 accessCode 的话 }); goChat(); // 导航到聊天页面或其他逻辑 } else { // 登录失败 showToast(data.status.msg); } } catch (error) { console.error("API 请求失败", error); showToast(Locale.Login.SystemFail); } }; const handleRegister = async () => { try { const url = BASE_URL + Login.Register; const formData = new URLSearchParams(); formData.append("username", username); formData.append("password", password); const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: formData.toString(), }); const data = await response.json(); if (data.status.code === 0 && data.result === true) { // 注册成功逻辑 showToast(Locale.Login.ResisterSuc); setIsLogin(true); // 切换到登录状态 } else { // 注册失败 showToast(Locale.Login.ResisterFail); } } catch (error) { console.error("API 请求失败", error); showToast(Locale.Login.SystemFail); } }; const handleSubmit = () => { if (isLogin) { handleLogin(); } else { if (password !== confirmPassword) { showToast(Locale.Login.PasswordFail); return; } handleRegister(); } }; return (