ソースを参照

调整营养素含量计算至后端计算,前端仅显示

wangyang 5 年 前
コミット
6db49c9540
3 ファイル変更57 行追加22 行削除
  1. 8 0
      src/api/food.js
  2. 9 0
      src/api/unit.js
  3. 40 22
      src/views/foodModifier/index.vue

+ 8 - 0
src/api/food.js

@@ -189,3 +189,11 @@ export function getFoodAllUnits(id) {
   })
 }
 
+// 食物营养素含量计算
+export function calculateNutrientsContent(id, params) {
+  return request({
+    url: `/api/foods/${id}/nutrients/content`,
+    method: 'get',
+    params
+  })
+}

+ 9 - 0
src/api/unit.js

@@ -43,3 +43,12 @@ export function updateUnit(id, data) {
     data
   })
 }
+
+// 单位转换
+export function convertUnits(unitPairs) {
+  return request({
+    url: `/api/units/convert`,
+    method: 'get',
+    params: { unitPairs }
+  })
+}

+ 40 - 22
src/views/foodModifier/index.vue

@@ -90,7 +90,8 @@
 
 <script>
 import { getFoodModifiers, removeFoodModifier, updateFoodModifierSort, convertFoodUnit, getNutrientList,
-  getDetail} from '@/api/food'
+  getDetail, calculateNutrientsContent } from '@/api/food'
+import { convertUnits } from '@/api/unit'
 
 export default {
   name: 'FoodModifierIndex',
@@ -148,28 +149,45 @@ export default {
         this.$message.error(res.data.message)
       })
     },
-    showDialog(row, column, event) {
-      getNutrientList(row.foodId).then(res => {
-        const nutrients = res.data
-        convertFoodUnit(row.foodId, { fromUnit: row.inInitUnit }).then(res => {
-          if (!res.data.ratio) {
-            return;
-          }
-          this.dialogHtml = `转换关系:1${row.inInitUnit}${this.food.name}=${res.data.ratio}${this.food.normalizedUnit}${this.food.name}<br>`
-          this.dialogHtml += `${row.inInit}${row.inInitUnit}${this.food.name}含有如下营养素:<br>`
-          this.dialogHtml += `******************************<br>`
-          for (let i=0; i < nutrients.length; i++) {
-            const nutrient = nutrients[i]
-            const quantity = nutrient.normalizedQuantity * res.data.ratio * row.inInit
-            this.dialogHtml += `${nutrient.nutrientName}:${quantity}${nutrient.unit}。<br>`
-            this.dialogHtml += `营养素计量为:${nutrient.quantity}${nutrient.unit}。Nv_Spec为:${nutrient.nvSpec}${nutrient.nvSpecUnit}。<br>`
-            this.dialogHtml += `******************************<br>`
-          }
-          this.dialogFormVisible = true
-        })
-      }).catch(res => {
-        this.$message.error('获取数据失败')
+    async showDialog(row, column, event) {
+      calculateNutrientsContent(row.foodId, { unit: row.inInitUnit, quantity: row.inInit }).then(res => {
+        this.dialogFormVisible = true
+        this.dialogHtml = res.data
       })
+      // try {
+      //   const nutrientRes = await getNutrientList(row.foodId)
+      //   const nutrients = nutrientRes.data
+      //   const foodUnitRes = await convertFoodUnit(row.foodId, { fromUnit: row.inInitUnit })
+      //   const foodUnitRatio = foodUnitRes.data.ratio
+      //   if (!foodUnitRatio) {
+      //     this.dialogHtml = ''
+      //     return
+      //   }
+      //   let unitPairs = []
+      //   nutrients.forEach(item => unitPairs.push({ fromUnit: }))
+      //   const convertedUnits = await convertUnits()
+      //
+      //
+      // } catch {
+      //   this.$message.error('获取数据失败')
+      // }
+      //
+      //     this.dialogHtml = `转换关系:1${row.inInitUnit}${this.food.name}=${res.data.ratio}${this.food.normalizedUnit}${this.food.name}<br>`
+      //     this.dialogHtml += `${row.inInit}${row.inInitUnit}${this.food.name}含有如下营养素:<br>`
+      //     this.dialogHtml += `******************************<br>`
+      //     for (let i=0; i < nutrients.length; i++) {
+      //       const nutrient = nutrients[i]
+      //       const quantity = nutrient.normalizedQuantity * res.data.ratio * row.inInit
+      //       this.dialogHtml += `${nutrient.nutrientName}:${quantity}${nutrient.unit}。<br>`
+      //       this.dialogHtml += `营养素计量为:${nutrient.quantity}${nutrient.unit}。Nv_Spec为:${nutrient.nvSpec}${nutrient.nvSpecUnit}。<br>`
+      //       this.dialogHtml += `******************************<br>`
+      //     }
+      //     this.dialogFormVisible = true
+      //     this.di
+      //     })
+      // }).catch(res => {
+      //   this.$message.error('获取数据失败')
+      // })
     }
   }
 }