import { Fonts } from "@/constants/Fonts"; import { router } from "expo-router"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { StyleSheet, TouchableOpacity, View } from "react-native"; import { useAuth } from "../../contexts/auth-context"; import { fetchApi } from "../../lib/server-api-util"; import { User } from "../../types/user"; import { ThemedText } from "../ThemedText"; import Button from "./ui/Button"; import TextInput from "./ui/TextInput"; interface LoginProps { updateUrlParam: (status: string, value: string) => void; setError: (error: string) => void; setShowPassword: (showPassword: boolean) => void; showPassword: boolean; } const Login = ({ updateUrlParam, setError, setShowPassword, showPassword }: LoginProps) => { const { t } = useTranslation(); const { login } = useAuth(); const [isLoading, setIsLoading] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const handleLogin = async () => { if (!email) { setError(t('auth.login.emailPlaceholder', { ns: 'login' })); return; }; if (!password) { setError(t('auth.login.passwordPlaceholder', { ns: 'login' })); return; } setIsLoading(true); try { const body = { account: email, password: password, }; const res = await fetchApi('/iam/login/password-login', { method: 'POST', body: JSON.stringify(body), }, true, false); login({ ...res, email: res?.account }, res.access_token || ''); const userInfo = await fetchApi("/iam/user-info"); if (userInfo?.nickname) { router.replace('/ask'); } else { router.replace('/user-message'); } } catch (error) { const errorMessage = error instanceof Error ? error.message : t('auth.login.loginError', { ns: 'login' }); setError(errorMessage); } finally { setIsLoading(false); } }; const handleForgotPassword = () => { updateUrlParam('status', 'forgetPwd'); }; const handleSignUp = () => { updateUrlParam('status', 'signUp'); }; return ( {/* 邮箱 */} { setEmail(text); setError('123'); }} autoCapitalize="none" value={email} /> {/* 密码 */} { setPassword(text); setError('123'); }} autoCapitalize="none" value={password} type="password" setShowPassword={setShowPassword} showPassword={showPassword} containerStyle={{ marginBottom: 0 }} /> {/* 忘记密码 */} {t('auth.login.forgotPassword', { ns: 'login' })} {/* 登录按钮 */}