feat(utils): 添加通用反序列化方法以处理JSON格式字符串

production
钟良源 7 months ago
parent 059a04cfd1
commit eeddc8ab8a

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import styles from '@/components/FlowEditor/node/style/baseOther.module.less'; import styles from '@/components/FlowEditor/node/style/baseOther.module.less';
import { Handle, Position, useStore } from '@xyflow/react'; import { Handle, Position, useStore } from '@xyflow/react';
import { isJSON } from '@/utils/common'; import { deserializeValue } from '@/utils/common';
import cronstrue from 'cronstrue/i18n'; import cronstrue from 'cronstrue/i18n';
interface NodeContentData { 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) => (
<Handle <Handle
key={`input-handle-${index}`} key={`input-handle-${index}`}
type="target" type="target"
position={Position.Left} position={Position.Left}
id={dataIns[index].name || `input-${index}`} id={dataIns[index].name || `input-${index}`}
style={{ style={{
...handleStyles.data, ...handleStyles.data,
top: `${70 + apiIns.length * 20 + index * 20}px` top: `${70 + apiIns.length * 20 + index * 20}px`
}} }}
/> />
))} ))}
</> </>
); );
}; };
@ -179,22 +179,26 @@ const renderRegularNodeHandles = (dataIns: any[], dataOuts: any[], apiIns: any[]
}; };
const formatFooter = (data: any) => { const formatFooter = (data: any) => {
switch (data.type) { try {
case 'WAIT': switch (data.type) {
const { duration } = data.customDef; case 'WAIT':
const hours = Math.floor(duration / 3600); const { duration } = deserializeValue(data.customDef);
const minutes = Math.floor((duration % 3600) / 60); const hours = Math.floor(duration / 3600);
const seconds = Math.floor(duration % 60); const minutes = Math.floor((duration % 3600) / 60);
return `${hours}小时${minutes}分钟${seconds}`; const seconds = Math.floor(duration % 60);
case 'CYCLE': return `${hours}小时${minutes}分钟${seconds}`;
const { intervalSeconds } = data.customDef; case 'CYCLE':
return cronstrue.toString(intervalSeconds, { locale: 'zh_CN' }); const { intervalSeconds } = deserializeValue(data.customDef);
case 'EVENTSEND': return cronstrue.toString(intervalSeconds, { locale: 'zh_CN' });
case 'EVENTLISTENE': case 'EVENTSEND':
const { name } = data.customDef; case 'EVENTLISTENE':
return `事件: ${name}`; const { name } = data.customDef;
default: return `事件: ${name}`;
return '这个类型还没开发'; default:
return '这个类型还没开发';
}
} catch (e) {
console.log(e);
} }
}; };

@ -198,4 +198,35 @@ export function formatSeconds(value: string) {
result = `${parseInt(`${hourTime}`, 10)}小时${result}`; result = `${parseInt(`${hourTime}`, 10)}小时${result}`;
} }
return result; return result;
} }
/**
* 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;
}
};
Loading…
Cancel
Save