2e461c2c by lxs

数据定价计算方式更新

1 parent c0101796
......@@ -600,7 +600,7 @@ const setTableRowData = (dGuid, rIndex) => {
rowData.dataFields.map(t => {
t.damFieldTable = JSON.parse(JSON.stringify(damFieldOptions));
const match = damFieldOptions.find(d => d.chName == t.fieldName);
if(match) {
if (match) {
t.chName = match.chName;
t.enName = match.enName;
}
......@@ -856,17 +856,13 @@ const calculatePrice = (pData) => {
// 3. 提取变量名
const variableRegex = /[\u4e00-\u9fa5a-zA-Z_][\u4e00-\u9fa5a-zA-Z0-9_]*/g;
const variableNames = modelFormula.match(variableRegex) || [];
const uniqueVariables = [...new Set(variableNames)];
const variableNames = [...new Set(modelFormula.match(variableRegex) || [])];
// 4. 构建变量映射
const variables = {};
uniqueVariables.forEach(name => {
// 过滤数学函数
if (!mathFunctions.includes(name)) {
const dim = pData.find(d => d.dimensionalityName === name);
variables[name] = dim ? parseFloat(dim.sNum) || 0 : 0;
}
variableNames.forEach(name => {
const dim = pData.find(d => d.dimensionalityName === name);
variables[name] = dim ? parseFloat(dim.sNum) || 0 : 0;
});
// 5. 替换变量为数值(考虑边界情况)
......@@ -875,35 +871,38 @@ const calculatePrice = (pData) => {
expression = expression.replace(new RegExp(name, 'g'), variables[name]);
});
// 7. 表达式安全检查
// 7.1 检查是否只包含允许的字符
const sanitizedExpr = expression.replace(allowedOperators, '');
if (sanitizedExpr.length > 0) {
console.error('公式包含非法字符:', sanitizedExpr);
// 6. 表达式规范化(不丢失括号)
expression = expression
.replace(/\s+/g, '') // 去空格
.replace(/\^/g, '**') // 幂运算转换
.replace(/"|'/g, '') // 去引号(不破坏括号)
.replace(/(\d)\(/g, '$1*(') // 处理隐式乘法
.replace(/\)\(/g, ')*('); // 括号间乘法
// 7. 验证括号配对
const balance = expression.split('').reduce((acc, char) => {
if (char === '(') acc++;
if (char === ')') acc--;
return acc;
}, 0);
if (balance !== 0) {
console.error('括号不匹配');
return NaN;
}
// 8. 执行计算
try {
// 8.1 转换运算符
expression = expression
.replace(/\s+/g, '') // 去除空格
.replace(/\^/g, '**') // 转换幂运算
.replace(/÷/g, '/') // 转换除法符号
.replace(/×/g, '*') // 转换乘法符号
.replace(/(\d+)\(/g, '$1*(') // 处理隐式乘法
.replace(/\)\(/g, ')*('); // 处理括号间乘法
// 8.2 安全计算
// 8. 安全计算
try {
const result = new Function('return ' + expression)();
const roundedResult = Math.round(parseFloat(result) * 100) / 100;
dataTransactionPrice.value = roundedResult.toFixed(2);
return roundedResult;
} catch (error) {
console.error('公式计算错误:', {
console.error('计算错误:', {
error,
originalFormula: modelFormula,
processedFormula: expression
original: modelFormula,
processed: expression
});
return NaN;
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!