|
@@ -282,11 +282,48 @@ export default {
|
|
let point = _this.com.attrs[i].data.point;
|
|
let point = _this.com.attrs[i].data.point;
|
|
formual = await _this.getRemote(formual);
|
|
formual = await _this.getRemote(formual);
|
|
|
|
|
|
- _this.com.attrs[i].content = eval(formual).toFixed(point);
|
|
|
|
- data = data.replace(
|
|
|
|
- "{{" + attrId + "}}",
|
|
|
|
- eval(formual).toFixed(point)
|
|
|
|
- );
|
|
|
|
|
|
+ // 定义一个安全的评估函数
|
|
|
|
+ const safeEval = (formula) => {
|
|
|
|
+ // 定义允许使用的数学函数和 IF 函数
|
|
|
|
+ const mathFunctions = {
|
|
|
|
+ abs: Math.abs,
|
|
|
|
+ ceil: Math.ceil,
|
|
|
|
+ floor: Math.floor,
|
|
|
|
+ max: Math.max,
|
|
|
|
+ min: Math.min,
|
|
|
|
+ round: Math.round,
|
|
|
|
+ sqrt: Math.sqrt,
|
|
|
|
+ IF: (condition, trueValue, falseValue) =>
|
|
|
|
+ condition ? trueValue : falseValue,
|
|
|
|
+ // 添加其他需要的数学函数
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // 创建一个新的函数,将数学函数作为参数传入
|
|
|
|
+ const func = new Function(
|
|
|
|
+ ...Object.keys(mathFunctions),
|
|
|
|
+ `return ${formula}`
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 执行函数,传入数学函数作为参数
|
|
|
|
+ return func(...Object.values(mathFunctions));
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ const result = safeEval(formual);
|
|
|
|
+ _this.com.attrs[i].content = result.toFixed(point);
|
|
|
|
+ data = data.replace(
|
|
|
|
+ "{{" + _this.com.attrs[i].id + "}}",
|
|
|
|
+ result.toFixed(point)
|
|
|
|
+ );
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error("处理公式时出错:", error);
|
|
|
|
+ console.error("错误的公式:", formual);
|
|
|
|
+ _this.com.attrs[i].content = "计算错误";
|
|
|
|
+ data = data.replace(
|
|
|
|
+ "{{" + _this.com.attrs[i].id + "}}",
|
|
|
|
+ "计算错误"
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
|
|
/* let formual = await _this.analysisFormual(_this.com.attrs[i]);
|
|
/* let formual = await _this.analysisFormual(_this.com.attrs[i]);
|
|
formual = await _this.getRemote(formual);
|
|
formual = await _this.getRemote(formual);
|
|
@@ -494,33 +531,74 @@ export default {
|
|
//分析公式
|
|
//分析公式
|
|
async analysisFormual(attrs) {
|
|
async analysisFormual(attrs) {
|
|
let _this = this;
|
|
let _this = this;
|
|
- // console.log("开始分析公式:", attrs.formula);
|
|
|
|
- var pattern = /(\[.*?\]){3}/;
|
|
|
|
- var formual = attrs.formula;
|
|
|
|
- var reg = new RegExp(pattern);
|
|
|
|
- while (true) {
|
|
|
|
- var items = formual.match(reg);
|
|
|
|
- if (items == null) break;
|
|
|
|
- let itemName = items[0];
|
|
|
|
- // console.log("处理项:", itemName);
|
|
|
|
- try {
|
|
|
|
- let data = await _this.getFormualData(itemName, attrs.data);
|
|
|
|
- // console.log(`获取到的数据: ${itemName} = ${data}`);
|
|
|
|
- if (data === null || data === undefined || isNaN(data)) {
|
|
|
|
- console.warn(`获取到的数据无效: ${itemName}`);
|
|
|
|
- formual = formual.replace(itemName, "(0)");
|
|
|
|
- } else {
|
|
|
|
- formual = formual.replace(itemName, `(${parseFloat(data)})`);
|
|
|
|
|
|
+ console.log("开始分析公式:", attrs.formula);
|
|
|
|
+ let formual = attrs.formula;
|
|
|
|
+
|
|
|
|
+ // 处理 [T][...][...] 引用
|
|
|
|
+ const tPattern = /\[T\]\[(.*?)\]\[(.*?)\]/g;
|
|
|
|
+ formual = await this.replaceAsync(
|
|
|
|
+ formual,
|
|
|
|
+ tPattern,
|
|
|
|
+ async (match, p1, p2) => {
|
|
|
|
+ try {
|
|
|
|
+ let data = await _this.getModuleData(p1, p2);
|
|
|
|
+ console.log(`获取到的数据: ${match} = ${data}`);
|
|
|
|
+ if (data === null || data === undefined || isNaN(data)) {
|
|
|
|
+ console.warn(`获取到的数据无效: ${match}`);
|
|
|
|
+ return "0";
|
|
|
|
+ } else {
|
|
|
|
+ return `${parseFloat(data)}`;
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(`处理 ${match} 时出错:`, error);
|
|
|
|
+ return "0";
|
|
}
|
|
}
|
|
- } catch (error) {
|
|
|
|
- console.error(`处理 ${itemName} 时出错:`, error);
|
|
|
|
- formual = formual.replace(itemName, "(0)");
|
|
|
|
}
|
|
}
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 处理嵌套的 IF 语句
|
|
|
|
+ const ifPattern = /IF\s*\((.*?),(.*?),(.*?)\)/g;
|
|
|
|
+ let depth = 0;
|
|
|
|
+ while (formual.match(ifPattern) && depth < 10) {
|
|
|
|
+ formual = formual.replace(
|
|
|
|
+ ifPattern,
|
|
|
|
+ (match, condition, trueValue, falseValue) => {
|
|
|
|
+ // 递归处理嵌套的 IF 语句
|
|
|
|
+ if (trueValue.includes("IF(") || falseValue.includes("IF(")) {
|
|
|
|
+ return `(${condition} ? (${trueValue}) : (${falseValue}))`;
|
|
|
|
+ }
|
|
|
|
+ return `(${condition} ? ${trueValue} : ${falseValue})`;
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ depth++;
|
|
}
|
|
}
|
|
- /* console.log("分析后的公式:", formual); */
|
|
|
|
|
|
+
|
|
|
|
+ console.log("分析后的公式:", formual);
|
|
return formual;
|
|
return formual;
|
|
},
|
|
},
|
|
|
|
|
|
|
|
+ // 辅助方法:异步替换
|
|
|
|
+ async replaceAsync(str, regex, asyncFn) {
|
|
|
|
+ const promises = [];
|
|
|
|
+ str.replace(regex, (match, ...args) => {
|
|
|
|
+ const promise = asyncFn(match, ...args);
|
|
|
|
+ promises.push(promise);
|
|
|
|
+ });
|
|
|
|
+ const data = await Promise.all(promises);
|
|
|
|
+ return str.replace(regex, () => data.shift());
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // 辅助方法:异步替换
|
|
|
|
+ async replaceAsync(str, regex, asyncFn) {
|
|
|
|
+ const promises = [];
|
|
|
|
+ str.replace(regex, (match, ...args) => {
|
|
|
|
+ const promise = asyncFn(match, ...args);
|
|
|
|
+ promises.push(promise);
|
|
|
|
+ });
|
|
|
|
+ const data = await Promise.all(promises);
|
|
|
|
+ return str.replace(regex, () => data.shift());
|
|
|
|
+ },
|
|
|
|
+
|
|
async getFormualData(formualItem, data) {
|
|
async getFormualData(formualItem, data) {
|
|
let _this = this;
|
|
let _this = this;
|
|
// console.log("开始获取公式数据:", formualItem);
|
|
// console.log("开始获取公式数据:", formualItem);
|