diff --git a/src/components/Common/Typesetting/typesetting.js b/src/components/Common/Typesetting/typesetting.js index 57406df..655c7cb 100644 --- a/src/components/Common/Typesetting/typesetting.js +++ b/src/components/Common/Typesetting/typesetting.js @@ -86,10 +86,10 @@ const Typesetting = props => { // 如果需要自定义排版数 // 确认是竖向排列还是横向排列 - 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 L = masterData?.dLength; + const W = masterData?.dWidth; + const H = masterData?.dWidth; + const D = masterData?.dHeight; // 动态计算公式值 const evaluateFormula = (formula, variables) => { @@ -109,14 +109,12 @@ const Typesetting = props => { return 0; } }; - // 计算公式值 const variables = { L, W, H, D, - ...props.slaveRowData, }; let boxList = []; @@ -140,52 +138,43 @@ const Typesetting = props => { { name: "右贴边位", value: "dYTBW" }, { name: "右(下)插位组件", value: "dYXCW" }, ]; - 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, - }); + 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, // 参数名称 }); - 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 }), {}); // 计算表达式结果 @@ -194,33 +183,42 @@ const Typesetting = props => { W, H, D, - ...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; + console.log(variabless, "variabless"); + const evaluateFormulas = (formula, variables) => { + if (!formula) return ""; // 如果公式为空,返回空字符串 + 提; //取公式中的变量名 + const variableNames = formula.match(/\b[a-zA-Z_][a-zA-Z0-9_]*\b/g) || []; + + // 替换公式中的变量为实际值 + let modifiedFormula = formula; + variableNames.forEach(name => { + if (variables[name] !== undefined) { + modifiedFormula = modifiedFormula.replace(new RegExp(`\\b${name}\\b`, "g"), variables[name]); + } + }); + + return modifiedFormula; + }; + // const s = `((dYXCW + D) >= dSFHS) && ((dYXCW + D) >= dZSCW) + // ? (dSFHS >= dZSCW ? -(dYXCW + D - dSFHS) * 2 : -((dYXCW + D) - dZSCW)) * 2 + // : dSFHS >= dZSCW ? (dYXCW + D >= dZSCW) ? -(dSFHS - (dYXCW + D)) * 2 : -(dSFHS - dZXCW) * 2 : (dYXCW + D >= dSFHS) ? -(dZSCW - (dYXCW + D)) * 2 : -(dZXCW - dSFHS) * 2` + // const ddd = evaluateFormula(s, variabless); + // console.log("🚀 ~ ddd:", ddd); let DisableMateriallIcon = L ? sPackDetailPath : null; // 盒子展长展开公式计算 - - 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 innerWidth = slaveRowData.sTypes === "6" ? evaluateFormula(sWidthFormula, variabless) : evaluateFormula(sLengthFormula, variabless); + const innerHeight = slaveRowData.sTypes === "6" ? evaluateFormula(sLengthFormula, variabless) : evaluateFormula(sWidthFormula, variabless); const { sSettingMethod, sSGroupOffset, iFAngle, iSAngle, sSecondorizontalOffset, sSecondLongitudinalOffset } = radioValue ? radioValue : {}; - 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 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 isVertical = sSettingMethod === "从上到下"; // true 表示竖向排列,false 表示横向排列 sSettingMethod === '从上到下' const reference = sSGroupOffset === "首盒"; // 第三列参考 @@ -317,7 +315,7 @@ const Typesetting = props => { dSvgBoxHeight, showNew: 2, }; - // const { dZTBW } = variabless; + const { dZTBW } = variabless; const createDiv = (col, row, index, leftPosition, topPosition, offsetZ, icon) => (