This commit is contained in:
eric
2026-03-27 18:20:36 -05:00
parent a8976b2eee
commit 326bad4336
34 changed files with 2787 additions and 996 deletions

View File

@@ -12,6 +12,8 @@ type LoginFormProps = {
) => Promise<boolean | { ok: boolean; error?: string }>;
onSuccess: () => void;
autoFocus?: boolean;
/** 验证请求进行中(用于全屏 loading 等) */
onBusyChange?: (busy: boolean) => void;
};
export function LoginForm({
@@ -19,6 +21,7 @@ export function LoginForm({
onVerifyByEmail,
onSuccess,
autoFocus,
onBusyChange,
}: LoginFormProps) {
const [mode, setMode] = useState<"user_id" | "email">("user_id");
const [userId, setUserId] = useState("");
@@ -41,6 +44,10 @@ export function LoginForm({
if (autoFocus) inputRef.current?.focus();
}, [autoFocus, mode]);
useEffect(() => {
onBusyChange?.(loading);
}, [loading, onBusyChange]);
const handleUserIdSubmit = async (e: FormEvent) => {
e.preventDefault();
const trimmed = userId.trim();
@@ -51,14 +58,19 @@ export function LoginForm({
setError(null);
setLoading(true);
let succeeded = false;
try {
const ok = await onVerify(trimmed);
if (ok) onSuccess();
else setError("未找到该账号的会员记录,请确认账号正确");
if (ok) {
succeeded = true;
onSuccess();
} else {
setError("未找到该账号的会员记录,请确认账号正确");
}
} catch {
setError("验证失败,请稍后重试");
} finally {
setLoading(false);
if (!succeeded) setLoading(false);
}
};
@@ -79,6 +91,7 @@ export function LoginForm({
setError(null);
setLoading(true);
let succeeded = false;
try {
const candidateUserId = getCandidateUserId();
const result = await onVerifyByEmail(
@@ -88,12 +101,16 @@ export function LoginForm({
);
const ok = typeof result === "object" ? result.ok : result;
const errMsg = typeof result === "object" ? result.error : undefined;
if (ok) onSuccess();
else setError(errMsg || "邮箱或密码错误,或未找到关联的会员记录");
if (ok) {
succeeded = true;
onSuccess();
} else {
setError(errMsg || "邮箱或密码错误,或未找到关联的会员记录");
}
} catch {
setError("验证失败,请稍后重试");
} finally {
setLoading(false);
if (!succeeded) setLoading(false);
}
};