feat: 增强功能批量入库 - 双色彩引擎/抖动/杂色清理/品牌色板/PDF导出/编辑工具/分享/i18n 等验收代码
This commit is contained in:
1 parent
02762400fb
commit
f846fabfc6
77 files changed
+11372
-226
No files matched your search
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* 品牌色板元数据 API(palette API)。
|
||||
*
|
||||
* 设计说明:
|
||||
* 1. 后端只嵌入「最小品牌注册表」(品牌 id / 名称 / 色号条数),
|
||||
* 不复制 291 标准色的完整映射数据 —— 完整数据(hex/rgb/lab + 各品牌色号)
|
||||
* 在前端 perler-lab/src/lib/brand-palettes.ts。需要全量数据时请同步该前端模块。
|
||||
* 2. 色号列表在模块加载时从参考 CSV 读取一次(非每请求读取),按品牌列
|
||||
* 派生出去重、排序的色号数组;列头(品牌名)为 UTF-8 中文。
|
||||
* 参考文件:D:\拼豆\competitor-analysis\palettes\zippland-品牌色号对应表.csv
|
||||
* 容器等不含该 CSV 的环境可用环境变量 PALETTES_CSV_PATH 指定路径;
|
||||
* 读取失败时告警并回退为空列表(不阻断服务启动)。
|
||||
* 3. colorCount = 该品牌在 291 标准色映射中的色号条数("-" 无对应色号的行不计),
|
||||
* 与前端实测一致:MARD 291 / COCO 291 / 漫漫 290 / 盼盼 291 / 咪小窝 291。
|
||||
* 注意:漫漫 的映射含一个重复色号(S4),去重后实际列表为 289 条。
|
||||
*/
|
||||
|
||||
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";
|
||||
|
||||
/** 品牌元信息(最小注册表,与前端 BRANDS 的 id/name 对齐)。 */
|
||||
export interface BrandMeta {
|
||||
id: string;
|
||||
name: string;
|
||||
colorCount: number;
|
||||
}
|
||||
|
||||
/** 品牌注册表:id/name 取自前端 src/lib/brand-palettes.ts 的 BRANDS。 */
|
||||
export const BRANDS: readonly BrandMeta[] = [
|
||||
{ id: "MARD", name: "MARD 玛德", colorCount: 291 },
|
||||
{ id: "COCO", name: "COCO", colorCount: 291 },
|
||||
{ id: "漫漫", name: "漫漫", colorCount: 290 },
|
||||
{ id: "盼盼", name: "盼盼", colorCount: 291 },
|
||||
{ id: "咪小窝", name: "咪小窝", colorCount: 291 },
|
||||
];
|
||||
|
||||
/** 参考 CSV 默认路径:仓库根目录 D:\拼豆\competitor-analysis\palettes\。 */
|
||||
const DEFAULT_CSV_PATH = resolve(
|
||||
dirname(fileURLToPath(import.meta.url)),
|
||||
"../../..",
|
||||
"competitor-analysis",
|
||||
"palettes",
|
||||
"zippland-品牌色号对应表.csv",
|
||||
);
|
||||
|
||||
/**
|
||||
* 读取参考 CSV(模块加载时执行一次),按品牌 id 派生出
|
||||
* 「去重、排序」的色号列表("-" 与空单元格视为无对应色号,跳过)。
|
||||
*/
|
||||
function loadCodesByBrand(): ReadonlyMap<string, string[]> {
|
||||
const csvPath = process.env["PALETTES_CSV_PATH"] ?? DEFAULT_CSV_PATH;
|
||||
let text: string;
|
||||
try {
|
||||
text = readFileSync(csvPath, "utf8");
|
||||
} catch (err) {
|
||||
// 容器镜像不含参考 CSV:告警并回退为空列表,保持服务可启动。
|
||||
console.warn(`[palettes] 无法读取品牌色号 CSV(${csvPath}):${(err as Error).message}`);
|
||||
return new Map<string, string[]>();
|
||||
}
|
||||
|
||||
const lines = text
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0);
|
||||
if (lines.length === 0) return new Map<string, string[]>();
|
||||
|
||||
const headers = lines[0]!.split(",");
|
||||
const raw = new Map<string, Set<string>>();
|
||||
for (const header of headers) {
|
||||
raw.set(header, new Set<string>());
|
||||
}
|
||||
for (const line of lines.slice(1)) {
|
||||
const cells = line.split(",");
|
||||
headers.forEach((header, index) => {
|
||||
const code = cells[index]?.trim() ?? "";
|
||||
if (code !== "" && code !== "-") {
|
||||
raw.get(header)?.add(code);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const result = new Map<string, string[]>();
|
||||
for (const [header, codes] of raw) {
|
||||
result.set(header, [...codes].sort());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 模块加载时读取一次(见任务要求:Read CSV at module load, not per-request)。 */
|
||||
const CODES_BY_BRAND: ReadonlyMap<string, string[]> = loadCodesByBrand();
|
||||
|
||||
/**
|
||||
* 创建 palette 路由。
|
||||
*
|
||||
* GET /api/palettes
|
||||
* 200 → [{ id, name, colorCount }](5 个品牌)
|
||||
*
|
||||
* GET /api/palettes/:brandId/codes
|
||||
* 200 → string[](该品牌色号列表,去重、排序)
|
||||
* 404 → { error: "品牌不存在" }
|
||||
*/
|
||||
export function createPalettesRouter(): Router {
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (_req: Request, res: Response): void => {
|
||||
res.status(200).json(
|
||||
BRANDS.map(({ id, name, colorCount }) => ({ id, name, colorCount })),
|
||||
);
|
||||
});
|
||||
|
||||
router.get("/:brandId/codes", (req: Request, res: Response): void => {
|
||||
const raw = req.params["brandId"];
|
||||
const brandId = typeof raw === "string" ? raw : "";
|
||||
const codes = CODES_BY_BRAND.get(brandId);
|
||||
if (codes === undefined) {
|
||||
res.status(404).json({ error: "品牌不存在" });
|
||||
return;
|
||||
}
|
||||
res.status(200).json(codes);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user