From 74ca367081f987001fa2d2d36192873c9db18bc9 Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 11 Nov 2025 18:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E4=BF=AE=E5=A4=8D=20temp?= =?UTF-8?q?late=20run=20=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E4=B8=8E=20id?= =?UTF-8?q?entifier=20=E7=BC=BA=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主要修复: 1. ✅ 添加 transformFormDataToRunFormat 函数转换表单数据 2. ✅ 根据节点类型正确格式化数据: - image: { images: [{ url: "..." }] } - video: { videos: [{ url: "..." }] } - text: { texts: ["..."] } - select: { selections: "..." } (单选) / ["..."] (多选) 3. ✅ 添加 identifier 字段到 API 请求 4. ✅ 从 storage 获取用户 session 作为 identifier 修复问题: - ❌ 之前:{ "data": { "node_xxx": "blob:..." } } - ✅ 现在:{ "data": { "node_xxx": { "images": [{ "url": "..." }] } }, "identifier": "user-id" } 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/templates/[id]/form.tsx | 65 +++++++++++++++++++++++++++++++++++-- lib/api/template-runs.ts | 19 ++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/app/templates/[id]/form.tsx b/app/templates/[id]/form.tsx index 32179af..c1316ca 100644 --- a/app/templates/[id]/form.tsx +++ b/app/templates/[id]/form.tsx @@ -95,6 +95,62 @@ export default function TemplateFormScreen() { return Object.keys(newErrors).length === 0; }; + const transformFormDataToRunFormat = (formData: Record): Record => { + if (!template?.formSchema?.startNodes) return {}; + + const transformed: Record = {}; + + template.formSchema.startNodes.forEach((node: TemplateGraphNode) => { + const { id, type } = node; + const value = formData[id]; + + // 跳过空值 + if (!value || (Array.isArray(value) && value.length === 0)) { + return; + } + + // 根据节点类型转换数据格式 + switch (type) { + case 'image': + transformed[id] = { + images: [{ url: value }], + }; + break; + + case 'video': + transformed[id] = { + videos: [{ url: value }], + }; + break; + + case 'text': + transformed[id] = { + texts: [value], + }; + break; + + case 'select': + // 根据 allowMultiple 判断是否为多选 + if (node.data.actionData?.allowMultiple) { + transformed[id] = { + selections: Array.isArray(value) ? value : [value], + }; + } else { + transformed[id] = { + selections: value, + }; + } + break; + + default: + // 未知类型,保持原样 + transformed[id] = value; + } + }); + + return transformed; + }; + const handleSubmit = async () => { if (!template) return; if (!validateForm()) { @@ -121,8 +177,11 @@ export default function TemplateFormScreen() { return; } - // 2. 调用 template run - const runResponse = await runTemplate(id, formData); + // 2. 转换表单数据为正确的 API 格式 + const transformedData = transformFormDataToRunFormat(formData); + + // 3. 调用 template run + const runResponse = await runTemplate(id, transformedData); if (!runResponse.success) { Alert.alert('错误', '生成任务创建失败'); @@ -131,7 +190,7 @@ export default function TemplateFormScreen() { const generationId = runResponse.data; - // 3. 跳转到结果页面 + // 4. 跳转到结果页面 Alert.alert('成功', '视频生成任务已创建', [ { text: '查看结果', diff --git a/lib/api/template-runs.ts b/lib/api/template-runs.ts index aae4779..ea72fb4 100644 --- a/lib/api/template-runs.ts +++ b/lib/api/template-runs.ts @@ -1,10 +1,27 @@ import { apiClient } from './client'; import { RunTemplateResponse, TemplateGenerationResponse, RunTemplateData } from '../types/template-run'; +import { storage } from '../storage'; export async function runTemplate(templateId: string, data: RunTemplateData): Promise { + // 从 storage 获取用户 session 作为 identifier + const sessionStr = await storage.getItem('session'); + let identifier = 'anonymous'; + + if (sessionStr) { + try { + const session = JSON.parse(sessionStr); + identifier = session?.user?.id || 'anonymous'; + } catch (error) { + console.warn('Failed to parse session:', error); + } + } + return apiClient(`/api/templates/${templateId}/run`, { method: 'POST', - body: JSON.stringify({ data }), + body: JSON.stringify({ + data, + identifier, + }), }); }