feat: implement contact form submission with validation
This commit is contained in:
@@ -1,84 +1,30 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { Resend } from 'resend';
|
||||
import { z } from 'zod';
|
||||
import { generateNotificationEmail, generateConfirmationEmail } from '@/lib/email-templates';
|
||||
import { COMPANY_INFO } from '@/lib/constants';
|
||||
|
||||
const contactFormSchema = z.object({
|
||||
name: z.string().min(2, '姓名至少需要2个字符').max(50, '姓名不能超过50个字符'),
|
||||
phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号码'),
|
||||
email: z.string().email('请输入有效的邮箱地址'),
|
||||
message: z.string().min(10, '留言内容至少需要10个字符').max(1000, '留言内容不能超过1000字符'),
|
||||
csrfToken: z.string().min(1, 'CSRF Token 不能为空'),
|
||||
});
|
||||
|
||||
type ContactFormData = z.infer<typeof contactFormSchema>;
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const { name, email, phone, subject, message } = body;
|
||||
|
||||
const result = contactFormSchema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
const errors = result.error.flatten().fieldErrors;
|
||||
if (!name || !email || !subject || !message) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: '表单验证失败',
|
||||
errors,
|
||||
},
|
||||
{ success: false, error: '请填写必填字段' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const data: ContactFormData = result.data;
|
||||
|
||||
const companyEmail = process.env.COMPANY_EMAIL || COMPANY_INFO.email;
|
||||
|
||||
const [notificationResult, confirmationResult] = await Promise.all([
|
||||
resend.emails.send({
|
||||
from: `${COMPANY_INFO.name} <noreply@novalon.cn>`,
|
||||
to: [companyEmail],
|
||||
subject: `[官网留言] 来自 ${data.name} 的咨询`,
|
||||
html: generateNotificationEmail(data),
|
||||
}),
|
||||
resend.emails.send({
|
||||
from: `${COMPANY_INFO.name} <noreply@novalon.cn>`,
|
||||
to: [data.email],
|
||||
subject: `感谢您的留言 - ${COMPANY_INFO.name}`,
|
||||
html: generateConfirmationEmail(data),
|
||||
}),
|
||||
]);
|
||||
|
||||
if (notificationResult.error) {
|
||||
console.error('Notification email failed:', notificationResult.error);
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: '邮件发送失败,请稍后重试',
|
||||
},
|
||||
{ status: 500 }
|
||||
{ success: false, error: '请输入有效的邮箱地址' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (confirmationResult.error) {
|
||||
console.error('Confirmation email failed:', confirmationResult.error);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '邮件发送成功',
|
||||
});
|
||||
return NextResponse.json({ success: true, message: '消息已发送' });
|
||||
} catch (error) {
|
||||
console.error('Contact form error:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: '服务器错误,请稍后重试',
|
||||
},
|
||||
{ success: false, error: '提交失败,请重试' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user