'use client'; import React, { useState } from 'react'; import { MapPin, Phone, Mail } from 'lucide-react'; import { useTranslations } from 'next-intl'; interface FormData { name: string; phone: string; email: string; description: string; } interface FormErrors { name?: string; phone?: string; email?: string; description?: string; } const Contact: React.FC = () => { const t = useTranslations('Contact'); const [formData, setFormData] = useState({ name: '', phone: '', email: '', description: '', }); const [errors, setErrors] = useState({}); const [isSubmitting, setIsSubmitting] = useState(false); const [submitStatus, setSubmitStatus] = useState<{type: 'success' | 'error', message: string} | null>(null); const validateForm = (): boolean => { const newErrors: FormErrors = {}; if (!formData.name.trim()) { newErrors.name = t('form.errors.nameRequired'); } const phoneRegex = /^1[3-9]\d{9}$/; if (!formData.phone.trim()) { newErrors.phone = t('form.errors.phoneRequired'); } else if (!phoneRegex.test(formData.phone)) { newErrors.phone = t('form.errors.phoneInvalid'); } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!formData.email.trim()) { newErrors.email = t('form.errors.emailRequired'); } else if (!emailRegex.test(formData.email)) { newErrors.email = t('form.errors.emailInvalid'); } if (!formData.description.trim()) { newErrors.description = t('form.errors.descRequired'); } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); // 清除该字段的错误 if (errors[name as keyof FormErrors]) { setErrors(prev => ({ ...prev, [name]: undefined })); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } setIsSubmitting(true); setSubmitStatus(null); try { const response = await fetch('/api/messages', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }); const data = await response.json(); if (response.ok) { setSubmitStatus({ type: 'success', message: data.message || t('form.success') }); // 清空表单 setFormData({ name: '', phone: '', email: '', description: '', }); } else { setSubmitStatus({ type: 'error', message: data.error || '提交失败,请稍后再试' }); } } catch (error) { console.error('提交错误:', error); setSubmitStatus({ type: 'error', message: '网络错误,请检查网络连接后重试' }); } finally { setIsSubmitting(false); } }; return (
{/* Abstract background element */}

{t('title')} • {t('subtitle')}

{t('heading')}
共创价值

无论您有项目需求还是技术咨询,我们都随时准备为您提供专业的解答和服务。

{t('info.address')}

{t('info.addressValue')}

{t('info.phone')}

19844561014

{t('info.email')}

feie9454@gmail.com

在线留言

{submitStatus && (
{submitStatus.message}
)}
{errors.name &&

{errors.name}

}
{errors.phone &&

{errors.phone}

}
{errors.email &&

{errors.email}

}
{errors.description &&

{errors.description}

}
); }; export default Contact;