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:
2026-09-08 23:18:53 +08:00
parent 76ba61d5bb
commit 6e1d2c6728
9 changed files with 2110 additions and 611 deletions
+42
View File
@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from "next/server";
export const runtime = "nodejs";
/**
* 微信 CDN 图片代理(仅用于本地预览)。
* mmbiz.qpic.cn 有防盗链: 带非微信域名 Referer 会返回占位图。
* 服务端 fetch 默认不带 Referer, 可拿到真图。仅允许代理微信图片域名。
*/
const ALLOWED_HOST_RE = /\.qpic\.cn$/;
export async function GET(req: NextRequest) {
const u = req.nextUrl.searchParams.get("u");
if (!u) return new NextResponse("missing u", { status: 400 });
let target: URL;
try {
target = new URL(u);
} catch {
return new NextResponse("bad url", { status: 400 });
}
if (target.protocol !== "https:" || !ALLOWED_HOST_RE.test(target.hostname)) {
return new NextResponse("host not allowed", { status: 403 });
}
try {
const upstream = await fetch(target.toString(), {
headers: { "User-Agent": "Mozilla/5.0", Accept: "image/*" },
cache: "no-store",
});
if (!upstream.ok || !upstream.body) {
return new NextResponse("upstream error", { status: 502 });
}
return new NextResponse(upstream.body, {
status: 200,
headers: {
"Content-Type": upstream.headers.get("content-type") ?? "image/jpeg",
"Cache-Control": "public, max-age=86400",
},
});
} catch {
return new NextResponse("fetch failed", { status: 502 });
}
}
+51
View File
@@ -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 });
}
}