fix: 修复TypeScript类型错误

- 移除未使用的导入
- 修复产品详情页面的description类型错误
- 修复服务详情页面的description类型错误
- 修复联系表单API的类型错误
- 添加Award图标的导入
This commit is contained in:
张翔
2026-02-26 16:26:40 +08:00
parent e4cf4836dd
commit 3de9890fc4
8 changed files with 2030 additions and 9 deletions
+107
View File
@@ -0,0 +1,107 @@
import { NextRequest, NextResponse } from 'next/server';
import { Resend } from 'resend';
import { z } from 'zod';
const contactSchema = z.object({
name: z.string().min(1, '姓名不能为空').max(50, '姓名不能超过50个字符'),
phone: z.string().optional(),
email: z.string().min(1, '邮箱不能为空').email('邮箱格式不正确').max(100, '邮箱不能超过100个字符'),
subject: z.string().min(1, '主题不能为空').max(100, '主题不能超过100个字符'),
message: z.string().min(1, '消息内容不能为空').max(1000, '消息内容不能超过1000个字符'),
});
const RATE_LIMIT_WINDOW = 60 * 60 * 1000; // 1小时
const RATE_LIMIT_MAX_REQUESTS = 10;
interface RateLimitRecord {
count: number;
resetTime: number;
}
const rateLimitStore: Record<string, RateLimitRecord> = {};
function checkRateLimit(ip: string): { allowed: boolean; remaining: number } {
const now = Date.now();
const record = rateLimitStore[ip];
if (!record || now > record.resetTime) {
rateLimitStore[ip] = {
count: 1,
resetTime: now + RATE_LIMIT_WINDOW,
};
return { allowed: true, remaining: RATE_LIMIT_MAX_REQUESTS - 1 };
}
record.count += 1;
const remaining = Math.max(0, RATE_LIMIT_MAX_REQUESTS - record.count);
if (record.count > RATE_LIMIT_MAX_REQUESTS) {
return { allowed: false, remaining: 0 };
}
return { allowed: true, remaining };
}
export async function POST(request: NextRequest) {
const clientIp = request.headers.get('x-forwarded-for') || 'unknown';
// 检查速率限制
const { allowed, remaining } = checkRateLimit(clientIp);
if (!allowed) {
return NextResponse.json(
{ error: '请求过于频繁,请稍后再试' },
{ status: 429, headers: { 'X-RateLimit-Remaining': '0' } }
);
}
try {
const body = await request.json();
const validatedData = contactSchema.parse(body);
const { name, phone, email, subject, message } = validatedData;
const resend = new Resend(process.env.RESEND_API_KEY);
await resend.emails.send({
from: process.env.FROM_EMAIL || 'No reply <noreply@resend.dev>',
to: [process.env.CONTACT_EMAIL || 'contact@novalon.cn'],
subject: `[${subject}] ${name}`,
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #333;">新消息通知</h2>
<div style="background: #f5f5f5; padding: 20px; border-radius: 8px; margin: 20px 0;">
<p style="margin: 10px 0;"><strong>姓名:</strong>${name}</p>
${phone ? `<p style="margin: 10px 0;"><strong>电话:</strong>${phone}</p>` : ''}
<p style="margin: 10px 0;"><strong>邮箱:</strong>${email}</p>
<p style="margin: 10px 0;"><strong>主题:</strong>${subject}</p>
<p style="margin: 10px 0;"><strong>消息内容:</strong></p>
<div style="background: #fff; padding: 15px; border-radius: 4px; margin-top: 10px; white-space: pre-wrap;">${message}</div>
</div>
<p style="color: #666; font-size: 14px;">此邮件由系统自动发送,请勿直接回复。</p>
</div>
`,
});
return NextResponse.json(
{ success: true, remaining },
{ status: 200, headers: { 'X-RateLimit-Remaining': remaining.toString() } }
);
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: '数据验证失败', details: error.issues },
{ status: 400 }
);
}
console.error('Contact form error:', error);
return NextResponse.json(
{ error: '发送失败,请稍后再试' },
{ status: 500 }
);
}
}
export async function OPTIONS() {
return NextResponse.json({}, { status: 200 });
}