142 lines
5.6 KiB
TypeScript
142 lines
5.6 KiB
TypeScript
import type { CatalogProduct } from "./catalog-data";
|
|
|
|
// The ecommerce image archive groups products by family rather than by the
|
|
// individual model code used in the PDF catalog. These are the corresponding
|
|
// family-level main images copied into public/catalog-products/source-main.
|
|
const SOURCE_MAIN_IMAGES = {
|
|
"1004": "/catalog-products/source-main/1004-main.png",
|
|
"258": "/catalog-products/source-main/258-main.jpg",
|
|
"259": "/catalog-products/source-main/259-main.jpg",
|
|
"266": "/catalog-products/source-main/266-main.jpg",
|
|
"267": "/catalog-products/source-main/267-main.jpg",
|
|
"268": "/catalog-products/source-main/268-main.jpg",
|
|
"270": "/catalog-products/source-main/270-main.jpg",
|
|
"271": "/catalog-products/source-main/271-main.jpg",
|
|
"272": "/catalog-products/source-main/272-main.jpg",
|
|
"273": "/catalog-products/source-main/273-main.jpg",
|
|
"274": "/catalog-products/source-main/274-main.jpg",
|
|
"275": "/catalog-products/source-main/275-main.jpg",
|
|
"276": "/catalog-products/source-main/276-main.jpg",
|
|
"308": "/catalog-products/source-main/308-main.png",
|
|
"309": "/catalog-products/source-main/309-main.png",
|
|
"310": "/catalog-products/source-main/310-main.png",
|
|
"311": "/catalog-products/source-main/311-main.png",
|
|
"313": "/catalog-products/source-main/313-main.png",
|
|
} as const;
|
|
|
|
const SOURCE_SIZE_IMAGES = {
|
|
"1004": "/catalog-products/size-reference/1004-size.png",
|
|
"258": "/catalog-products/size-reference/258-size.jpg",
|
|
"259": "/catalog-products/size-reference/259-size.jpg",
|
|
"266": "/catalog-products/size-reference/266-size.jpg",
|
|
"267": "/catalog-products/size-reference/267-size.jpg",
|
|
"268": "/catalog-products/size-reference/268-size.jpg",
|
|
"270": "/catalog-products/size-reference/270-size.jpg",
|
|
"271": "/catalog-products/size-reference/271-size.jpg",
|
|
"272": "/catalog-products/size-reference/272-size.jpg",
|
|
"273": "/catalog-products/size-reference/273-size.jpg",
|
|
"274": "/catalog-products/size-reference/274-size.jpg",
|
|
"275": "/catalog-products/size-reference/275-size.jpg",
|
|
"276": "/catalog-products/size-reference/276-size.jpg",
|
|
"308": "/catalog-products/size-reference/308-size.png",
|
|
"309": "/catalog-products/size-reference/309-size.png",
|
|
"310": "/catalog-products/size-reference/310-size.png",
|
|
"311": "/catalog-products/size-reference/311-size.png",
|
|
"313": "/catalog-products/size-reference/313-size.png",
|
|
"563": "/catalog-products/size-reference/563-size.png",
|
|
"565": "/catalog-products/size-reference/565-size.png",
|
|
"566": "/catalog-products/size-reference/566-size.png",
|
|
"567": "/catalog-products/size-reference/567-size.png",
|
|
"568": "/catalog-products/size-reference/568-size.png",
|
|
"572": "/catalog-products/size-reference/572-size.png",
|
|
} as const;
|
|
|
|
type ProductImageSet = Record<string, string>;
|
|
type SourceImageKey = keyof typeof SOURCE_MAIN_IMAGES;
|
|
|
|
function hasWeight(name: string, grams: string) {
|
|
return new RegExp(`${grams}\\s*(?:g|克)`, "i").test(name);
|
|
}
|
|
|
|
function getGearSeries(name: string) {
|
|
if (/半金属/.test(name)) return "semi";
|
|
if (/全金属|金属/.test(name)) return "metal";
|
|
if (/塑胶|塑料/.test(name)) return "plastic";
|
|
return null;
|
|
}
|
|
|
|
function getSmallServoImage(name: string, images: ProductImageSet): string | null {
|
|
if (hasWeight(name, "1\\.7")) return images["1004"];
|
|
|
|
const gearSeries = getGearSeries(name);
|
|
if (hasWeight(name, "2") && gearSeries === "plastic") return images["272"];
|
|
if (hasWeight(name, "3\\.7") && gearSeries === "plastic") return images["271"];
|
|
|
|
if (hasWeight(name, "9")) {
|
|
if (gearSeries === "metal") return images["266"];
|
|
if (gearSeries === "semi") return images["259"];
|
|
if (gearSeries === "plastic") return images["258"];
|
|
}
|
|
|
|
if (hasWeight(name, "17")) {
|
|
if (gearSeries === "metal") return images["274"];
|
|
if (gearSeries === "plastic") return images["273"];
|
|
}
|
|
|
|
if (hasWeight(name, "25")) {
|
|
if (gearSeries === "metal") return images["276"];
|
|
if (gearSeries === "plastic") return images["275"];
|
|
}
|
|
|
|
if (hasWeight(name, "37")) {
|
|
if (gearSeries === "metal") return images["270"];
|
|
if (gearSeries === "semi") return images["268"];
|
|
if (gearSeries === "plastic") return images["267"];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function getBrickServoImage(name: string, images: ProductImageSet): string | null {
|
|
if (name.includes("3x3x3")) return images["311"];
|
|
if (name.includes("3x3x7")) return images["310"];
|
|
if (name.includes("3x7x5")) return images["309"];
|
|
if (name.includes("6x6x5")) return images["308"];
|
|
if (name.includes("11x5x4")) return images["313"];
|
|
return null;
|
|
}
|
|
|
|
export function getCatalogProductImage(product: CatalogProduct) {
|
|
if (product.category === "积木舵机") {
|
|
return getBrickServoImage(product.name, SOURCE_MAIN_IMAGES) ?? product.image;
|
|
}
|
|
|
|
if (product.category === "标准舵机" || product.category === "异形舵机") {
|
|
return getSmallServoImage(product.name, SOURCE_MAIN_IMAGES) ?? product.image;
|
|
}
|
|
|
|
return product.image;
|
|
}
|
|
|
|
export function getCatalogProductDimensionImage(product: CatalogProduct) {
|
|
if (product.category === "精密齿轮配件") {
|
|
return product.dimensionImage ?? null;
|
|
}
|
|
|
|
if (product.category === "舵盘配件") {
|
|
return SOURCE_SIZE_IMAGES[product.id.replace("shop-", "")] ?? null;
|
|
}
|
|
|
|
if (product.category === "积木舵机") {
|
|
return getBrickServoImage(product.name, SOURCE_SIZE_IMAGES);
|
|
}
|
|
|
|
if (product.category === "标准舵机" || product.category === "异形舵机") {
|
|
return getSmallServoImage(product.name, SOURCE_SIZE_IMAGES);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export const CATALOG_SOURCE_IMAGE_KEYS: SourceImageKey[] = Object.keys(SOURCE_MAIN_IMAGES) as SourceImageKey[];
|