Selaa lähdekoodia

feat(wise-cost-util): 返回匹配上的关键字

zhangweicheng 4 vuotta sitten
vanhempi
commit
641a0b17b7
2 muutettua tiedostoa jossa 8 lisäystä ja 4 poistoa
  1. 2 2
      wise-cost-util/package.json
  2. 6 2
      wise-cost-util/src/material.ts

+ 2 - 2
wise-cost-util/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@sc/wise-cost-util",
-  "version": "1.0.4",
+  "version": "1.0.5",
   "description": "wise-cost项目前后端业务通用工具包",
   "main": "./dist/index.cjs.js",
   "module": "./dist/index.esm.js",
@@ -45,7 +45,7 @@
     "rollup-plugin-typescript2": "^0.27.3",
     "ts-node": "^9.0.0",
     "tslib": "^2.0.3",
-    "typescript": "^4.0.3"
+    "typescript": "4.0.3"
   },
   "dependencies": {
     "@sc/util": "^1.0.7",

+ 6 - 2
wise-cost-util/src/material.ts

@@ -5,6 +5,8 @@ import evaluate from 'evaluator.js';
 export interface IKeywordItem {
   optionCode: string; // 选项号
   coe: string; // 系数 *1.29
+  keyword: string;
+  unit: string;
 }
 
 export interface IInfoPriceMain {
@@ -29,6 +31,7 @@ export const calcMaterialExp = (infoPrice: IInfoPriceMain, keywordItems: IKeywor
   const decimal = 2; // 小数位数
   const groupStr = infoPrice.classCode.substr(preCodeLength);
   let { expString } = infoPrice; // 计算式
+  const matchKeywords: IKeywordItem[] = [];
   try {
     if (groupStr.length > 0) {
       let startIndex = 0;
@@ -41,6 +44,7 @@ export const calcMaterialExp = (infoPrice: IInfoPriceMain, keywordItems: IKeywor
           let coeNumber = parseFloat(group.coe.substr(1)); // 要代入计算的系数
           if (operator === '*') coeNumber -= 1;
           expString = replaceAll(option.charAt(0), `${coeNumber}`, expString);
+          matchKeywords.push(group);
         }
         startIndex += 3;
       }
@@ -49,11 +53,11 @@ export const calcMaterialExp = (infoPrice: IInfoPriceMain, keywordItems: IKeywor
 
       // 没找到的按0处理
       expString = replaceAll(/[a-zA-Z]/, '0', expString);
-      return roundForObj(evaluate(expString), decimal);
+      return { price: roundForObj(evaluate(expString), decimal), matchKeywords };
     }
   } catch (error) {
     throw new Error('计算失败,表达式有误');
   }
 
-  return roundForObj(infoPrice.basePrice, decimal);
+  return { price: roundForObj(infoPrice.basePrice, decimal), matchKeywords };
 };