import { CirclePlay, Send } from "lucide-react"; import { useState } from "react"; import { getRandomAccountApi, registerAccountApi } from "../../api/register"; import { loginAccountApi } from "../../api/login"; import { useAtomValue, useSetAtom } from "jotai"; import { modalStackAtom, openModalAction } from "../../store"; import { pmBridge } from "../../lib/PostMessageBridge"; export interface AccountRegisterProps { switchPhoneRegister: () => void; switchLogin: () => void; } export const AccountRegister = ({ switchPhoneRegister, switchLogin }: AccountRegisterProps) => { const [userName, setUserName] = useState(''); const [userPwd, setUserPwd] = useState(''); const [loading, setLoading] = useState(false); const [agreed, setAgreed] = useState(false); const modalState = useAtomValue(modalStackAtom); const openModal = useSetAtom(openModalAction); const handleRandomAccount = async () => { setLoading(true); try { const res = await getRandomAccountApi({ is_activate: 1 }); if (res.user_name && res.user_pwd) { setUserName(res.user_name); setUserPwd(res.user_pwd); } } catch (error: any) { console.error('获取随机账号失败:', error); alert(error.message || '获取随机账号失败'); } finally { setLoading(false); } }; const handleRegister = async () => { if (!userName) { alert('请输入账号'); return; } if (!userPwd) { alert('请输入密码'); return; } if (!agreed) { alert('请先同意服务协议与隐私权政策'); return; } setLoading(true); try { // 首先执行注册 await registerAccountApi({ user_name: userName, user_pwd: userPwd }); // 注册成功后立即执行登录 const loginRes = await loginAccountApi({ user_name: userName, user_pwd: userPwd }); if (loginRes.token) { // 存储 token localStorage.setItem('token', loginRes.token); // 使用 pmBridge 回传给父 iframe const requestId = modalState.login.requestId; pmBridge.sendToIframe("LOGIN_SUCCESS", {success:true, data: loginRes}, requestId); // 关闭登录弹窗 openModal({ name: 'login', item: { isOpen: false } }); } else { alert('登录失败:未返回 token'); } } catch (error: any) { console.error('流程失败:', error); alert(error.message || '操作失败'); } finally { setLoading(false); } }; return (
); }