From 6da837081e2baa695c75ca2c3f0b50a1c0694639 Mon Sep 17 00:00:00 2001 From: 陈鑫涛 <10125295+chen-xintao97@user.noreply.gitee.com> Date: Thu, 26 Jun 2025 15:53:54 +0800 Subject: [PATCH] 使用eval计算 --- src/components/Common/Typesetting/typesetting.js | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------ src/components/QuickQuote/index.jsx | 12 +++++++++--- 2 files changed, 95 insertions(+), 63 deletions(-) diff --git a/src/components/Common/Typesetting/typesetting.js b/src/components/Common/Typesetting/typesetting.js index 528195f..57406df 100644 --- a/src/components/Common/Typesetting/typesetting.js +++ b/src/components/Common/Typesetting/typesetting.js @@ -38,13 +38,13 @@ const Typesetting = props => { sColumnNameConfig, bAdvancedSetting, // 高级设置 } = slaveRowData; - + const { masterData, selectedNode, slaveData } = state; // 如果是卷筒类 那么上下左右留白等于左留白 - if (selectedNode && selectedNode.sTypeKey === 'juantong') { - dSBLB = dZBLB - dXBLB = dZBLB - dYBLB = dZBLB + if (selectedNode && selectedNode.sTypeKey === "juantong") { + dSBLB = dZBLB; + dXBLB = dZBLB; + dYBLB = dZBLB; } // 生成盒子图片 const [boxKey, setBoxKey] = useState(new Date().getTime()); @@ -86,10 +86,10 @@ const Typesetting = props => { // 如果需要自定义排版数 // 确认是竖向排列还是横向排列 - const L = masterData?.dLength; - const W = masterData?.dWidth; - const H = masterData?.dWidth; - const D = masterData?.dHeight; + const L = Number(masterData?.dLength) ?? 0; + const W = Number(masterData?.dWidth) ?? 0; + const H = Number(masterData?.dWidth) ?? 0; + const D = Number(masterData?.dHeight) ?? 0; // 动态计算公式值 const evaluateFormula = (formula, variables) => { @@ -109,6 +109,7 @@ const Typesetting = props => { return 0; } }; + // 计算公式值 const variables = { L, @@ -139,43 +140,52 @@ const Typesetting = props => { { name: "右贴边位", value: "dYTBW" }, { name: "右(下)插位组件", value: "dYXCW" }, ]; - slaveNewData.forEach(x => { - let key = 0; - if (x.sAssignFormula) { - key = evaluateFormula(x.sAssignFormula, variables); - } else { - key = slaveRowData[x.sName]; - } - boxList.push({ - value: key, - sName: titleList1.find(item => item.value === x.sName)?.name || "", - isEditable: true, - isSelect: false, - selectValue: null, - selectLabel: "", - selectImage: null, - type: x.sTypes || null, - show: true, - sCode: titleList1.find(item => item.value === x.sName)?.value || "", - showName: x.showName, // 参数名称 + const results = slaveNewData.reduce((acc, { sName, sDefault }) => ({ ...acc, [sName]: sDefault }), {}); + let { dSFHS, dHDC, dXFHS, dZSCW, dZTBW, dZXCW, dYSCW, dYTBW, dYXCW } = results; + if (results) { + slaveNewData.forEach(x => { + let key = 0; + if (x.sAssignFormula) { + const i = titleList1.findIndex(item => item.value === x.sAssignFormula); + if (i !== -1) { + key = results[x.sAssignFormula]; + } else { + key = eval(x.sAssignFormula); + } + } else { + key = slaveRowData[x.sName]; + } + boxList.push({ + value: key, + sName: titleList1.find(item => item.value === x.sName)?.name || "", + isEditable: true, + isSelect: false, + selectValue: null, + selectLabel: "", + selectImage: null, + type: x.sTypes || null, + show: true, + sCode: titleList1.find(item => item.value === x.sName)?.value || "", + showName: x.showName, // 参数名称 + sAssignFormula: x.sAssignFormula, + }); }); - }); - tables.forEach(x => { - boxList.push({ - value: x.value, - sName: x.name, - isEditable: true, - isSelect: false, - selectValue: null, - selectLabel: "", - selectImage: null, - type: x.type || null, - show: true, - showName: x.name, // 参数名称 + tables.forEach(x => { + boxList.push({ + value: x.value, + sName: x.name, + isEditable: true, + isSelect: false, + selectValue: null, + selectLabel: "", + selectImage: null, + type: x.type || null, + show: true, + showName: x.name, // 参数名称 + }); }); - }); + } } - const result = boxList.reduce((acc, { sCode, value }) => ({ ...acc, [sCode]: value }), {}); // 计算表达式结果 @@ -188,18 +198,29 @@ const Typesetting = props => { ...props.slaveRowData, ...result, }; + const keys = ["dSFHS", "dHDC", "dXFHS", "dZSCW", "dZTBW", "dZXCW", "dYSCW", "dYTBW", "dYXCW"]; + for (let key of keys) { + let num = Number(variabless[key]); // 将变量值转换为数字 + result[key] = Number.isNaN(num) ? 0 : num; // 如果是 NaN,则设置为 0,否则保留原值 + } + let { dSFHS, dHDC, dXFHS, dZSCW, dZTBW, dZXCW, dYSCW, dYTBW, dYXCW } = result; let DisableMateriallIcon = L ? sPackDetailPath : null; // 盒子展长展开公式计算 - const innerWidth = slaveRowData.sTypes === "6" ? evaluateFormula(sWidthFormula, variabless) : evaluateFormula(sLengthFormula, variabless); - const innerHeight = slaveRowData.sTypes === "6" ? evaluateFormula(sLengthFormula, variabless) : evaluateFormula(sWidthFormula, variabless); + + let innerWidth = slaveRowData.sTypes === "6" ? eval(sWidthFormula) : eval(sLengthFormula); + let innerHeight = slaveRowData.sTypes === "6" ? eval(sLengthFormula) : eval(sWidthFormula); + if (isNaN(innerHeight) || isNaN(innerHeight)) { + innerWidth = 0; + innerHeight = 0; + } const { sSettingMethod, sSGroupOffset, iFAngle, iSAngle, sSecondorizontalOffset, sSecondLongitudinalOffset } = radioValue ? radioValue : {}; - const dFWidthOffset = evaluateFormula(radioValue?.sFWidthOffset, variabless); // 首盒纵向偏移 - const dFLengthOffset = evaluateFormula(radioValue?.sFLengthOffset, variabless); // 首盒横向偏移 - const dSWidthOffset = evaluateFormula(radioValue?.sSWidthOffset, variabless); // 次盒纵向偏移 - const dSLengthOffset = evaluateFormula(radioValue?.sSLengthOffset, variabless); // 次盒横向偏移 - const boxLengthOffset = evaluateFormula(sSecondorizontalOffset, variabless); // 第二列横向偏移 - const boxWidthOffset = evaluateFormula(sSecondLongitudinalOffset, variabless); // 第二列纵向偏移 + const dFWidthOffset = eval(radioValue?.sFWidthOffset); // 首盒纵向偏移 + const dFLengthOffset = eval(radioValue?.sFLengthOffset); // 首盒横向偏移 + const dSWidthOffset = eval(radioValue?.sSWidthOffset); // 次盒纵向偏移 + const dSLengthOffset = eval(radioValue?.sSLengthOffset); // 次盒横向偏移 + const boxLengthOffset = eval(sSecondorizontalOffset); // 第二列横向偏移 + const boxWidthOffset = eval(sSecondLongitudinalOffset); // 第二列纵向偏移 const isVertical = sSettingMethod === "从上到下"; // true 表示竖向排列,false 表示横向排列 sSettingMethod === '从上到下' const reference = sSGroupOffset === "首盒"; // 第三列参考 @@ -296,7 +317,7 @@ const Typesetting = props => { dSvgBoxHeight, showNew: 2, }; - const { dZTBW } = variabless; + // const { dZTBW } = variabless; const createDiv = (col, row, index, leftPosition, topPosition, offsetZ, icon) => (