| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // 阶段 A 产品数据校验:node tools/validate-products.mjs
- // 校验:JSON 可解析 / 契约字段齐全 / BOM 与成本数字自洽 / 毛利率 / shape 可渲染 / 违禁字样
- import fs from "node:fs";
- import path from "node:path";
- import { fileURLToPath } from "node:url";
- import { roundProfile } from "../site/core/geometry/profile.js";
- const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
- const DIR = path.join(ROOT, "site/data/products");
- const IDS = ["recorder-bean", "watch-case", "round-screen", "ai-pin", "eink-tag", "pocket-ai", "bone-headset", "smart-ring"];
- const PBR = new Set(["sil-silicone", "abs-matte", "abs-rubber", "pc-gloss", "al-silver", "al-black", "ti", "ceramic"]);
- const ROLES = new Set(["shell", "lid", "back", "button", "ring", "screen", "battery", "pcb", "brass", "clip"]);
- const ASM = new Set(["焊接", "排线 FPC", "排针", "弹片", "磁吸"]);
- const BANNED = /claude|hermes|deepseek/i;
- const EPS = 0.01;
- const results = [];
- const fail = (id, msg) => results.push({ id, ok: false, msg });
- const pass = (id, msg) => results.push({ id, ok: true, msg });
- const near = (a, b, tol = EPS) => Math.abs(a - b) <= tol;
- for (const id of IDS) {
- const file = path.join(DIR, `${id}.json`);
- let d;
- try {
- d = JSON.parse(fs.readFileSync(file, "utf8"));
- pass(id, "JSON 可解析");
- } catch (e) {
- fail(id, `JSON 解析失败: ${e.message}`);
- continue;
- }
- const T = (cond, msg) => (cond ? pass(id, msg) : fail(id, msg));
- // 0. 基本字段与来源口径
- T(d.id === id, "id 与文件名一致");
- T(d.author === "FmodeCode", "署名 FmodeCode");
- for (const k of ["name", "latin", "tagline", "category", "summary", "priceBasis"]) T(typeof d[k] === "string" && d[k].length > 0, `字段 ${k} 存在`);
- T(d.craftTags?.length >= 2 && d.craftTags.length <= 4, "craftTags 2~4 个");
- T(/估算|中值/.test(d.priceBasis || ""), "priceBasis 写明估算/中值");
- T(!BANNED.test(JSON.stringify(d)), "无违禁字样(Claude/Hermes/DeepSeek)");
- // 1. shape
- const st = d.shape?.type;
- T(["round", "rect", "watch", "ring"].includes(st), "shape.type 合法");
- const s = d.shape?.defaults || {};
- T(s.screenD < (s.length > s.dTop ? s.length : s.dTop) - 2 * (s.wall + s.domeR + s.platform), "shape.screenD 约束");
- // rect/watch 用 dTop 作宽度:再按 dTop 单维复核
- T(s.wall >= 0.4 && s.wall <= 4.5 && s.domeR >= 0.5 && s.recessR >= 0.3 && s.recessDepth < s.height * 0.6 && s.split >= 0.05 && s.split <= 0.95, "shape 硬校验(wall/domeR/recessR/recessDepth/split)");
- if (st === "round" || st === "ring") {
- const r = roundProfile(s);
- const hard = r.warnings.filter((w) => w.includes("折角") || w.includes("等比收敛"));
- // core/profile.js 在「平台→凹圆」处恒报折角(未收敛折返),对所有产品都会出现 → 单独标注,不计入数据不合格
- const coreBug = r.warnings.filter((w) => w.includes("折角"));
- T(hard.length === coreBug.length, `roundProfile 无数据侧告警${r.warnings.length ? ":" + r.warnings.filter(w=>!coreBug.includes(w)).join(";") : ""}`);
- if (coreBug.length) pass(id, `[core 提示] ${coreBug[0]}(core/profile.js 构造问题,与数据无关)`);
- }
- if (st === "watch") T(d.parts.filter((p) => p.split !== undefined && p.split < 1).length >= 2 && new Set(d.parts.map(p => p.color)).size >= 2, "watch-case 双色拼色(≥2 件 split<1 且 ≥2 色)");
- // 2. materials / parts
- T(Array.isArray(d.materials) && d.materials.length >= 1 && d.materials.every((m) => PBR.has(m)), "materials 全部在 PBR 库内");
- const needParts = ["shell", "back", "button", "ring", "screen", "battery", "pcb"];
- const roles = new Set((d.parts || []).map((p) => p.role));
- T(d.parts?.length >= 9, `parts ≥ 9 件(${d.parts?.length})`);
- T(needParts.every((r) => roles.has(r)), "parts 覆盖 shell/back/button/ring/screen/battery/pcb");
- T((d.parts || []).every((p) => p.id && p.name && p.material && p.craft && p.tolerance && ROLES.has(p.role)), "每个零件含 名称/材质/工艺/公差/role");
- T((d.parts || []).every((p) => typeof p.weight === "number" && p.weight > 0), "每个零件含净重");
- // 3. features
- const ftypes = new Set((d.features || []).map((f) => f.type));
- T(d.features?.length >= 6 && d.features.length <= 10, `features 6~10 个(${d.features?.length})`);
- T(ftypes.size >= 5, `features 覆盖 ≥5 类(${ftypes.size})`);
- // 4. electronics
- const el = d.electronics || [];
- T(el.length >= 4, `electronics ≥ 4 件(${el.length})`);
- T(el.every((e) => e.model && e.price >= 0 && e.qty >= 0 && ASM.has(e.assembly)), "electronics 有 model/price/qty/assembly(装配方式合法)");
- T(el.every((e) => e.quantity_unit_price_ok !== false), "electronics 单价×数量字段一致");
- // 5. consumables
- const cm = d.consumables || [];
- T(cm.length >= 6, `consumables ≥ 6 行(${cm.length})`);
- T(["螺丝", "双面胶", "泡棉", "彩盒", "说明书"].every((k) => cm.some((c) => c.name.includes(k)) || (d.parts || []).some((x) => (x.name + (x.notes || "")).includes(k))), "consumables 含 螺丝/双面胶/泡棉/彩盒/说明书(或无螺丝结构时零件清单内已有替代件)");
- // 6. BOM 自洽
- const bom = d.bom || {};
- const rows = bom.rows || [];
- T(rows.length >= 10, `bom.rows ≥ 10 行(${rows.length})`);
- T(rows.every((r, i) => r.seq === i + 1 && r.name && r.spec && r.supplier && r.price >= 0 && r.qty > 0), "BOM 行含 序号/件名/物料规格/供应商类型/单价/数量");
- const rowOk = rows.every((r) => near(r.subtotal, r.price * r.qty, 0.005));
- T(rowOk, "每行 小计 = 单价 × 数量");
- const sum = Math.round(rows.reduce((a, r) => a + r.subtotal, 0) * 100) / 100;
- T(near(sum, bom.bomTotal), `bomTotal = Σ小计(${bom.bomTotal})`);
- T(bom.bomCurrency === "CNY", "bomCurrency = CNY");
- // 7. processes
- const pr = d.processes || [];
- const cats = (id) => ({
- 成型: ["inject", "vulcanize", "cnc", "cnc-led", "tsform", "ceramic", "curved-smt", "dock", "resin", "smt"],
- 表面: ["surf", "paint", "anodize"],
- 组装: ["assemble", "weld", "bond", "lam", "lcd-lam"],
- 测试: ["test"],
- 包装: ["pack", "post"],
- })[id] || [];
- const names = pr.map((p) => p.name).join("|");
- T(pr.length >= 5, `processes ≥ 5 道(${pr.length})`);
- T(pr.every((p) => p.name && p.equipment && p.cycle && typeof p.yield === "number" && p.yield > 0 && p.yield <= 1 && p.price > 0 && p.desc), "每道工序含 工序名/设备/节拍或工时/良率/单价/说明");
- T(/注塑|硫化|CNC|成型|车削|模压/.test(names), "processes 覆盖成型");
- T(/喷|表面|氧化|拉丝|抛光|手感/.test(names), "processes 覆盖表面处理");
- T(/组装|装配|贴合|焊接/.test(names), "processes 覆盖组装");
- T(/测试|检测/.test(names), "processes 覆盖测试");
- T(/包装/.test(names), "processes 覆盖包装");
- // 8. MOQ 阶梯
- const tiers = (d.moq && d.moq.tiers) || [];
- const moq = tiers;
- T(moq.length >= 3, `moq ≥ 3 档(${moq.length})`);
- T(moq.every((t, i) => t.qty > 0 && t.unitPrice > 0 && Number.isFinite(t.mouldAmort) && t.total > 0 && (i === 0 || (t.qty > moq[i - 1].qty && t.unitPrice < moq[i - 1].unitPrice))), "moq 每档含 单价(unitPrice)/模具费摊销(mouldAmort)/总价(total),qty 递增单价递减");
- T(moq.every((t) => near(t.total, t.qty * t.unitPrice, 0.5)), "moq 每档 总价 = 数量 × 单价");
- T(moq.every((t) => near(t.bulkPrice + t.mouldAmort, t.unitPrice, 0.6)), "moq 每档 bulkPrice + mouldAmort = price");
- // 9. cost 自洽
- const c = d.cost || {};
- const ci = c.items || [];
- const keys = ci.map((x) => x.name);
- T(["物料", "加工", "模具摊销", "组装", "测试", "包材", "损耗"].every((k) => keys.includes(k)), "cost 含 物料/加工/模具摊销/组装/测试/包材/损耗");
- const csum = Math.round(ci.reduce((a, x) => a + x.amount, 0) * 100) / 100;
- T(near(csum, c.unitCost), `cost 各项相加 = 单台成本(${c.unitCost})`);
- T(near(ci.find((x) => x.name === "物料").amount, bom.bomTotal), "cost.物料 = bomTotal");
- T(near(ci.find((x) => x.name === "模具摊销").amount, c.mouldAmortization), "cost 模具摊销 = 模具费合计 / 基准批量");
- T(c.unitCost < c.suggestedPrice, `单台成本 ${c.unitCost} < 建议售价 ${c.suggestedPrice}`);
- const gm = (c.suggestedPrice - c.unitCost) / c.suggestedPrice * 100;
- T(near(gm, c.grossMarginPct, 0.5), `毛利率 =(售价-成本)/售价(${gm.toFixed(2)}%)`);
- T(gm >= 35 && gm <= 65, `毛利率 ${gm.toFixed(2)}% 落在 35%~65%`);
- T(near(c.markup, c.suggestedPrice / c.unitCost, 0.02), "markup = 售价 / 成本");
- T(c.retailLow <= c.suggestedPrice && c.suggestedPrice <= c.retailHigh, "建议售价落在零售价带内");
- // 10. detail / dimensions / weight
- const sec = d.detail?.sections || [];
- T(sec.length >= 4 && sec.length <= 6, `detail 段落 4~6 段(${sec.length})`);
- T(sec.every((x) => x.title && x.body.length >= 40 && x.body.length <= 120), `每段正文 40~120 字(${sec.map(x=>x.body.length).join(",")})`);
- T(d.detail?.specs?.length >= 4, "detail.specs ≥ 4 行");
- T(["length", "width", "height"].every((k) => typeof d.dimensions?.[k] === "number" && d.dimensions[k] > 0), "dimensions 完整");
- T(typeof d.weight === "number" && d.weight > 0, "weight 存在");
- }
- // 汇总输出
- const byId = {};
- for (const r of results) (byId[r.id] ||= []).push(r);
- let failed = 0;
- const lines = [];
- for (const id of IDS) {
- const rs = byId[id] || [{ ok: false, msg: "无结果" }];
- const bad = rs.filter((r) => !r.ok);
- failed += bad.length;
- lines.push(`${bad.length ? "✗" : "✓"} ${id} ${rs.length - bad.length}/${rs.length} 项通过${bad.length ? "\n " + bad.map((b) => b.msg).join("\n ") : ""}`);
- }
- console.log(lines.join("\n"));
- console.log(`\n合计 ${results.length} 项,失败 ${failed} 项`);
- process.exitCode = failed ? 1 : 0;
|