8dd504ae by lxs

数据定价计算方式更新

1 parent f882928b
...@@ -600,7 +600,7 @@ const setTableRowData = (dGuid, rIndex) => { ...@@ -600,7 +600,7 @@ const setTableRowData = (dGuid, rIndex) => {
600 rowData.dataFields.map(t => { 600 rowData.dataFields.map(t => {
601 t.damFieldTable = JSON.parse(JSON.stringify(damFieldOptions)); 601 t.damFieldTable = JSON.parse(JSON.stringify(damFieldOptions));
602 const match = damFieldOptions.find(d => d.chName == t.fieldName); 602 const match = damFieldOptions.find(d => d.chName == t.fieldName);
603 if(match) { 603 if (match) {
604 t.chName = match.chName; 604 t.chName = match.chName;
605 t.enName = match.enName; 605 t.enName = match.enName;
606 } 606 }
...@@ -856,17 +856,13 @@ const calculatePrice = (pData) => { ...@@ -856,17 +856,13 @@ const calculatePrice = (pData) => {
856 856
857 // 3. 提取变量名 857 // 3. 提取变量名
858 const variableRegex = /[\u4e00-\u9fa5a-zA-Z_][\u4e00-\u9fa5a-zA-Z0-9_]*/g; 858 const variableRegex = /[\u4e00-\u9fa5a-zA-Z_][\u4e00-\u9fa5a-zA-Z0-9_]*/g;
859 const variableNames = modelFormula.match(variableRegex) || []; 859 const variableNames = [...new Set(modelFormula.match(variableRegex) || [])];
860 const uniqueVariables = [...new Set(variableNames)];
861 860
862 // 4. 构建变量映射 861 // 4. 构建变量映射
863 const variables = {}; 862 const variables = {};
864 uniqueVariables.forEach(name => { 863 variableNames.forEach(name => {
865 // 过滤数学函数
866 if (!mathFunctions.includes(name)) {
867 const dim = pData.find(d => d.dimensionalityName === name); 864 const dim = pData.find(d => d.dimensionalityName === name);
868 variables[name] = dim ? parseFloat(dim.sNum) || 0 : 0; 865 variables[name] = dim ? parseFloat(dim.sNum) || 0 : 0;
869 }
870 }); 866 });
871 867
872 // 5. 替换变量为数值(考虑边界情况) 868 // 5. 替换变量为数值(考虑边界情况)
...@@ -875,35 +871,38 @@ const calculatePrice = (pData) => { ...@@ -875,35 +871,38 @@ const calculatePrice = (pData) => {
875 expression = expression.replace(new RegExp(name, 'g'), variables[name]); 871 expression = expression.replace(new RegExp(name, 'g'), variables[name]);
876 }); 872 });
877 873
878 // 7. 表达式安全检查 874 // 6. 表达式规范化(不丢失括号)
879 // 7.1 检查是否只包含允许的字符 875 expression = expression
880 const sanitizedExpr = expression.replace(allowedOperators, ''); 876 .replace(/\s+/g, '') // 去空格
881 if (sanitizedExpr.length > 0) { 877 .replace(/\^/g, '**') // 幂运算转换
882 console.error('公式包含非法字符:', sanitizedExpr); 878 .replace(/"|'/g, '') // 去引号(不破坏括号)
879 .replace(/(\d)\(/g, '$1*(') // 处理隐式乘法
880 .replace(/\)\(/g, ')*('); // 括号间乘法
881
882 // 7. 验证括号配对
883 const balance = expression.split('').reduce((acc, char) => {
884 if (char === '(') acc++;
885 if (char === ')') acc--;
886 return acc;
887 }, 0);
888
889 if (balance !== 0) {
890 console.error('括号不匹配');
883 return NaN; 891 return NaN;
884 } 892 }
885 // 8. 执行计算
886 try {
887 // 8.1 转换运算符
888 expression = expression
889 .replace(/\s+/g, '') // 去除空格
890 .replace(/\^/g, '**') // 转换幂运算
891 .replace(/÷/g, '/') // 转换除法符号
892 .replace(/×/g, '*') // 转换乘法符号
893 .replace(/(\d+)\(/g, '$1*(') // 处理隐式乘法
894 .replace(/\)\(/g, ')*('); // 处理括号间乘法
895 893
896 // 8.2 安全计算 894 // 8. 安全计算
895 try {
897 const result = new Function('return ' + expression)(); 896 const result = new Function('return ' + expression)();
898 const roundedResult = Math.round(parseFloat(result) * 100) / 100; 897 const roundedResult = Math.round(parseFloat(result) * 100) / 100;
899 898
900 dataTransactionPrice.value = roundedResult.toFixed(2); 899 dataTransactionPrice.value = roundedResult.toFixed(2);
901 return roundedResult; 900 return roundedResult;
902 } catch (error) { 901 } catch (error) {
903 console.error('公式计算错误:', { 902 console.error('计算错误:', {
904 error, 903 error,
905 originalFormula: modelFormula, 904 original: modelFormula,
906 processedFormula: expression 905 processed: expression
907 }); 906 });
908 return NaN; 907 return NaN;
909 } 908 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!