🐛 fix: 修复 template run 数据格式与 identifier 缺失问题
主要修复: 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 <noreply@anthropic.com>
This commit is contained in:
parent
5ad83146ad
commit
74ca367081
|
|
@ -95,6 +95,62 @@ export default function TemplateFormScreen() {
|
|||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const transformFormDataToRunFormat = (formData: Record<string, any>): Record<string, any> => {
|
||||
if (!template?.formSchema?.startNodes) return {};
|
||||
|
||||
const transformed: Record<string, any> = {};
|
||||
|
||||
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: '查看结果',
|
||||
|
|
|
|||
|
|
@ -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<RunTemplateResponse> {
|
||||
// 从 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<RunTemplateResponse>(`/api/templates/${templateId}/run`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ data }),
|
||||
body: JSON.stringify({
|
||||
data,
|
||||
identifier,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue