feat: 作品页支持模块化编排与自定义内容模块
This commit is contained in:
@@ -2,3 +2,4 @@ export * from './enums';
|
||||
export * from './error-codes';
|
||||
export * from './response';
|
||||
export * from './constants';
|
||||
export * from './trace-modules';
|
||||
|
||||
@@ -0,0 +1,365 @@
|
||||
export const TRACE_LAYOUT_VERSION = 1 as const;
|
||||
export const MAX_TRACE_MODULES = 32;
|
||||
export const MAX_TRACE_MODULE_TITLE_LENGTH = 120;
|
||||
export const MAX_TRACE_MODULE_BODY_LENGTH = 5000;
|
||||
export const MAX_TRACE_MODULE_MEDIA = 30;
|
||||
|
||||
export const TRACE_MODULE_TYPES = [
|
||||
'HERO_CAROUSEL',
|
||||
'AUTHENTICITY_STATUS',
|
||||
'PRODUCT_INTRO',
|
||||
'SHOWCASE',
|
||||
'CERTIFICATES',
|
||||
'TIMELINE',
|
||||
'BLOCKCHAIN',
|
||||
'IMAGE_TEXT',
|
||||
'HORIZONTAL_GALLERY',
|
||||
] as const;
|
||||
|
||||
export type TraceModuleType = (typeof TRACE_MODULE_TYPES)[number];
|
||||
|
||||
export const SYSTEM_TRACE_MODULE_TYPES = [
|
||||
'HERO_CAROUSEL',
|
||||
'AUTHENTICITY_STATUS',
|
||||
'PRODUCT_INTRO',
|
||||
'SHOWCASE',
|
||||
'CERTIFICATES',
|
||||
'TIMELINE',
|
||||
'BLOCKCHAIN',
|
||||
] as const satisfies readonly TraceModuleType[];
|
||||
|
||||
export type SystemTraceModuleType = (typeof SYSTEM_TRACE_MODULE_TYPES)[number];
|
||||
export type CustomTraceModuleType = Exclude<TraceModuleType, SystemTraceModuleType>;
|
||||
export type TraceImagePosition = 'TOP' | 'BOTTOM' | 'LEFT' | 'RIGHT';
|
||||
|
||||
export const SYSTEM_TRACE_MODULE_IDS: Record<SystemTraceModuleType, string> = {
|
||||
HERO_CAROUSEL: 'hero',
|
||||
AUTHENTICITY_STATUS: 'authenticity',
|
||||
PRODUCT_INTRO: 'product-intro',
|
||||
SHOWCASE: 'showcase',
|
||||
CERTIFICATES: 'certificates',
|
||||
TIMELINE: 'timeline',
|
||||
BLOCKCHAIN: 'blockchain',
|
||||
};
|
||||
|
||||
export interface TraceModuleMedia {
|
||||
key: string;
|
||||
name?: string;
|
||||
type?: 'image';
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface TraceImageTextConfig {
|
||||
image?: TraceModuleMedia;
|
||||
heading?: string;
|
||||
body?: string;
|
||||
imagePosition?: TraceImagePosition;
|
||||
}
|
||||
|
||||
export interface TraceHorizontalGalleryConfig {
|
||||
items?: TraceModuleMedia[];
|
||||
}
|
||||
|
||||
export interface TraceModule {
|
||||
id: string;
|
||||
type: TraceModuleType;
|
||||
visible: boolean;
|
||||
title?: string;
|
||||
config?: TraceImageTextConfig | TraceHorizontalGalleryConfig;
|
||||
}
|
||||
|
||||
export interface TraceLayout {
|
||||
version: typeof TRACE_LAYOUT_VERSION;
|
||||
modules: TraceModule[];
|
||||
}
|
||||
|
||||
export interface TraceLayoutValidationOptions {
|
||||
template?: boolean;
|
||||
}
|
||||
|
||||
const TRACE_TYPE_SET = new Set<string>(TRACE_MODULE_TYPES);
|
||||
const SYSTEM_TYPE_SET = new Set<string>(SYSTEM_TRACE_MODULE_TYPES);
|
||||
const IMAGE_POSITIONS = new Set<string>(['TOP', 'BOTTOM', 'LEFT', 'RIGHT']);
|
||||
const MODULE_KEYS = new Set(['id', 'type', 'visible', 'title', 'config']);
|
||||
const MEDIA_KEYS = new Set(['key', 'name', 'type', 'description']);
|
||||
const IMAGE_TEXT_CONFIG_KEYS = new Set(['image', 'heading', 'body', 'imagePosition']);
|
||||
const GALLERY_CONFIG_KEYS = new Set(['items']);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function unknownKeys(value: Record<string, unknown>, allowed: Set<string>): string[] {
|
||||
return Object.keys(value).filter((key) => !allowed.has(key));
|
||||
}
|
||||
|
||||
function validateOptionalString(
|
||||
value: unknown,
|
||||
path: string,
|
||||
maxLength: number,
|
||||
errors: string[],
|
||||
): void {
|
||||
if (value === undefined) return;
|
||||
if (typeof value !== 'string') {
|
||||
errors.push(`${path} 必须是字符串`);
|
||||
return;
|
||||
}
|
||||
if (value.length > maxLength) errors.push(`${path} 不能超过 ${maxLength} 个字符`);
|
||||
}
|
||||
|
||||
function validateMedia(value: unknown, path: string, errors: string[]): void {
|
||||
if (!isRecord(value)) {
|
||||
errors.push(`${path} 必须是媒体对象`);
|
||||
return;
|
||||
}
|
||||
const extras = unknownKeys(value, MEDIA_KEYS);
|
||||
if (extras.length) errors.push(`${path} 包含不支持的字段:${extras.join(', ')}`);
|
||||
if (typeof value.key !== 'string' || !value.key.trim()) errors.push(`${path}.key 不能为空`);
|
||||
validateOptionalString(value.name, `${path}.name`, 255, errors);
|
||||
validateOptionalString(value.description, `${path}.description`, 1000, errors);
|
||||
if (value.type !== undefined && value.type !== 'image') errors.push(`${path}.type 仅支持 image`);
|
||||
}
|
||||
|
||||
export function createDefaultTraceLayout(): TraceLayout {
|
||||
const types: SystemTraceModuleType[] = [
|
||||
'HERO_CAROUSEL',
|
||||
'AUTHENTICITY_STATUS',
|
||||
'PRODUCT_INTRO',
|
||||
'SHOWCASE',
|
||||
'CERTIFICATES',
|
||||
'TIMELINE',
|
||||
'BLOCKCHAIN',
|
||||
];
|
||||
return {
|
||||
version: TRACE_LAYOUT_VERSION,
|
||||
modules: types.map((type) => ({
|
||||
id: SYSTEM_TRACE_MODULE_IDS[type],
|
||||
type,
|
||||
visible: true,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function cloneTraceLayout(layout: TraceLayout): TraceLayout {
|
||||
return JSON.parse(JSON.stringify(layout)) as TraceLayout;
|
||||
}
|
||||
|
||||
export function validateTraceLayout(
|
||||
value: unknown,
|
||||
options: TraceLayoutValidationOptions = {},
|
||||
): string[] {
|
||||
const errors: string[] = [];
|
||||
if (!isRecord(value)) return ['页面模块配置必须是对象'];
|
||||
const topExtras = unknownKeys(value, new Set(['version', 'modules']));
|
||||
if (topExtras.length) errors.push(`页面模块配置包含不支持的字段:${topExtras.join(', ')}`);
|
||||
if (value.version !== TRACE_LAYOUT_VERSION) errors.push(`页面模块配置版本必须为 ${TRACE_LAYOUT_VERSION}`);
|
||||
if (!Array.isArray(value.modules)) {
|
||||
errors.push('页面模块列表必须是数组');
|
||||
return errors;
|
||||
}
|
||||
if (value.modules.length > MAX_TRACE_MODULES) {
|
||||
errors.push(`页面模块不能超过 ${MAX_TRACE_MODULES} 个`);
|
||||
}
|
||||
|
||||
const ids = new Set<string>();
|
||||
const systemTypes = new Set<string>();
|
||||
value.modules.forEach((rawModule, index) => {
|
||||
const path = `modules[${index}]`;
|
||||
if (!isRecord(rawModule)) {
|
||||
errors.push(`${path} 必须是对象`);
|
||||
return;
|
||||
}
|
||||
const extras = unknownKeys(rawModule, MODULE_KEYS);
|
||||
if (extras.length) errors.push(`${path} 包含不支持的字段:${extras.join(', ')}`);
|
||||
|
||||
const id = rawModule.id;
|
||||
if (
|
||||
typeof id !== 'string' ||
|
||||
!id.trim() ||
|
||||
id.length > 64 ||
|
||||
!/^[A-Za-z0-9_-]+$/.test(id)
|
||||
) {
|
||||
errors.push(`${path}.id 必须是 1-64 位字母、数字、下划线或连字符`);
|
||||
} else if (ids.has(id)) {
|
||||
errors.push(`${path}.id 与其他模块重复`);
|
||||
} else {
|
||||
ids.add(id);
|
||||
}
|
||||
|
||||
const type = rawModule.type;
|
||||
if (typeof type !== 'string' || !TRACE_TYPE_SET.has(type)) {
|
||||
errors.push(`${path}.type 不受支持`);
|
||||
return;
|
||||
}
|
||||
const moduleType = type as TraceModuleType;
|
||||
if (SYSTEM_TYPE_SET.has(moduleType)) {
|
||||
if (systemTypes.has(moduleType)) errors.push(`${moduleType} 系统模块只能出现一次`);
|
||||
systemTypes.add(moduleType);
|
||||
const expectedId = SYSTEM_TRACE_MODULE_IDS[moduleType as SystemTraceModuleType];
|
||||
if (id !== expectedId) errors.push(`${moduleType} 系统模块 id 必须为 ${expectedId}`);
|
||||
}
|
||||
|
||||
if (typeof rawModule.visible !== 'boolean') errors.push(`${path}.visible 必须是布尔值`);
|
||||
validateOptionalString(rawModule.title, `${path}.title`, MAX_TRACE_MODULE_TITLE_LENGTH, errors);
|
||||
|
||||
const config = rawModule.config;
|
||||
if (SYSTEM_TYPE_SET.has(moduleType)) {
|
||||
if (config !== undefined && (!isRecord(config) || Object.keys(config).length > 0)) {
|
||||
errors.push(`${path}.config 不允许保存系统业务数据`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (config !== undefined && !isRecord(config)) {
|
||||
errors.push(`${path}.config 必须是对象`);
|
||||
return;
|
||||
}
|
||||
const configRecord = isRecord(config) ? config : {};
|
||||
|
||||
if (moduleType === 'IMAGE_TEXT') {
|
||||
const configExtras = unknownKeys(configRecord, IMAGE_TEXT_CONFIG_KEYS);
|
||||
if (configExtras.length) {
|
||||
errors.push(`${path}.config 包含不支持的字段:${configExtras.join(', ')}`);
|
||||
}
|
||||
if (configRecord.image !== undefined) {
|
||||
if (options.template) errors.push(`${path}.config.image 不能保存在模板默认布局中`);
|
||||
else validateMedia(configRecord.image, `${path}.config.image`, errors);
|
||||
}
|
||||
if (options.template && (configRecord.heading || configRecord.body)) {
|
||||
errors.push(`${path}.config 的作品内容不能保存在模板默认布局中`);
|
||||
}
|
||||
validateOptionalString(
|
||||
configRecord.heading,
|
||||
`${path}.config.heading`,
|
||||
MAX_TRACE_MODULE_TITLE_LENGTH,
|
||||
errors,
|
||||
);
|
||||
validateOptionalString(
|
||||
configRecord.body,
|
||||
`${path}.config.body`,
|
||||
MAX_TRACE_MODULE_BODY_LENGTH,
|
||||
errors,
|
||||
);
|
||||
if (
|
||||
configRecord.imagePosition !== undefined &&
|
||||
(typeof configRecord.imagePosition !== 'string' ||
|
||||
!IMAGE_POSITIONS.has(configRecord.imagePosition))
|
||||
) {
|
||||
errors.push(`${path}.config.imagePosition 不受支持`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const configExtras = unknownKeys(configRecord, GALLERY_CONFIG_KEYS);
|
||||
if (configExtras.length) {
|
||||
errors.push(`${path}.config 包含不支持的字段:${configExtras.join(', ')}`);
|
||||
}
|
||||
if (configRecord.items !== undefined) {
|
||||
if (!Array.isArray(configRecord.items)) {
|
||||
errors.push(`${path}.config.items 必须是数组`);
|
||||
} else {
|
||||
if (configRecord.items.length > MAX_TRACE_MODULE_MEDIA) {
|
||||
errors.push(`${path}.config.items 不能超过 ${MAX_TRACE_MODULE_MEDIA} 张图片`);
|
||||
}
|
||||
if (options.template && configRecord.items.length > 0) {
|
||||
errors.push(`${path}.config.items 不能保存在模板默认布局中`);
|
||||
} else {
|
||||
configRecord.items.forEach((item, itemIndex) =>
|
||||
validateMedia(item, `${path}.config.items[${itemIndex}]`, errors),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
|
||||
function optionalTrimmedString(value: unknown): string | undefined {
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const normalized = value.trim();
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
function normalizeMedia(value: unknown): TraceModuleMedia | undefined {
|
||||
if (!isRecord(value) || typeof value.key !== 'string' || !value.key.trim()) return undefined;
|
||||
const media: TraceModuleMedia = { key: value.key.trim(), type: 'image' };
|
||||
const name = optionalTrimmedString(value.name);
|
||||
const description = optionalTrimmedString(value.description);
|
||||
if (name) media.name = name;
|
||||
if (description) media.description = description;
|
||||
return media;
|
||||
}
|
||||
|
||||
function normalizeValidatedTraceLayout(value: Record<string, unknown>): TraceLayout {
|
||||
const modules = (value.modules as Array<Record<string, unknown>>).map((rawModule) => {
|
||||
const type = rawModule.type as TraceModuleType;
|
||||
const module: TraceModule = {
|
||||
id: String(rawModule.id),
|
||||
type,
|
||||
visible: rawModule.visible === true,
|
||||
};
|
||||
const title = optionalTrimmedString(rawModule.title);
|
||||
if (title) module.title = title;
|
||||
|
||||
if (type === 'IMAGE_TEXT') {
|
||||
const rawConfig = isRecord(rawModule.config) ? rawModule.config : {};
|
||||
const config: TraceImageTextConfig = {};
|
||||
const image = normalizeMedia(rawConfig.image);
|
||||
const heading = optionalTrimmedString(rawConfig.heading);
|
||||
const body = optionalTrimmedString(rawConfig.body);
|
||||
if (image) config.image = image;
|
||||
if (heading) config.heading = heading;
|
||||
if (body) config.body = body;
|
||||
if (typeof rawConfig.imagePosition === 'string' && IMAGE_POSITIONS.has(rawConfig.imagePosition)) {
|
||||
config.imagePosition = rawConfig.imagePosition as TraceImagePosition;
|
||||
} else {
|
||||
config.imagePosition = 'TOP';
|
||||
}
|
||||
module.config = config;
|
||||
} else if (type === 'HORIZONTAL_GALLERY') {
|
||||
const rawConfig = isRecord(rawModule.config) ? rawModule.config : {};
|
||||
const items = Array.isArray(rawConfig.items)
|
||||
? rawConfig.items.map(normalizeMedia).filter((item): item is TraceModuleMedia => Boolean(item))
|
||||
: [];
|
||||
module.config = { items };
|
||||
}
|
||||
return module;
|
||||
});
|
||||
return { version: TRACE_LAYOUT_VERSION, modules };
|
||||
}
|
||||
|
||||
export function parseTraceLayout(
|
||||
value: unknown,
|
||||
options: TraceLayoutValidationOptions = {},
|
||||
): TraceLayout {
|
||||
const errors = validateTraceLayout(value, options);
|
||||
if (errors.length) throw new Error(errors.join(';'));
|
||||
return normalizeValidatedTraceLayout(value as Record<string, unknown>);
|
||||
}
|
||||
|
||||
export function resolveTraceLayout(value: unknown): TraceLayout {
|
||||
try {
|
||||
return parseTraceLayout(value);
|
||||
} catch {
|
||||
return createDefaultTraceLayout();
|
||||
}
|
||||
}
|
||||
|
||||
export function templateTraceLayout(value: unknown): TraceLayout {
|
||||
const layout = resolveTraceLayout(value);
|
||||
return {
|
||||
version: TRACE_LAYOUT_VERSION,
|
||||
modules: layout.modules.map((module) => {
|
||||
if (module.type === 'IMAGE_TEXT') {
|
||||
const config = module.config as TraceImageTextConfig | undefined;
|
||||
return {
|
||||
...module,
|
||||
config: { imagePosition: config?.imagePosition ?? 'TOP' },
|
||||
};
|
||||
}
|
||||
if (module.type === 'HORIZONTAL_GALLERY') {
|
||||
return { ...module, config: { items: [] } };
|
||||
}
|
||||
return module;
|
||||
}),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user