|
@@ -150,6 +150,19 @@ export default {
|
|
methods: {
|
|
methods: {
|
|
async replaceData(data) {
|
|
async replaceData(data) {
|
|
let _this = this;
|
|
let _this = this;
|
|
|
|
+ // 定义数学函数和 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,
|
|
|
|
+ // 添加其他需要的数学函数
|
|
|
|
+ };
|
|
for (var i = 0; i < _this.com.attrs.length; i++) {
|
|
for (var i = 0; i < _this.com.attrs.length; i++) {
|
|
let attrId = _this.com.attrs[i].id;
|
|
let attrId = _this.com.attrs[i].id;
|
|
if (_this.com.attrs[i].type == "variable") {
|
|
if (_this.com.attrs[i].type == "variable") {
|
|
@@ -276,44 +289,63 @@ export default {
|
|
data = data.replace("{{" + item.id + "}}", selectHtml);
|
|
data = data.replace("{{" + item.id + "}}", selectHtml);
|
|
}
|
|
}
|
|
} else if (_this.com.attrs[i].type == "formual") {
|
|
} else if (_this.com.attrs[i].type == "formual") {
|
|
- //// console.log('attrs',_this.com.attrs[i]);
|
|
|
|
//处理计算公式 不处理
|
|
//处理计算公式 不处理
|
|
let formual = await _this.analysisFormual(_this.com.attrs[i]);
|
|
let formual = await _this.analysisFormual(_this.com.attrs[i]);
|
|
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);
|
|
|
|
|
|
|
|
+ /* console.log("处理的公式:", formual); // 添加日志 */
|
|
|
|
+
|
|
|
|
+ // 收集公式中使用的变量
|
|
|
|
+ const variables = {};
|
|
|
|
+ const variableRegex = /\b[a-zA-Z_][a-zA-Z0-9_]*\b/g;
|
|
|
|
+ const matches = formual.match(variableRegex);
|
|
|
|
+ if (matches) {
|
|
|
|
+ for (const varName of matches) {
|
|
|
|
+ if (
|
|
|
|
+ varName !== "IF" &&
|
|
|
|
+ !Object.keys(mathFunctions).includes(varName)
|
|
|
|
+ ) {
|
|
|
|
+ variables[varName] = `${varName}`; // 默认值设为0,您可以根据需要修改
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* console.log("识别的变量:", variables); // 添加日志 */
|
|
|
|
+
|
|
// 定义一个安全的评估函数
|
|
// 定义一个安全的评估函数
|
|
- 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 safeEval = (formula, variables) => {
|
|
|
|
+ // 创建一个新的函数,将数学函数和变量作为参数传入
|
|
const func = new Function(
|
|
const func = new Function(
|
|
...Object.keys(mathFunctions),
|
|
...Object.keys(mathFunctions),
|
|
|
|
+ ...Object.keys(variables),
|
|
`return ${formula}`
|
|
`return ${formula}`
|
|
);
|
|
);
|
|
|
|
|
|
- // 执行函数,传入数学函数作为参数
|
|
|
|
- return func(...Object.values(mathFunctions));
|
|
|
|
|
|
+ // 执行函数,传入数学函数和变量作为参数
|
|
|
|
+ return func(
|
|
|
|
+ ...Object.values(mathFunctions),
|
|
|
|
+ ...Object.values(variables)
|
|
|
|
+ );
|
|
};
|
|
};
|
|
|
|
|
|
try {
|
|
try {
|
|
- const result = safeEval(formual);
|
|
|
|
- _this.com.attrs[i].content = result.toFixed(point);
|
|
|
|
|
|
+ const result = safeEval(formual, variables);
|
|
|
|
+ /* console.log("计算结果:", result); // 添加日志 */
|
|
|
|
+
|
|
|
|
+ let formattedResult;
|
|
|
|
+ if (typeof result === "number" && !isNaN(result)) {
|
|
|
|
+ formattedResult = result.toFixed(point);
|
|
|
|
+ } else if (typeof result === "boolean") {
|
|
|
|
+ formattedResult = result ? "1" : "0";
|
|
|
|
+ } else {
|
|
|
|
+ formattedResult = String(result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _this.com.attrs[i].content = formattedResult;
|
|
data = data.replace(
|
|
data = data.replace(
|
|
"{{" + _this.com.attrs[i].id + "}}",
|
|
"{{" + _this.com.attrs[i].id + "}}",
|
|
- result.toFixed(point)
|
|
|
|
|
|
+ formattedResult
|
|
);
|
|
);
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error("处理公式时出错:", error);
|
|
console.error("处理公式时出错:", error);
|
|
@@ -531,7 +563,7 @@ export default {
|
|
//分析公式
|
|
//分析公式
|
|
async analysisFormual(attrs) {
|
|
async analysisFormual(attrs) {
|
|
let _this = this;
|
|
let _this = this;
|
|
- console.log("开始分析公式:", attrs.formula);
|
|
|
|
|
|
+ /* console.log("开始分析公式:", attrs.formula); */
|
|
let formual = attrs.formula;
|
|
let formual = attrs.formula;
|
|
|
|
|
|
// 处理 [T][...][...] 引用
|
|
// 处理 [T][...][...] 引用
|
|
@@ -542,29 +574,34 @@ export default {
|
|
async (match, p1, p2) => {
|
|
async (match, p1, p2) => {
|
|
try {
|
|
try {
|
|
let data = await _this.getModuleData(p1, p2);
|
|
let data = await _this.getModuleData(p1, p2);
|
|
- console.log(`获取到的数据: ${match} = ${data}`);
|
|
|
|
- if (data === null || data === undefined || isNaN(data)) {
|
|
|
|
|
|
+ /* console.log(`获取到的数据: ${match} = ${data}`); */
|
|
|
|
+ if (data === null || data === undefined) {
|
|
console.warn(`获取到的数据无效: ${match}`);
|
|
console.warn(`获取到的数据无效: ${match}`);
|
|
- return "0";
|
|
|
|
|
|
+ return "''"; // 返回空字符串而不是 0
|
|
} else {
|
|
} else {
|
|
- return `${parseFloat(data)}`;
|
|
|
|
|
|
+ return typeof data === "string" ? `${data}` : data;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(`处理 ${match} 时出错:`, error);
|
|
console.error(`处理 ${match} 时出错:`, error);
|
|
- return "0";
|
|
|
|
|
|
+ return "''";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
);
|
|
);
|
|
|
|
|
|
// 处理嵌套的 IF 语句
|
|
// 处理嵌套的 IF 语句
|
|
- const ifPattern = /IF\s*\((.*?),(.*?),(.*?)\)/g;
|
|
|
|
|
|
+ const ifPattern = /IF\s*\((.*?),(.*?),(.*?)\)/gi;
|
|
let depth = 0;
|
|
let depth = 0;
|
|
while (formual.match(ifPattern) && depth < 10) {
|
|
while (formual.match(ifPattern) && depth < 10) {
|
|
formual = formual.replace(
|
|
formual = formual.replace(
|
|
ifPattern,
|
|
ifPattern,
|
|
(match, condition, trueValue, falseValue) => {
|
|
(match, condition, trueValue, falseValue) => {
|
|
// 递归处理嵌套的 IF 语句
|
|
// 递归处理嵌套的 IF 语句
|
|
- if (trueValue.includes("IF(") || falseValue.includes("IF(")) {
|
|
|
|
|
|
+ if (
|
|
|
|
+ trueValue.includes("IF(") ||
|
|
|
|
+ falseValue.includes("IF(") ||
|
|
|
|
+ trueValue.includes("if(") ||
|
|
|
|
+ falseValue.includes("if(")
|
|
|
|
+ ) {
|
|
return `(${condition} ? (${trueValue}) : (${falseValue}))`;
|
|
return `(${condition} ? (${trueValue}) : (${falseValue}))`;
|
|
}
|
|
}
|
|
return `(${condition} ? ${trueValue} : ${falseValue})`;
|
|
return `(${condition} ? ${trueValue} : ${falseValue})`;
|
|
@@ -573,7 +610,7 @@ export default {
|
|
depth++;
|
|
depth++;
|
|
}
|
|
}
|
|
|
|
|
|
- console.log("分析后的公式:", formual);
|
|
|
|
|
|
+ /* console.log("分析后的公式:", formual); */
|
|
return formual;
|
|
return formual;
|
|
},
|
|
},
|
|
|
|
|
|
@@ -749,23 +786,23 @@ export default {
|
|
//获取本地文档模块数据
|
|
//获取本地文档模块数据
|
|
async getModuleData(code, attrName) {
|
|
async getModuleData(code, attrName) {
|
|
let _this = this;
|
|
let _this = this;
|
|
- // console.log(`尝试获取模块数据: code=${code}, attrName=${attrName}`);
|
|
|
|
- // console.log("当前 coms:", _this.coms);
|
|
|
|
|
|
+ /* console.log(`尝试获取模块数据: code=${code}, attrName=${attrName}`);
|
|
|
|
+ console.log("当前 coms:", _this.coms); */
|
|
|
|
|
|
let item = _this.coms.filter((o) => o.name == code);
|
|
let item = _this.coms.filter((o) => o.name == code);
|
|
- // console.log(`找到的模块:`, item);
|
|
|
|
|
|
+ /* console.log(`找到的模块:`, item); */
|
|
|
|
|
|
if (item.length > 0) {
|
|
if (item.length > 0) {
|
|
let attr = item[0].attrs.filter((o) => o.name == attrName);
|
|
let attr = item[0].attrs.filter((o) => o.name == attrName);
|
|
- // console.log(`找到的属性:`, attr);
|
|
|
|
|
|
+ /* console.log(`找到的属性:`, attr); */
|
|
|
|
|
|
if (attr.length > 0) {
|
|
if (attr.length > 0) {
|
|
- // console.log(`返回的值:`, attr[0].content);
|
|
|
|
- return parseFloat(attr[0].content);
|
|
|
|
|
|
+ /* console.log(`返回的值:`, attr[0].content); */
|
|
|
|
+ return attr[0].content; // 返回原始值,不进行转换
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // console.log("未找到匹配的模块或属性,返回 0");
|
|
|
|
- return 0;
|
|
|
|
|
|
+ /* console.log("未找到匹配的模块或属性,返回空字符串"); */
|
|
|
|
+ return ""; // 返回空字符串而不是 0
|
|
},
|
|
},
|
|
|
|
|
|
handleChangeProduct(e) {
|
|
handleChangeProduct(e) {
|