import { useState, useEffect } from 'react'; import { loginSendCodeApi, getAccountListByCodeApi } from '../../api/login'; import { SelectAccount, type Account } from './select-account'; import { Agreement } from '../agreement'; import { pmBridge } from '../../lib/PostMessageBridge'; import { useAtomValue } from 'jotai'; import { modalStackAtom } from '../../store'; export interface phoneLoginProps { switchAccountLogin: () => void; switchRegister:()=> void; } export const PhoneLogin = ({ switchAccountLogin, switchRegister }: phoneLoginProps) => { const modalState = useAtomValue(modalStackAtom); const [mobile, setMobile] = useState(''); const [code, setCode] = useState(''); const [countdown, setCountdown] = useState(0); const [loading, setLoading] = useState(false); const [accountList, setAccountList] = useState([]); const [showSelectAccount, setShowSelectAccount] = useState(false); const [showAgreement, setShowAgreement] = useState<{ title: string; url: string } | null>(null); useEffect(() => { let timer: ReturnType; if (countdown > 0) { timer = setInterval(() => { setCountdown((prev) => prev - 1); }, 1000); } return () => { if (timer) clearInterval(timer); }; }, [countdown]); const handleGetCode = async () => { if (!mobile) { alert('请输入手机号'); return; } if (!/^1[3-9]\d{9}$/.test(mobile)) { alert('请输入正确的手机号'); return; } setLoading(true); try { await loginSendCodeApi({ mobile }); setCountdown(60); } catch (error: any) { console.error('获取验证码失败:', error); alert(error.message || '获取验证码失败'); } finally { setLoading(false); } }; const handleLogin = async () => { if (!mobile) { alert('请输入手机号'); return; } if (!code) { alert('请输入验证码'); return; } setLoading(true); try { const res = await getAccountListByCodeApi({ mobile, code }); if (res.length > 0) { setAccountList(res); setShowSelectAccount(true); } else { alert('登录成功') const requestId = modalState.login.requestId; pmBridge.sendToIframe("LOGIN_REQUEST", {success:true, data: res}, requestId); } } catch (error: any) { console.error('验证失败:', error); alert(error.message || '验证失败'); } finally { setLoading(false); } }; const handleSelectConfirm = (account: Account) => { console.log('选择账号确认:', account); setShowSelectAccount(false); }; return ( <>

游戏登录

{/* Tabs */}
{/* Form Content */}
{/* Country Code & Phone Input */}
setMobile(e.target.value)} />
{/* Verification Code */}
setCode(e.target.value)} />
{/* Action Button */} {/* Secondary Links */}
{/* Footer Privacy */}

登录即代表您已同意

{showSelectAccount && ( setShowSelectAccount(false)} /> )} {showAgreement && ( setShowAgreement(null)} /> )} ); };