/* eslint-disable */ import React, { useState } from 'react'; import { Button, message, Radio, Modal, Tooltip } from 'antd-v4'; import { HistoryOutlined } from '@ant-design/icons'; import * as commonUtils from '@/utils/utils';/* 通用方法 */ import styles from './JsonFormatter.less'; const JSONFormatter = ({ initialJsonString, onJsonFormatOk, onJsonFormatCancel }) => { const [jsonString, setJsonString] = useState(initialJsonString); const [question, setQuestion] = useState(''); const [answer, setAnswer] = useState(''); const [selectedOption, setSelectedOption] = useState(''); const [qaHistory, setQaHistory] = useState([]); const [isModalVisible, setIsModalVisible] = useState(false); const handleJsonChange = (e) => { const newJsonString = e.target.value; setJsonString(newJsonString); }; const handleQuestionChange = (e) => { setQuestion(e.target.value); }; const handleOptionChange = (e) => { const value = e.target.value; setSelectedOption(value); // 获取选中的 Radio 元素 const radioElement = document.querySelector(`input[type="radio"][value="${value}"]`); if (radioElement) { // 获取自定义属性中的文本值 const optionText = radioElement.getAttribute('data-question-text'); setQuestion(optionText); } }; const handleAsk = () => { const finalQuestion = selectedOption || question; if (jsonString) { try { const parsedData = JSON.parse(jsonString); let newAnswer; if (finalQuestion.includes('有多少个属性')) { newAnswer = `JSON 对象有 ${Object.keys(parsedData).length} 个属性。`; } else if (finalQuestion.includes('获取props.sSrcModelsId')) { newAnswer = '暂未实现获取 props.sSrcModelsId 的逻辑,请根据实际情况处理。'; } else { newAnswer = '暂不支持该问题,请换个问题试试。'; } setAnswer(newAnswer); setQaHistory([...qaHistory, { question: finalQuestion, answer: newAnswer }]); } catch (error) { const errorAnswer = 'JSON 解析出错,请检查 JSON 格式。'; setAnswer(errorAnswer); setQaHistory([...qaHistory, { question: finalQuestion, answer: errorAnswer }]); } } else { const noDataAnswer = '请先输入有效的 JSON 数据。'; setAnswer(noDataAnswer); setQaHistory([...qaHistory, { question: finalQuestion, answer: noDataAnswer }]); } setQuestion(''); // 清空输入框 setSelectedOption(''); // 清空单选框选择 }; const handleKeyPress = (e) => { if (e.key === 'Enter') { handleAsk(); } }; const handleOk = () => { if (jsonString) { try { JSON.parse(jsonString); if (onJsonFormatOk) { onJsonFormatOk(jsonString); } } catch (error) { message.error('JSON 格式不正确,请检查'); } } }; const handleCancel = () => { if (onJsonFormatCancel) { onJsonFormatCancel(); } }; const showModal = () => { setIsModalVisible(true); }; const handleModalCancel = () => { setIsModalVisible(false); }; try { const parsedData = commonUtils.isNotEmptyObject(jsonString) ? JSON.parse(jsonString) : {}; const formattedJSON = commonUtils.isNotEmptyObject(parsedData) ? JSON.stringify(parsedData, null, 2) : ''; return (

原始 JSON