执行正常,保存导出错误
This commit is contained in:
@@ -266,6 +266,8 @@
|
||||
getNodesByCategory,
|
||||
nodeDefinitions
|
||||
} from "~/components/flow-nodes/nodes";
|
||||
import { save, open } from '@tauri-apps/plugin-dialog';
|
||||
import { writeTextFile, readTextFile } from '@tauri-apps/plugin-fs';
|
||||
|
||||
const router = useRouter();
|
||||
const workflowNodes = ref<FlowNode[]>([]);
|
||||
@@ -380,6 +382,7 @@
|
||||
|
||||
const taskData = {
|
||||
name: `任务_${new Date().toLocaleTimeString()}`,
|
||||
createdAt: new Date().toISOString(),
|
||||
nodes: workflowNodes.value.map((node) => ({
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
@@ -389,13 +392,25 @@
|
||||
}))
|
||||
};
|
||||
|
||||
console.log("执行", taskData);
|
||||
console.log("任务已开始,请查看控制台");
|
||||
console.log("=== 工作流执行 ===");
|
||||
console.log("任务名称:", taskData.name);
|
||||
console.log("创建时间:", taskData.createdAt);
|
||||
console.log("节点数量:", taskData.nodes.length);
|
||||
console.log("完整JSON数据:");
|
||||
console.log(JSON.stringify(taskData, null, 2));
|
||||
console.log("=== 执行完成 ===");
|
||||
};
|
||||
|
||||
const saveTask = () => {
|
||||
const saveTask = async () => {
|
||||
if (workflowNodes.value.length === 0) {
|
||||
console.log("工作流为空,无法保存");
|
||||
return;
|
||||
}
|
||||
|
||||
const taskData = {
|
||||
name: `任务_${new Date().toLocaleTimeString()}`,
|
||||
createdAt: new Date().toISOString(),
|
||||
version: "1.0",
|
||||
nodes: workflowNodes.value.map((node) => ({
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
@@ -405,50 +420,71 @@
|
||||
}))
|
||||
};
|
||||
|
||||
const blob = new Blob([JSON.stringify(taskData, null, 2)], {
|
||||
type: "application/json"
|
||||
});
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `任务_${Date.now()}.json`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
try {
|
||||
const filePath = await save({
|
||||
filters: [{
|
||||
name: 'JSON',
|
||||
extensions: ['json']
|
||||
}],
|
||||
defaultPath: `workflow_${Date.now()}.json`
|
||||
});
|
||||
|
||||
const exportTask = () => {
|
||||
saveTask();
|
||||
};
|
||||
|
||||
const importTask = () => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = "application/json";
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
try {
|
||||
const data = JSON.parse(e.target?.result as string);
|
||||
if (data.nodes && Array.isArray(data.nodes)) {
|
||||
workflowNodes.value = data.nodes.map((node: any) => {
|
||||
const definition = nodeDefinitions[node.id.replace(/-\d+$/, "")];
|
||||
return {
|
||||
...definition,
|
||||
id: node.id,
|
||||
config: node.config || {}
|
||||
};
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("文件格式错误", error);
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
if (filePath) {
|
||||
await writeTextFile(filePath, JSON.stringify(taskData, null, 2));
|
||||
console.log("工作流已保存到:", filePath);
|
||||
}
|
||||
};
|
||||
input.click();
|
||||
} catch (error) {
|
||||
console.error("保存工作流失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const exportTask = async () => {
|
||||
await saveTask();
|
||||
};
|
||||
|
||||
const importTask = async () => {
|
||||
try {
|
||||
const selected = await open({
|
||||
multiple: false,
|
||||
filters: [{
|
||||
name: 'JSON',
|
||||
extensions: ['json']
|
||||
}]
|
||||
});
|
||||
|
||||
if (selected) {
|
||||
const content = await readTextFile(selected as string);
|
||||
const data = JSON.parse(content);
|
||||
|
||||
if (data.nodes && Array.isArray(data.nodes)) {
|
||||
// 验证并重建节点
|
||||
const importedNodes = data.nodes.map((node: any) => {
|
||||
// 提取原始节点ID(去除时间戳后缀)
|
||||
const originalId = node.id.replace(/-\d+-[a-z0-9]+$/, '');
|
||||
const definition = nodeDefinitions[originalId];
|
||||
|
||||
if (!definition) {
|
||||
console.warn(`未找到节点定义: ${originalId}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
...definition,
|
||||
id: node.id, // 保持导入的ID
|
||||
config: node.config || definition.config || {}
|
||||
};
|
||||
}).filter(Boolean); // 过滤掉null值
|
||||
|
||||
workflowNodes.value = importedNodes as FlowNode[];
|
||||
console.log(`成功导入 ${importedNodes.length} 个节点`);
|
||||
console.log("导入的工作流数据:", data);
|
||||
} else {
|
||||
console.error("无效的工作流文件格式");
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("导入工作流失败:", error);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
|
Reference in New Issue
Block a user