feat: add baby object match edutainment flow
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
231
src/services/edutainment-baby-object/babyObjectMatchClient.ts
Normal file
231
src/services/edutainment-baby-object/babyObjectMatchClient.ts
Normal file
@@ -0,0 +1,231 @@
|
||||
import type {
|
||||
BabyObjectMatchDraft,
|
||||
BabyObjectMatchItemAsset,
|
||||
BabyObjectMatchPublishRequest,
|
||||
BabyObjectMatchPublishResponse,
|
||||
CreateBabyObjectMatchDraftRequest,
|
||||
SaveBabyObjectMatchDraftRequest,
|
||||
} from '../../../packages/shared/src/contracts/edutainmentBabyObject';
|
||||
import {
|
||||
BABY_OBJECT_MATCH_EDUTAINMENT_TAG,
|
||||
BABY_OBJECT_MATCH_TEMPLATE_ID,
|
||||
BABY_OBJECT_MATCH_TEMPLATE_NAME,
|
||||
normalizeBabyObjectMatchTags,
|
||||
validateBabyObjectMatchItemNames,
|
||||
} from '../../../packages/shared/src/contracts/edutainmentBabyObject';
|
||||
import { buildBabyObjectMatchPublicWorkCode } from '../publicWorkCode';
|
||||
|
||||
const STORAGE_KEY = 'genarrative.edutainmentBabyObject.localDrafts.v1';
|
||||
|
||||
type LocalDraftStore = Record<string, BabyObjectMatchDraft>;
|
||||
|
||||
function canUseLocalStorage() {
|
||||
return (
|
||||
typeof window !== 'undefined' && typeof window.localStorage !== 'undefined'
|
||||
);
|
||||
}
|
||||
|
||||
function readLocalDraftStore(): LocalDraftStore {
|
||||
if (!canUseLocalStorage()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
try {
|
||||
const rawValue = window.localStorage.getItem(STORAGE_KEY);
|
||||
if (!rawValue) {
|
||||
return {};
|
||||
}
|
||||
const parsed = JSON.parse(rawValue) as LocalDraftStore;
|
||||
return parsed && typeof parsed === 'object' ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function writeLocalDraftStore(store: LocalDraftStore) {
|
||||
if (!canUseLocalStorage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(store));
|
||||
}
|
||||
|
||||
function createLocalId(prefix: string) {
|
||||
const randomPart =
|
||||
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
||||
? crypto.randomUUID().replace(/-/gu, '')
|
||||
: Math.random().toString(36).slice(2);
|
||||
|
||||
return `${prefix}-${Date.now().toString(36)}-${randomPart.slice(0, 12)}`;
|
||||
}
|
||||
|
||||
function encodeSvgDataUri(svg: string) {
|
||||
return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;
|
||||
}
|
||||
|
||||
function buildPlaceholderItemImage(itemName: string, index: number) {
|
||||
const palettes = [
|
||||
{
|
||||
bg: '#fef3c7',
|
||||
accent: '#fb7185',
|
||||
shadow: '#f59e0b',
|
||||
text: '#7c2d12',
|
||||
},
|
||||
{
|
||||
bg: '#dbeafe',
|
||||
accent: '#34d399',
|
||||
shadow: '#60a5fa',
|
||||
text: '#064e3b',
|
||||
},
|
||||
] as const;
|
||||
const palette = palettes[index % palettes.length]!;
|
||||
const displayText = itemName.slice(0, 6);
|
||||
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><rect width="512" height="512" rx="96" fill="${palette.bg}"/><circle cx="256" cy="238" r="132" fill="${palette.accent}" opacity=".92"/><ellipse cx="256" cy="356" rx="132" ry="34" fill="${palette.shadow}" opacity=".22"/><circle cx="210" cy="202" r="24" fill="#fff" opacity=".82"/><circle cx="302" cy="202" r="24" fill="#fff" opacity=".82"/><path d="M204 276c30 30 74 30 104 0" fill="none" stroke="#fff" stroke-width="18" stroke-linecap="round"/><text x="256" y="438" text-anchor="middle" font-family="Arial,'Microsoft YaHei',sans-serif" font-size="42" font-weight="700" fill="${palette.text}">${displayText}</text></svg>`;
|
||||
|
||||
return encodeSvgDataUri(svg);
|
||||
}
|
||||
|
||||
function buildItemAsset(
|
||||
itemName: string,
|
||||
index: number,
|
||||
): BabyObjectMatchItemAsset {
|
||||
return {
|
||||
itemId: `baby-object-item-${index + 1}`,
|
||||
itemName,
|
||||
imageSrc: buildPlaceholderItemImage(itemName, index),
|
||||
assetObjectId: null,
|
||||
generationProvider: 'placeholder',
|
||||
prompt: `生成适合 4-8 岁儿童识物分类游戏的${itemName}物品图,绘本草地舞台风格,单个物体,透明或干净背景,无文字、无水印、无按钮。`,
|
||||
};
|
||||
}
|
||||
|
||||
function saveDraftToLocalStore(draft: BabyObjectMatchDraft) {
|
||||
const store = readLocalDraftStore();
|
||||
store[draft.profileId] = draft;
|
||||
writeLocalDraftStore(store);
|
||||
}
|
||||
|
||||
export function normalizeBabyObjectMatchDraft(
|
||||
draft: BabyObjectMatchDraft,
|
||||
): BabyObjectMatchDraft {
|
||||
const now = new Date().toISOString();
|
||||
return {
|
||||
...draft,
|
||||
templateId: BABY_OBJECT_MATCH_TEMPLATE_ID,
|
||||
templateName: BABY_OBJECT_MATCH_TEMPLATE_NAME,
|
||||
workTitle: draft.workTitle.trim() || '宝贝识物',
|
||||
workDescription: draft.workDescription.trim(),
|
||||
itemNames: [
|
||||
draft.itemNames[0].trim(),
|
||||
draft.itemNames[1].trim(),
|
||||
],
|
||||
itemAssets: [
|
||||
{
|
||||
...draft.itemAssets[0],
|
||||
itemName: draft.itemNames[0].trim(),
|
||||
},
|
||||
{
|
||||
...draft.itemAssets[1],
|
||||
itemName: draft.itemNames[1].trim(),
|
||||
},
|
||||
],
|
||||
themeTags: normalizeBabyObjectMatchTags(draft.themeTags),
|
||||
updatedAt: draft.updatedAt || now,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前为本地 Demo 创作链路。真实 image-2 接入后替换为后端接口,
|
||||
* 但返回契约保持 BabyObjectMatchDraftResponse。
|
||||
*/
|
||||
export async function createBabyObjectMatchDraft(
|
||||
payload: CreateBabyObjectMatchDraftRequest,
|
||||
) {
|
||||
const validated = validateBabyObjectMatchItemNames(payload);
|
||||
if (!validated.valid) {
|
||||
throw new Error('请填写两个物品名称。');
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const draftId = createLocalId('baby-object-draft');
|
||||
const profileId = createLocalId('baby-object-profile');
|
||||
const itemNames: [string, string] = [
|
||||
validated.itemAName,
|
||||
validated.itemBName,
|
||||
];
|
||||
const draft = normalizeBabyObjectMatchDraft({
|
||||
draftId,
|
||||
profileId,
|
||||
templateId: BABY_OBJECT_MATCH_TEMPLATE_ID,
|
||||
templateName: BABY_OBJECT_MATCH_TEMPLATE_NAME,
|
||||
workTitle: '宝贝识物',
|
||||
workDescription: `${itemNames[0]}和${itemNames[1]}识物分类`,
|
||||
itemNames,
|
||||
itemAssets: [buildItemAsset(itemNames[0], 0), buildItemAsset(itemNames[1], 1)],
|
||||
themeTags: [BABY_OBJECT_MATCH_EDUTAINMENT_TAG, '宝贝识物'],
|
||||
publicationStatus: 'draft',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
publishedAt: null,
|
||||
});
|
||||
|
||||
saveDraftToLocalStore(draft);
|
||||
return { draft };
|
||||
}
|
||||
|
||||
export async function saveBabyObjectMatchDraft(
|
||||
payload: SaveBabyObjectMatchDraftRequest,
|
||||
) {
|
||||
const draft = normalizeBabyObjectMatchDraft({
|
||||
...payload.draft,
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
saveDraftToLocalStore(draft);
|
||||
|
||||
return { draft };
|
||||
}
|
||||
|
||||
export async function publishBabyObjectMatchWork(
|
||||
payload: BabyObjectMatchPublishRequest,
|
||||
): Promise<BabyObjectMatchPublishResponse> {
|
||||
const draft = normalizeBabyObjectMatchDraft({
|
||||
...payload.draft,
|
||||
publicationStatus: 'published',
|
||||
publishedAt: payload.draft.publishedAt ?? new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
saveDraftToLocalStore(draft);
|
||||
|
||||
return {
|
||||
draft,
|
||||
publicWorkCode: buildBabyObjectMatchPublicWorkCode(draft.profileId),
|
||||
};
|
||||
}
|
||||
|
||||
export function listLocalBabyObjectMatchDrafts() {
|
||||
return Object.values(readLocalDraftStore()).sort(
|
||||
(left, right) =>
|
||||
new Date(right.updatedAt).getTime() - new Date(left.updatedAt).getTime(),
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteLocalBabyObjectMatchDraft(profileId: string) {
|
||||
const normalizedProfileId = profileId.trim();
|
||||
if (!normalizedProfileId) {
|
||||
return listLocalBabyObjectMatchDrafts();
|
||||
}
|
||||
|
||||
const store = readLocalDraftStore();
|
||||
delete store[normalizedProfileId];
|
||||
writeLocalDraftStore(store);
|
||||
|
||||
return listLocalBabyObjectMatchDrafts();
|
||||
}
|
||||
|
||||
export const babyObjectMatchClient = {
|
||||
createDraft: createBabyObjectMatchDraft,
|
||||
deleteDraft: deleteLocalBabyObjectMatchDraft,
|
||||
saveDraft: saveBabyObjectMatchDraft,
|
||||
publish: publishBabyObjectMatchWork,
|
||||
listLocalDrafts: listLocalBabyObjectMatchDrafts,
|
||||
};
|
||||
Reference in New Issue
Block a user