|
|
|
|
@ -60,11 +60,20 @@ export function calcTank(data,row,equation){ |
|
|
|
|
'添加点': self['添加点'], |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
console.log('formula--------',formula) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(globalReplacements)) { |
|
|
|
|
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
|
|
|
|
console.log('escapedKey---------------',escapedKey) |
|
|
|
|
// 2. 【新增】清洗和校验 value,确保它是合法数字
|
|
|
|
|
let safeValue = Number(value); |
|
|
|
|
// 如果转换后是 NaN,或者原始值本身就是空/非法的,就默认设为 0
|
|
|
|
|
if (isNaN(safeValue) || value === '' || value === null) { |
|
|
|
|
safeValue = 0; |
|
|
|
|
} |
|
|
|
|
formula = formula.replace(new RegExp(escapedKey, 'g'), String(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 5. 转为合法 JS 表达式
|
|
|
|
|
let expr = formula |
|
|
|
|
.replace(/{/g, '(') |
|
|
|
|
@ -145,15 +154,64 @@ export function calcTank(data,row,equation){ |
|
|
|
|
// return jsExpr;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// export function safeEval(expr) {
|
|
|
|
|
// console.log('expr--------',expr)
|
|
|
|
|
// // 只允许数字、括号、+ - * / . 等安全字符
|
|
|
|
|
// if (!/^[\d+\-*/().]+$/.test(expr)) {
|
|
|
|
|
// throw new Error("公式包含非法字符");
|
|
|
|
|
// }
|
|
|
|
|
// try {
|
|
|
|
|
// // 使用 Function 而非 eval 更安全(无作用域访问)
|
|
|
|
|
// return new Function('return (' + expr + ')')();
|
|
|
|
|
// } catch (e) {
|
|
|
|
|
// throw new Error("公式计算失败: " + e.message);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// export function safeEval(expr, fallback = 0) {
|
|
|
|
|
// console.log('expr--------', expr)
|
|
|
|
|
// // 只允许数字、括号、+ - * / . 等安全字符
|
|
|
|
|
// if (!/^[\d+\-*/().\s]+$/.test(expr)) { // 稍微优化了正则,增加了\s允许空格
|
|
|
|
|
// throw new Error("公式包含非法字符");
|
|
|
|
|
// }
|
|
|
|
|
// try {
|
|
|
|
|
// // 使用 Function 进行计算
|
|
|
|
|
// const result = new Function('return (' + expr + ')')();
|
|
|
|
|
|
|
|
|
|
// console.log('result--------',result)
|
|
|
|
|
|
|
|
|
|
// // 【核心修改】拦截 NaN:如果计算结果是 NaN,就返回兜底值(默认为 0)
|
|
|
|
|
// // 这样 (0/0)+5 就会变成 0+5,最终返回 5
|
|
|
|
|
// return isNaN(result) ? fallback : result;
|
|
|
|
|
|
|
|
|
|
// } catch (e) {
|
|
|
|
|
// throw new Error("公式计算失败: " + e.message);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
export function safeEval(expr) { |
|
|
|
|
// 只允许数字、括号、+ - * / . 等安全字符
|
|
|
|
|
if (!/^[\d+\-*/().]+$/.test(expr)) { |
|
|
|
|
throw new Error("公式包含非法字符"); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
// 使用 Function 而非 eval 更安全(无作用域访问)
|
|
|
|
|
return new Function('return (' + expr + ')')(); |
|
|
|
|
// 【核心修改】预处理:把公式中所有的 0/0 替换成 0
|
|
|
|
|
// 正则解释:\b0\/0\b 精确匹配独立的 "0/0",避免误伤 "10/0" 或 "0/05"
|
|
|
|
|
expr = expr.replace(/\b0\/0\b/g, '0'); |
|
|
|
|
|
|
|
|
|
const result = new Function('return (' + expr + ')')(); |
|
|
|
|
|
|
|
|
|
console.log('result--------',result,result == 0) |
|
|
|
|
// if(result == 0){
|
|
|
|
|
// return { success: true, message: 0 };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 剩下的兜底逻辑:处理 5/0 这种等于 Infinity 的情况
|
|
|
|
|
if (!isFinite(result) || isNaN(result)) { |
|
|
|
|
// // 如果你希望 5/0 这种也变成 0 从而算出 5,可以把这里改成 result = 0;
|
|
|
|
|
// return "检测到公式中存在除以0或无效运算";
|
|
|
|
|
return { success: false, message: "检测到公式中存在除以0或无效运算" }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// return result;
|
|
|
|
|
return { success: true, value: result == 0 ? 0 : result }; |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error("公式计算失败: " + e.message); |
|
|
|
|
return { success: false, message: "公式语法错误" }; |
|
|
|
|
// return "公式语法错误";
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |