From eeddc8ab8a80b1b33adf8937164cde918f86d476 Mon Sep 17 00:00:00 2001 From: ZLY Date: Thu, 25 Sep 2025 16:40:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BB=A5=E5=A4=84=E7=90=86JSON=E6=A0=BC=E5=BC=8F=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/nodeContentOther.tsx | 62 ++++++++++--------- src/utils/common.ts | 33 +++++++++- 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/src/pages/flowEditor/components/nodeContentOther.tsx b/src/pages/flowEditor/components/nodeContentOther.tsx index cbdd2b6..6021d34 100644 --- a/src/pages/flowEditor/components/nodeContentOther.tsx +++ b/src/pages/flowEditor/components/nodeContentOther.tsx @@ -1,7 +1,7 @@ import React from 'react'; import styles from '@/components/FlowEditor/node/style/baseOther.module.less'; import { Handle, Position, useStore } from '@xyflow/react'; -import { isJSON } from '@/utils/common'; +import { deserializeValue } from '@/utils/common'; import cronstrue from 'cronstrue/i18n'; interface NodeContentData { @@ -94,18 +94,18 @@ const renderSpecialNodeHandles = (isStartNode: boolean, isEndNode: boolean, data }} /> ))} - {dataIns.length > 0 && dataIns.map((_, index) => ( - - ))} + {dataIns.length > 0 && dataIns.map((_, index) => ( + + ))} ); }; @@ -179,22 +179,26 @@ const renderRegularNodeHandles = (dataIns: any[], dataOuts: any[], apiIns: any[] }; const formatFooter = (data: any) => { - switch (data.type) { - case 'WAIT': - const { duration } = data.customDef; - const hours = Math.floor(duration / 3600); - const minutes = Math.floor((duration % 3600) / 60); - const seconds = Math.floor(duration % 60); - return `${hours}小时${minutes}分钟${seconds}秒`; - case 'CYCLE': - const { intervalSeconds } = data.customDef; - return cronstrue.toString(intervalSeconds, { locale: 'zh_CN' }); - case 'EVENTSEND': - case 'EVENTLISTENE': - const { name } = data.customDef; - return `事件: ${name}`; - default: - return '这个类型还没开发'; + try { + switch (data.type) { + case 'WAIT': + const { duration } = deserializeValue(data.customDef); + const hours = Math.floor(duration / 3600); + const minutes = Math.floor((duration % 3600) / 60); + const seconds = Math.floor(duration % 60); + return `${hours}小时${minutes}分钟${seconds}秒`; + case 'CYCLE': + const { intervalSeconds } = deserializeValue(data.customDef); + return cronstrue.toString(intervalSeconds, { locale: 'zh_CN' }); + case 'EVENTSEND': + case 'EVENTLISTENE': + const { name } = data.customDef; + return `事件: ${name}`; + default: + return '这个类型还没开发'; + } + } catch (e) { + console.log(e); } }; diff --git a/src/utils/common.ts b/src/utils/common.ts index f245e06..9f279f1 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -198,4 +198,35 @@ export function formatSeconds(value: string) { result = `${parseInt(`${hourTime}`, 10)}小时${result}`; } return result; -} \ No newline at end of file +} + +/** + * 通用反序列化方法,用于处理可能为JSON格式的字符串 + * @param value - 需要反序列化的值 + * @returns 如果是有效JSON则返回解析后的对象,否则返回原值 + */ +export const deserializeValue = (value: any): any => { + // 如果不是字符串类型,直接返回原值 + if (typeof value !== 'string') { + return value; + } + + // 如果是空字符串,返回原值 + if (value === '') { + return value; + } + + // 尝试解析JSON + try { + // 检查是否为有效的JSON格式 + if (isJSON(value)) { + return JSON.parse(value); + } + // 不是JSON格式,返回原值 + return value; + } catch (error) { + // 解析失败,返回原值 + console.warn('Failed to deserialize value:', value, error); + return value; + } +}; \ No newline at end of file