feat: V1.1 批次入库——色号表SEO页/Showcase/图纸库/帮助中心/JSON-LD/CI门禁/竞品分析
This commit is contained in:
1 parent
40e2342a02
commit
f53f0a2b25
104 files changed
+21760
-157
No files matched your search
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* 色号表 SEO 页面 JSON-LD 结构化数据 API。
|
||||
*
|
||||
* 设计说明(SPA 无 SSR 的注入方案):
|
||||
* 1. 本项目是纯前端 SPA(Vite),没有 SSR 可以在页面源码中直接输出
|
||||
* <script type="application/ld+json">。因此采用「后端数据接口 + 前端注入」:
|
||||
* 后端提供 GET /api/color-chart/:brand 返回该品牌色号表的 JSON-LD
|
||||
* (schema.org CollectionPage + ItemList),前端色号表路由页加载后把
|
||||
* 返回值序列化注入 <script type="application/ld+json"> 即可让搜索引擎
|
||||
* 识别该页面的结构化数据。
|
||||
* 2. 数据来源:D:\拼豆\competitor-analysis\palettes\ 下的品牌色板 JSON
|
||||
* (只读)。mard 使用 291 标准色映射(hex → {MARD: 色号, ...}),
|
||||
* 其余品牌使用各自 {code, hex, rgb, name} 数组文件:
|
||||
* mard → zippland-291标准色映射.json
|
||||
* artkal-c → artkal-c.json
|
||||
* perler → perler.json
|
||||
* hama → hama.json
|
||||
* pyssla → ikea-pyssla.json
|
||||
* nabbi → nabbi.json
|
||||
* 3. 文件在模块加载时读取一次(非每请求读取);读取失败时告警并回退为空
|
||||
* 列表(不阻断服务启动,与 palettes 模块行为一致)。
|
||||
*/
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
/** 单个色号条目(JSON-LD ListItem 的 item 部分)。 */
|
||||
export interface ChartColor {
|
||||
code: string;
|
||||
hex: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/** 品牌色号表数据。 */
|
||||
export interface BrandChart {
|
||||
id: string;
|
||||
name: string;
|
||||
colors: ChartColor[];
|
||||
}
|
||||
|
||||
/** 品牌定义:id / 展示名 / 数据文件 / 文件类型。 */
|
||||
interface BrandDef {
|
||||
id: string;
|
||||
name: string;
|
||||
fileName: string;
|
||||
/** true 表示 hex → {品牌: 色号} 映射对象;false 表示 {code,hex,rgb,name} 数组。 */
|
||||
mapping: boolean;
|
||||
/** 映射文件中该品牌对应的键(仅 mapping=true 时使用,如 zippland 映射里是 "MARD")。 */
|
||||
mappingKey?: string;
|
||||
}
|
||||
|
||||
/** 色号表品牌注册表(前端路由 /color-chart/:brand 的 slug 对齐)。 */
|
||||
const BRAND_DEFS: readonly BrandDef[] = [
|
||||
{ id: "mard", name: "MARD 玛德", fileName: "zippland-291标准色映射.json", mapping: true, mappingKey: "MARD" },
|
||||
{ id: "artkal-c", name: "Artkal C", fileName: "artkal-c.json", mapping: false },
|
||||
{ id: "perler", name: "Perler", fileName: "perler.json", mapping: false },
|
||||
{ id: "hama", name: "Hama", fileName: "hama.json", mapping: false },
|
||||
{ id: "pyssla", name: "IKEA Pyssla", fileName: "ikea-pyssla.json", mapping: false },
|
||||
{ id: "nabbi", name: "Nabbi", fileName: "nabbi.json", mapping: false },
|
||||
];
|
||||
|
||||
/** 色板 JSON 默认目录:仓库根 D:\拼豆\competitor-analysis\palettes\。 */
|
||||
const DEFAULT_PALETTES_DIR = resolve(
|
||||
dirname(fileURLToPath(import.meta.url)),
|
||||
"../../..",
|
||||
"competitor-analysis",
|
||||
"palettes",
|
||||
);
|
||||
|
||||
/** hex 格式校验(映射文件 key)。 */
|
||||
const HEX_RE = /^#[0-9A-Fa-f]{6}$/;
|
||||
|
||||
/**
|
||||
* 从 hex→{品牌:色号} 映射对象派生品牌色号列表。
|
||||
* 仅纳入映射中存在该品牌色号、且 hex 合法的条目;按映射顺序输出。
|
||||
*/
|
||||
function deriveFromMapping(raw: unknown, mappingKey: string): ChartColor[] {
|
||||
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
||||
return [];
|
||||
}
|
||||
const colors: ChartColor[] = [];
|
||||
for (const [hex, entry] of Object.entries(raw as Record<string, Record<string, string>>)) {
|
||||
if (!HEX_RE.test(hex)) continue;
|
||||
const code = entry?.[mappingKey];
|
||||
if (typeof code === "string" && code.trim() !== "") {
|
||||
colors.push({ code: code.trim(), hex: hex.toUpperCase(), name: code.trim() });
|
||||
}
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
/** 从 {code, hex, rgb, name} 数组派生品牌色号列表。 */
|
||||
function deriveFromArray(raw: unknown): ChartColor[] {
|
||||
if (!Array.isArray(raw)) return [];
|
||||
const colors: ChartColor[] = [];
|
||||
for (const entry of raw) {
|
||||
if (typeof entry !== "object" || entry === null) continue;
|
||||
const { code, hex, name } = entry as { code?: unknown; hex?: unknown; name?: unknown };
|
||||
if (typeof code !== "string" || code.trim() === "") continue;
|
||||
if (typeof hex !== "string" || !HEX_RE.test(hex)) continue;
|
||||
const label = typeof name === "string" && name.trim() !== "" ? name.trim() : code.trim();
|
||||
colors.push({ code: code.trim(), hex: hex.toUpperCase(), name: label });
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
/** 加载单个品牌色号表(模块加载时执行一次)。 */
|
||||
function loadBrandChart(def: BrandDef): BrandChart {
|
||||
const dir = process.env["PALETTES_DIR"] ?? DEFAULT_PALETTES_DIR;
|
||||
const filePath = resolve(dir, def.fileName);
|
||||
let raw: unknown;
|
||||
try {
|
||||
raw = JSON.parse(readFileSync(filePath, "utf8"));
|
||||
} catch (err) {
|
||||
console.warn(`[color-chart] 无法读取品牌色板文件(${filePath}):${(err as Error).message}`);
|
||||
return { id: def.id, name: def.name, colors: [] };
|
||||
}
|
||||
const colors = def.mapping
|
||||
? deriveFromMapping(raw, def.mappingKey ?? def.id)
|
||||
: deriveFromArray(raw);
|
||||
return { id: def.id, name: def.name, colors };
|
||||
}
|
||||
|
||||
/** 模块加载时读取一次全部品牌色号表。 */
|
||||
const CHARTS: ReadonlyMap<string, BrandChart> = new Map(
|
||||
BRAND_DEFS.map((def) => [def.id, loadBrandChart(def)]),
|
||||
);
|
||||
|
||||
/**
|
||||
* 组装 JSON-LD:CollectionPage 包裹 ItemList。
|
||||
* - 顶层 CollectionPage:提供页面级 name / url / description。
|
||||
* - mainEntity ItemList:提供 numberOfItems 与逐项 itemListElement(ListItem,
|
||||
* 每个 item 为 Product,携带 identifier=色号、name=色名/色号、color=hex)。
|
||||
*/
|
||||
export function buildJsonLd(chart: BrandChart): Record<string, unknown> {
|
||||
return {
|
||||
"@context": "https://schema.org",
|
||||
"@type": "CollectionPage",
|
||||
name: `${chart.name} 拼豆色号表`,
|
||||
url: `/color-chart/${chart.id}`,
|
||||
description: `${chart.name} 拼豆品牌色号对照表,共 ${chart.colors.length} 个颜色。`,
|
||||
mainEntity: {
|
||||
"@type": "ItemList",
|
||||
name: `${chart.name} 拼豆色号表`,
|
||||
numberOfItems: chart.colors.length,
|
||||
itemListElement: chart.colors.map((color, index) => ({
|
||||
"@type": "ListItem",
|
||||
position: index + 1,
|
||||
item: {
|
||||
"@type": "Product",
|
||||
name: color.name,
|
||||
identifier: color.code,
|
||||
color: color.hex,
|
||||
brand: {
|
||||
"@type": "Brand",
|
||||
name: chart.name,
|
||||
},
|
||||
},
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建色号表 JSON-LD 路由。
|
||||
*
|
||||
* GET /api/color-chart/:brand
|
||||
* 200 → { "@context": "https://schema.org", "@type": "CollectionPage", ... }
|
||||
* 404 → { error: "品牌不存在" }
|
||||
*/
|
||||
export function createColorChartRouter(): Router {
|
||||
const router = Router();
|
||||
|
||||
router.get("/:brand", (req: Request, res: Response): void => {
|
||||
const raw = req.params["brand"];
|
||||
const brandId = typeof raw === "string" ? raw : "";
|
||||
const chart = CHARTS.get(brandId);
|
||||
if (chart === undefined) {
|
||||
res.status(404).json({ error: "品牌不存在" });
|
||||
return;
|
||||
}
|
||||
res.setHeader("Content-Type", "application/ld+json; charset=utf-8");
|
||||
res.status(200).json(buildJsonLd(chart));
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user