feat: 新增135编辑器转换子功能
- 新增 /editor135 子页面:上传幼儿园食谱docx → 解析并填充135模板 → 预览/源代码/解析结果三视图 - 新增 /api/editor135/render 解析接口(jsdom 解析填充,逐条回读校验 mismatch) - 新增 /api/editor135/img 微信CDN图片代理(解决预览防盗链,仅允许 *.qpic.cn) - 缺失星期自动删除:docx 缺某天(含周末/间隔天)时整段移除该天内容,尾部三审三校/温馨提示保留 - 逐节点精确区间删除算法:处理相邻天共用容器、嵌套结构,不误删保留天 - 新增依赖 jsdom + @types/jsdom;附 6 场景回归测试脚本 scripts/test-remove-days.ts - 首页 header 增加「135编辑器转换」导航入口
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { parseDocxToRecipe, fillTemplate, extractTemplateRecipe, type Recipe } from "@/lib/editor135-core";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
function loadTemplate(): string {
|
||||
// 优先取项目根目录可替换的外部模板, 其次内置模板
|
||||
const external = path.join(process.cwd(), "template135.html");
|
||||
if (fs.existsSync(external)) return fs.readFileSync(external, "utf8");
|
||||
return fs.readFileSync(path.join(process.cwd(), "public", "template135.html"), "utf8");
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const form = await req.formData();
|
||||
const file = form.get("file");
|
||||
if (!(file instanceof File)) {
|
||||
return NextResponse.json({ error: "请上传文件(表单字段名 file)" }, { status: 400 });
|
||||
}
|
||||
if (!/\.docx$/i.test(file.name)) {
|
||||
return NextResponse.json({ error: "仅支持 .docx 文件" }, { status: 400 });
|
||||
}
|
||||
const buf = Buffer.from(await file.arrayBuffer());
|
||||
const recipe: Recipe = await parseDocxToRecipe(buf);
|
||||
if (!recipe.days.length) {
|
||||
return NextResponse.json(
|
||||
{ error: "未能从文档中识别出食谱(需要包含 星期一~星期五 与 早餐/早点/午餐/体弱儿餐/午点 等关键词)" },
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
const template = loadTemplate();
|
||||
const templateRecipe = extractTemplateRecipe(template);
|
||||
const { html, report } = fillTemplate(template, recipe);
|
||||
const totalItems = recipe.days.reduce((n, d) => n + d.meals.reduce((k, m) => k + m.items.length, 0), 0);
|
||||
return NextResponse.json({
|
||||
fileName: file.name,
|
||||
recipe, report, html,
|
||||
summary: {
|
||||
days: recipe.days.length,
|
||||
totalItems,
|
||||
templateDays: templateRecipe.days.length,
|
||||
templateItems: templateRecipe.days.reduce((n, d) => n + d.meals.reduce((k, m) => k + m.items.length, 0), 0),
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
return NextResponse.json({ error: `解析失败: ${msg}` }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user