import React, { useState, useEffect, useRef } from "react" import { Modal, Tabs, Form, Input, Button, message } from "antd" import { sendVerificationCode, bindContact, type BindContactResponse } from "@/api/auth" interface BindContactModalProps { open: boolean onSuccess?: (user: BindContactResponse["user"]) => void onCancel?: () => void } const BindContactModal: React.FC = ({ open, onSuccess, onCancel }) => { const [activeTab, setActiveTab] = useState<"email" | "phone">("email") const [form] = Form.useForm() const [loading, setLoading] = useState(false) const [codeLoading, setCodeLoading] = useState(false) const [countdown, setCountdown] = useState(0) const timerRef = useRef | null>(null) useEffect(() => { if (countdown > 0) { timerRef.current = setInterval(() => { setCountdown((prev) => prev - 1) }, 1000) } else if (timerRef.current) { clearInterval(timerRef.current) timerRef.current = null } return () => { if (timerRef.current) clearInterval(timerRef.current) } }, [countdown]) useEffect(() => { if (open) { form.resetFields() setCountdown(0) } }, [open, form]) const handleSendCode = async () => { try { const value = form.getFieldValue(activeTab === "email" ? "email" : "phone") if (!value) { message.warning(activeTab === "email" ? "请输入邮箱" : "请输入手机号") return } setCodeLoading(true) await sendVerificationCode({ target: activeTab, value, purpose: "bind", }) message.success("验证码已发送") setCountdown(60) } catch (error) { // error handled by interceptor } finally { setCodeLoading(false) } } const handleSubmit = async () => { try { const values = await form.validateFields() setLoading(true) const payload = activeTab === "email" ? { email: values.email, email_code: values.code } : { phone: values.phone, phone_code: values.code } const result = await bindContact(payload) message.success("绑定成功") onSuccess?.(result.user) } catch (error) { // error handled by interceptor } finally { setLoading(false) } } return (

为了保障账号安全,请绑定您的邮箱或手机号

setActiveTab(key as "email" | "phone")} items={[ { key: "email", label: "邮箱绑定", children: (
), }, { key: "phone", label: "手机绑定", children: (
), }, ]} />
) } export default BindContactModal