/** * 图案导出路由(T-076 + T-077):PDF 图纸生成 + 珠子用量统计。 * 纯计算模块,不依赖数据库。 * * POST /api/export/pdf { title?, gridSize, grid, palette } → application/pdf(附件下载) * POST /api/export/stats { palette, totalBeads } → 200 { stats } */ import { Router } from "express"; import type { Request, Response } from "express"; import { computeUsageStats } from "./usage-stats.js"; import { generatePatternPdf } from "./pdf.js"; const MAX_GRID_SIZE = 512; /** * 校验并标准化色板为 { code, hex, count } 列表; * 任一元素缺 code/hex(非字符串)视为整体非法,count 缺失时按 0 处理。 */ function parsePalette(raw: unknown): { code: string; hex: string; count: number }[] | null { if (!Array.isArray(raw)) return null; const out: { code: string; hex: string; count: number }[] = []; for (const item of raw) { if (typeof item !== "object" || item === null) return null; const rec = item as Record; if (typeof rec.code !== "string" || typeof rec.hex !== "string") return null; const count = typeof rec.count === "number" && Number.isFinite(rec.count) ? Math.max(0, Math.floor(rec.count)) : 0; out.push({ code: rec.code, hex: rec.hex, count }); } return out; } /** 创建图案导出路由(无状态,无需注入数据库) */ export function createExportRouter(): Router { const router = Router(); /** * POST /api/export/pdf * 请求: { title?, gridSize, grid: { color }[], palette: { code, hex, count }[] } * 响应: 200 application/pdf(Content-Disposition: attachment) * 400 中文参数提示 / 500 服务器内部错误 */ router.post("/pdf", (req: Request, res: Response): void => { try { const body = (req.body ?? {}) as Record; const title = typeof body.title === "string" ? body.title : ""; const gridSize = Number(body.gridSize); if (!Number.isInteger(gridSize) || gridSize < 1 || gridSize > MAX_GRID_SIZE) { res.status(400).json({ error: "网格尺寸无效" }); return; } const grid = body.grid; if (!Array.isArray(grid) || grid.length !== gridSize * gridSize) { res.status(400).json({ error: "网格数据与尺寸不匹配" }); return; } const cells: { color: string }[] = grid.map((item) => { if (typeof item !== "object" || item === null) return { color: "" }; const rec = item as Record; return { color: typeof rec.color === "string" ? rec.color : "" }; }); const palette = parsePalette(body.palette); if (palette === null) { res.status(400).json({ error: "色板数据无效" }); return; } // 珠子总数 = 网格中实际填色的格子数 const totalBeads = cells.filter((c) => c.color.trim() !== "").length; const stats = computeUsageStats(palette, totalBeads); const pdf = generatePatternPdf({ title, gridSize, grid: cells, stats }); res .status(200) .setHeader("Content-Type", "application/pdf") .setHeader("Content-Disposition", 'attachment; filename="perler-pattern.pdf"') .send(pdf); } catch (err) { console.error("POST /api/export/pdf error:", err); res.status(500).json({ error: "服务器内部错误,请稍后重试" }); } }); /** * POST /api/export/stats * 请求: { palette: { code, hex, count }[], totalBeads: number } * 响应: 200 { stats } / 400 中文参数提示 / 500 服务器内部错误 */ router.post("/stats", (req: Request, res: Response): void => { try { const body = (req.body ?? {}) as Record; const palette = parsePalette(body.palette); if (palette === null) { res.status(400).json({ error: "色板数据无效" }); return; } const totalBeads = Number(body.totalBeads); if (!Number.isInteger(totalBeads) || totalBeads < 0) { res.status(400).json({ error: "珠子总数无效" }); return; } res.status(200).json({ stats: computeUsageStats(palette, totalBeads) }); } catch (err) { console.error("POST /api/export/stats error:", err); res.status(500).json({ error: "服务器内部错误,请稍后重试" }); } }); return router; }