更新安全配置项目

This commit is contained in:
2026-04-18 23:52:15 +08:00
parent 70d3020035
commit 59b3117eb9
7 changed files with 332 additions and 377 deletions
+55 -55
View File
@@ -1,68 +1,68 @@
import { NextRequest, NextResponse } from "next/server";
import { batchTranslateWithAI, translateWithAI } from "@/lib/ai-translate";
import {NextRequest, NextResponse} from "next/server";
import {batchTranslateWithAI, translateWithAI} from "@/lib/ai-translate";
// 单个翻译
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const word = searchParams.get("word");
const {searchParams} = new URL(request.url);
const word = searchParams.get("word");
if (!word) {
return NextResponse.json({ error: "缺少 word 参数" }, { status: 400 });
}
try {
const translation = await translateWithAI(word);
if (!translation) {
return NextResponse.json({ error: "翻译失败" }, { status: 500 });
if (!word) {
return NextResponse.json({error: "缺少 word 参数"}, {status: 400});
}
return NextResponse.json({
word,
english: translation,
});
} catch (error) {
console.error("AI translation error:", error);
return NextResponse.json(
{ error: String(error) },
{ status: 500 }
);
}
try {
const translation = await translateWithAI(word);
if (!translation) {
return NextResponse.json({error: "翻译失败"}, {status: 500});
}
return NextResponse.json({
word,
english: translation,
});
} catch (error) {
console.error("AI translation error:", error);
return NextResponse.json(
{error: String(error)},
{status: 500}
);
}
}
// 批量翻译
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { words } = body as { words: string[] };
try {
const body = await request.json();
const {words} = body as { words: string[] };
if (!Array.isArray(words) || words.length === 0) {
return NextResponse.json({ error: "缺少 words 参数(数组)" }, { status: 400 });
if (!Array.isArray(words) || words.length === 0) {
return NextResponse.json({error: "缺少 words 参数(数组)"}, {status: 400});
}
// 检查 API Key
if (!process.env.AI_API_KEY) {
return NextResponse.json({error: "AI 配置未完成,请先在设置中配置 AI"}, {status: 500});
}
const results = await batchTranslateWithAI(words);
// 转换为响应格式
const translations: Record<string, string | null> = {};
for (const word of words) {
translations[word] = results.get(word) || null;
}
return NextResponse.json({
translations,
total: words.length,
success: results.size,
});
} catch (error) {
console.error("AI batch translation error:", error);
return NextResponse.json(
{error: String(error)},
{status: 500}
);
}
// 检查 API Key
if (!process.env.AI_API_KEY) {
return NextResponse.json({ error: "AI 配置未完成,请先在设置中配置 AI" }, { status: 500 });
}
const results = await batchTranslateWithAI(words);
// 转换为响应格式
const translations: Record<string, string | null> = {};
for (const word of words) {
translations[word] = results.get(word) || null;
}
return NextResponse.json({
translations,
total: words.length,
success: results.size,
});
} catch (error) {
console.error("AI batch translation error:", error);
return NextResponse.json(
{ error: String(error) },
{ status: 500 }
);
}
}
+35 -35
View File
@@ -1,48 +1,48 @@
import { NextRequest, NextResponse } from "next/server";
import { getTranslation, getTranslations } from "@/lib/database";
import {NextRequest, NextResponse} from "next/server";
import {getTranslation, getTranslations} from "@/lib/database";
// 单个翻译查询
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const chineseName = searchParams.get("name");
const {searchParams} = new URL(request.url);
const chineseName = searchParams.get("name");
if (!chineseName) {
return NextResponse.json({ error: "缺少 name 参数" }, { status: 400 });
}
if (!chineseName) {
return NextResponse.json({error: "缺少 name 参数"}, {status: 400});
}
const translation = await getTranslation(chineseName);
const translation = await getTranslation(chineseName);
return NextResponse.json({
chinese: chineseName,
english: translation,
found: translation !== null,
});
return NextResponse.json({
chinese: chineseName,
english: translation,
found: translation !== null,
});
}
// 批量翻译查询
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { names } = body as { names: string[] };
try {
const body = await request.json();
const {names} = body as { names: string[] };
if (!names || !Array.isArray(names)) {
return NextResponse.json({ error: "缺少 names 参数(数组)" }, { status: 400 });
if (!names || !Array.isArray(names)) {
return NextResponse.json({error: "缺少 names 参数(数组)"}, {status: 400});
}
const translations = await getTranslations(names);
const result: Record<string, string | null> = {};
for (const name of names) {
result[name] = translations.get(name) || null;
}
return NextResponse.json({
translations: result,
total: names.length,
found: translations.size,
});
} catch (error) {
console.error("Translation API error:", error);
return NextResponse.json({error: "请求格式错误"}, {status: 400});
}
const translations = await getTranslations(names);
const result: Record<string, string | null> = {};
for (const name of names) {
result[name] = translations.get(name) || null;
}
return NextResponse.json({
translations: result,
total: names.length,
found: translations.size,
});
} catch (error) {
console.error("Translation API error:", error);
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
}
}
+56 -109
View File
@@ -1,34 +1,35 @@
"use client";
import {useState, useCallback, useEffect} from "react";
import {useCallback, useEffect, useState} from "react";
import {
Upload,
Sun,
AlertCircle,
Apple,
UtensilsCrossed,
CheckCircle2,
Cookie,
Download,
Heart,
Languages,
CheckCircle2,
Loader2,
Settings,
Sun,
Upload,
UtensilsCrossed,
X,
AlertCircle,
Download, Settings,
} from "lucide-react";
import Link from 'next/link'
import {Button} from "@/components/ui/button";
import {
AlignmentType,
Document,
Packer,
Table,
TableRow,
TableCell,
TextRun,
Paragraph,
AlignmentType,
WidthType,
PageOrientation,
TableLayoutType
Paragraph,
Table,
TableCell,
TableLayoutType,
TableRow,
TextRun,
WidthType
} from "docx";
// 餐食类型
@@ -228,7 +229,7 @@ export default function RecipeTranslationPage() {
orientation: "landscape",
secondPageHeader: false,
};
const { untranslatedColor, orientation, secondPageHeader } = exportConfig;
const {untranslatedColor, orientation, secondPageHeader} = exportConfig;
// 2. 收集所有需要翻译的内容(星期、餐食类型、菜品)
const allNames: string[] = [];
@@ -343,7 +344,7 @@ export default function RecipeTranslationPage() {
const isLandscape = orientation === "landscape";
const pageWidth = isLandscape ? 11906 : 16838;
const pageHeight = isLandscape ? 16838 : 11906;
// 构建表头行
const buildHeaderRow = (): TableRow => {
const headerCells: TableCell[] = [];
@@ -374,102 +375,48 @@ export default function RecipeTranslationPage() {
})
);
}
return new TableRow({children: headerCells, tableHeader: true});
return new TableRow({ children: headerCells, tableHeader: false });
};
// 估算每页能放多少天(横向 A4 约 27cm 高度,减去标题和边距约 25cm,每行约 1cm)
const daysPerPage = 5;
// 构建 sections
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sections: any[] = [];
if (secondPageHeader && dayRows.length > daysPerPage) {
// 分页模式:每页有表头
const pageCount = Math.ceil(dayRows.length / daysPerPage);
for (let page = 0; page < pageCount; page++) {
const startIdx = page * daysPerPage;
const endIdx = Math.min(startIdx + daysPerPage, dayRows.length);
const pageRows = dayRows.slice(startIdx, endIdx);
sections.push({
properties: {
page: {
size: {
orientation: isLandscape ? PageOrientation.LANDSCAPE : PageOrientation.PORTRAIT,
width: pageWidth,
height: pageHeight,
},
margin: {
top: 700,
bottom: 700,
left: 900,
right: 900,
},
},
sections.push({
properties: {
page: {
size: {
orientation: isLandscape ? PageOrientation.LANDSCAPE : PageOrientation.PORTRAIT,
width: pageWidth,
height: pageHeight,
},
children: [
...(page === 0 ? [
new Paragraph({
children: [
new TextRun({
text: t(data.title?.zh || "食谱") || data.title?.zh || "食谱",
bold: true,
size: 32,
}),
],
alignment: AlignmentType.CENTER,
}),
new Paragraph({children: []}),
] : []),
new Table({
rows: [buildHeaderRow(), ...pageRows],
width: {size: 100, type: WidthType.PERCENTAGE},
layout: TableLayoutType.FIXED,
}),
],
});
}
} else {
// 普通模式:单表格无分页表头
sections.push({
properties: {
page: {
size: {
orientation: isLandscape ? PageOrientation.LANDSCAPE : PageOrientation.PORTRAIT,
width: pageWidth,
height: pageHeight,
},
margin: {
top: 700,
bottom: 700,
left: 900,
right: 900,
},
margin: {
top: 700,
bottom: 700,
left: 900,
right: 900,
},
},
children: [
new Paragraph({
children: [
new TextRun({
text: t(data.title?.zh || "食谱") || data.title?.zh || "食谱",
bold: true,
size: 32,
}),
],
alignment: AlignmentType.CENTER,
}),
new Paragraph({children: []}),
new Table({
rows: [buildHeaderRow(), ...dayRows],
width: {size: 100, type: WidthType.PERCENTAGE},
layout: TableLayoutType.FIXED,
}),
],
});
}
},
children: [
new Paragraph({
children: [
new TextRun({
text: `${fileName || data.title?.zh || "食谱翻译"}`,
bold: true,
size: 32,
}),
],
alignment: AlignmentType.CENTER,
}),
new Paragraph({children: []}),
new Table({
rows: [buildHeaderRow(), ...dayRows],
width: {size: 100, type: WidthType.PERCENTAGE},
layout: TableLayoutType.FIXED
}),
],
});
const doc = new Document({
sections,
});
@@ -755,7 +702,7 @@ export default function RecipeTranslationPage() {
key={dish.id}
className="flex flex-col gap-0.5 text-sm"
>
<span style={{ color: dish.name.en ? undefined : exportConfig.untranslatedColor }}>
<span style={{color: dish.name.en ? undefined : exportConfig.untranslatedColor}}>
{dish.name.zh}
</span>
{dish.name.en && (
@@ -788,7 +735,7 @@ export default function RecipeTranslationPage() {
key={dish.id}
className="flex flex-col gap-0.5"
>
<span style={{ color: dish.name.en ? undefined : exportConfig.untranslatedColor }}>
<span style={{color: dish.name.en ? undefined : exportConfig.untranslatedColor}}>
{dish.name.zh}
</span>
{dish.name.en && (
+4 -24
View File
@@ -1,19 +1,17 @@
"use client";
import { useState, useEffect } from "react";
import { useToast } from "@/components/ui/toast";
import { Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import {useEffect, useState} from "react";
import {useToast} from "@/components/ui/toast";
import {Loader2} from "lucide-react";
import {Button} from "@/components/ui/button";
interface ExportConfig {
untranslatedColor: string;
secondPageHeader: boolean;
orientation: "portrait" | "landscape";
}
const defaultExportConfig: ExportConfig = {
untranslatedColor: "CC0000",
secondPageHeader: false,
orientation: "landscape",
};
@@ -30,7 +28,6 @@ export default function ExportPage() {
if (result.success && result.data) {
setConfig({
untranslatedColor: result.data.export?.untranslatedColor || defaultExportConfig.untranslatedColor,
secondPageHeader: result.data.export?.secondPageHeader ?? defaultExportConfig.secondPageHeader,
orientation: result.data.export?.orientation || defaultExportConfig.orientation,
});
}
@@ -136,19 +133,6 @@ export default function ExportPage() {
</label>
</div>
</div>
<div className="space-y-2">
<label className="flex items-center gap-2 cursor-pointer">
<input
type="checkbox"
checked={config.secondPageHeader}
onChange={(e) => setConfig({ ...config, secondPageHeader: e.target.checked })}
className="h-4 w-4 cursor-pointer rounded border-input"
/>
<span className="text-sm"></span>
</label>
<p className="text-xs text-muted-foreground"></p>
</div>
</div>
{/* 预览 */}
@@ -169,10 +153,6 @@ export default function ExportPage() {
<span className="text-sm"></span>
<span className="text-sm">{config.orientation === "landscape" ? "横向 (Landscape)" : "纵向 (Portrait)"}</span>
</div>
<div className="flex items-center gap-2">
<span className="text-sm"></span>
<span className="text-sm">{config.secondPageHeader ? "启用" : "禁用"}</span>
</div>
</div>
</div>
</div>