Browse Source

完成食物营养素从近似食物导入

wangyang 5 năm trước cách đây
mục cha
commit
a2dda00dca

+ 9 - 0
src/api/food.js

@@ -207,6 +207,15 @@ export function importNutrientsFromTemplate(id, data) {
   })
 }
 
+// 食物从近似食物添加营养素
+export function importNutrientFromSimilarFood(id, data) {
+  return request({
+    url: `/api/foods/${id}/nutrients/import-from-food`,
+    method: 'post',
+    data
+  })
+}
+
 // 食物从近似食物导入规格
 export function importModifiersFromSimilarFood(id, data) {
   return request({

+ 0 - 7
src/components/FloatingWindow/index.vue

@@ -45,7 +45,6 @@ export default {
   },
   methods: {
     start(e) {
-      console.log("start");
       e.preventDefault();   //阻止默认的拖拽
       if (e.button == 0) {
         this.canDrag = this.ableMove;
@@ -55,7 +54,6 @@ export default {
       }
     },
     move(e) {
-      console.log("move");
       if (this.canDrag == true) {
         this.isClick = false;
         let x = e.clientX - this.x0;
@@ -78,17 +76,14 @@ export default {
     },
     stop() {
       this.canDrag = false;
-      console.log("stop");
       this.show();
     },
     enter() {
-      console.log("enter");
       if (this.hoverShow) {
         this.show();
       }
     },
     leave() {
-      console.log("leave");
       this.canDrag = false;
       this.hide();
     },
@@ -105,7 +100,6 @@ export default {
     },
     hide() {
       if (this.ableHide) {
-        console.log("hide");
         let img = document.querySelector(".suspendBox");
         let box = document.querySelector(".outBox");
         if (!this.canDrag && img.offsetLeft < this.distance) {
@@ -116,7 +110,6 @@ export default {
       }
     },
     clickEvent() {      //悬浮窗的点击事件
-      console.log("悬浮窗的点击事件");
       this.$emit("clickHandle")
     },
   },

+ 53 - 23
src/views/food/components/FoodDetail.vue

@@ -79,26 +79,28 @@
           保存
         </el-button>
         <el-button v-loading="loading" style="margin-left: 10px;" type="success" v-if="!isEdit" @click="handleDialog">
-          保存并从模板导入营养素
+          保存并导入营养素
         </el-button>
       </div>
     </el-form>
 
-    <el-dialog title="从模板导入营养素" :visible.sync="dialogFormVisible">
-      <el-form ref="dialogForm" label-position="left" label-width="60px" style="width: 80%; margin-left:50px;">
-        <el-form-item label="模板:" prop="templateId" >
+    <el-dialog title="导入营养素" :visible.sync="dialogFormVisible" v-loading="importLoading">
+      <el-form ref="dialogForm" label-position="left" style="width: 80%; margin-left:50px;">
+        <el-radio :label="0" v-model="importType" @change="handleImportTypeChange" >从模板导入</el-radio>
+        <el-radio :label="1" v-model="importType" @change="handleImportTypeChange" style="margin-bottom: 10px">从食物导入</el-radio>
+        <el-form-item prop="importId">
           <el-select
-            v-model="templateId"
+            v-model="importId"
             class="filter-item"
             filterable
             remote
             reserve-keyword
-            placeholder="请输入模板关键词"
-            :loading="templateLoading"
-            :remote-method="queryTemplates"
+            placeholder="请输入关键词"
+            :loading="importLoading"
+            :remote-method="queryImportItems"
             style="width: 100%"
           >
-            <el-option v-for="item in templates" :key="`template${item.id}`" :label="item.name" :value="item.id" />
+            <el-option v-for="item in importItems" :key="`import${item.id}`" :label="item.name" :value="item.id" />
           </el-select>
         </el-form-item>
       </el-form>
@@ -106,7 +108,7 @@
         <el-button @click="dialogFormVisible = false">
           取消
         </el-button>
-        <el-button type="primary" @click="submitFromTemplate">
+        <el-button type="primary" @click="submitImport">
           提交
         </el-button>
       </div>
@@ -115,7 +117,7 @@
 </template>
 
 <script>
-import { create, getDetail, update } from '@/api/food'
+import { create, getDetail, getList as getFoodList, update } from '@/api/food'
 import Dropzone from '@/components/Dropzone'
 import SingleImage from '@/components/Upload/SingleImage'
 import FloatingWindow from '@/components/FloatingWindow'
@@ -139,17 +141,18 @@ export default {
         name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
       },
       foodId: '',
-      dialogFormVisible: false,
-      templateLoading:false,
-      templates: [],
-      templateId: '',
       foodCategories: [],
       categoriesProp: {
         value: 'id',
         expandTrigger: 'hover'
       },
       isFixed: false,
-      offsetTop: 0
+      offsetTop: 0,
+      dialogFormVisible: false,
+      importLoading: false,
+      importType: 0,
+      importId: '',
+      importItems: []
     }
   },
   mounted() {
@@ -216,23 +219,50 @@ export default {
         this.postForm.otherImages = [file.fileUrl]
       }
     },
+    handleImportTypeChange() {
+      this.importId = ''
+      this.importItems = []
+      this.queryImportItems()
+    },
+    queryImportItems(query=''){
+      this.importType === 0 ? this.queryTemplates(query) : this.queryFoods(query)
+    },
     queryTemplates(query='') {
+      this.importLoading = true
       getNutrientTemplates({ query }).then(res => {
-        this.templates = res.data.list
-        if (this.templates.length > 0) {
-          this.templateId = this.templates[0].id
+        this.importItems = res.data.list
+        if (this.importItems.length > 0) {
+          this.importId = this.importItems[0].id
         }
+        this.importLoading = false
       }).catch(() => {
-        this.templates = []
+        this.importItems = []
+        this.importLoading = false
+      })
+    },
+    queryFoods(query='') {
+      this.importLoading = true
+      getFoodList({ query: query, orderBy: 0, pageNum: 1, pageSize: 20 }).then(res => {
+        this.importItems = res.data.list
+        this.importLoading = false
+      }).catch(() => {
+        this.importItems = []
+        this.importLoading = false
       })
     },
     handleDialog() {
-      this.templateId = ''
+      this.importId = ''
+      this.importType = 0
+      this.importItems = []
       this.queryTemplates()
       this.dialogFormVisible = true
     },
-    submitFromTemplate() {
-      this.postForm.templateId = this.templateId
+    submitImport() {
+      if (this.importType === 0 && this.importId) {
+        this.postForm.templateId = this.importId
+      } else if (this.importType === 1 && this.importId) {
+        this.postForm.importFoodId = this.importId
+      }
       this.submitForm()
     },
     handleChange(data) {

+ 56 - 25
src/views/food/nutrient.vue

@@ -99,7 +99,7 @@
           style="margin-left: 10px;"
           type="success"
           @click="handleImport">
-          从模板导入营养素
+          导入营养素
         </el-button>
       </el-row>
     </div>
@@ -231,20 +231,23 @@
       </el-table-column>
     </el-table>
 
-    <el-dialog title="从模板导入营养素" :visible.sync="dialogFormVisible" v-loading="templateLoading">
-      <el-form ref="dialogForm" label-position="left" label-width="60px" style="width: 80%; margin-left:50px;">
-        <el-form-item label="模板:" prop="templateId">
+    <el-dialog title="导入营养素" :visible.sync="dialogFormVisible" v-loading="importLoading">
+      <el-form ref="dialogForm" label-position="left" style="width: 80%; margin-left:50px;">
+        <el-radio :label="0" v-model="importType" @change="handleImportTypeChange" >从模板导入</el-radio>
+        <el-radio :label="1" v-model="importType" @change="handleImportTypeChange" style="margin-bottom: 10px">从食物导入</el-radio>
+        <el-form-item prop="importId">
           <el-select
-            v-model="templateId"
+            v-model="importId"
             class="filter-item"
             filterable
             remote
             reserve-keyword
-            placeholder="请输入模板关键词"
-            :remote-method="queryTemplates"
+            placeholder="请输入关键词"
+            :loading="importLoading"
+            :remote-method="queryImportItems"
             style="width: 100%"
           >
-            <el-option v-for="item in templates" :key="`template${item.id}`" :label="item.name" :value="item.id" />
+            <el-option v-for="item in importItems" :key="`import${item.id}`" :label="item.name" :value="item.id" />
           </el-select>
         </el-form-item>
       </el-form>
@@ -252,7 +255,7 @@
         <el-button @click="dialogFormVisible = false">
           取消
         </el-button>
-        <el-button type="primary" @click="submitFromTemplate">
+        <el-button type="primary" @click="submitImport">
           提交
         </el-button>
       </div>
@@ -262,7 +265,8 @@
 
 <script>
 import { getNutrientList, addFoodNutrient, updateFoodNutrient, removeFoodNutrient,
-  updateFoodNutrientSort, confirmDeleteFoodNutrient, importNutrientsFromTemplate } from '@/api/food'
+  updateFoodNutrientSort, confirmDeleteFoodNutrient, importNutrientsFromTemplate,
+  getList as getFoodList, importNutrientFromSimilarFood } from '@/api/food'
 import FloatingWindow from '@/components/FloatingWindow'
 import { getList, getNutrientUnits } from '@/api/nutrient'
 import { getList as getUnits } from '@/api/unit'
@@ -311,9 +315,10 @@ export default {
       unitLoading: false,
       sources: [{ value: "营养标签" }, { value: "食品官方资料" }, { value: "计算值" }],
       dialogFormVisible: false,
-      templateLoading: false,
-      templateId: '',
-      templates: [],
+      importType: 0,
+      importId: '',
+      importLoading: false,
+      importItems: [],
       isFixed: false,
       offsetTop: 0
     }
@@ -445,31 +450,57 @@ export default {
       event.currentTarget.select();
     },
     handleImport() {
-      this.queryTemplates()
-      this.dialogFormVisible = true
       this.query = ''
-      this.templateId = ''
+      this.importId = ''
+      this.importType = 0
+      this.importItems = []
+      this.queryImportItems()
+      this.dialogFormVisible = true
+    },
+    handleImportTypeChange() {
+      this.importId = ''
+      this.importItems = []
+      this.queryImportItems()
+    },
+    queryImportItems(query=''){
+      this.importType === 0 ? this.queryTemplates(query) : this.queryFoods(query)
     },
     queryTemplates(query='') {
+      this.importLoading = true
       getNutrientTemplates({ query }).then(res => {
-        this.templates = res.data.list
-        if (this.templates.length > 0) {
-          this.templateId = this.templates[0].id
+        this.importItems = res.data.list
+        if (this.importItems.length > 0) {
+          this.importId = this.importItems[0].id
         }
+        this.importLoading = false
+      }).catch(() => {
+        this.importItems = []
+        this.importLoading = false
+      })
+    },
+    queryFoods(query='') {
+      this.importLoading = true
+      getFoodList({ query: query, orderBy: 0, pageNum: 1, pageSize: 20 }).then(res => {
+        this.importItems = res.data.list
+        this.importLoading = false
       }).catch(() => {
-        this.templates = []
+        this.importItems = []
+        this.importLoading = false
       })
     },
-    submitFromTemplate() {
-      this.templateLoading = true
-      importNutrientsFromTemplate(this.foodId, { templateId: this.templateId }).then(res => {
+    submitImport() {
+      this.importLoading = true
+      const resPromise = this.importType === 0 ?
+        importNutrientsFromTemplate(this.foodId, { templateId: this.importId }) :
+        importNutrientFromSimilarFood(this.foodId, { foodId: this.importId })
+      resPromise.then(res => {
         this.fetchData()
         this.$notify.success("提交成功")
         this.dialogFormVisible = false
-        this.templateLoading = false
+        this.importLoading = false
       }).catch(res => {
         this.$message.error(res.data.message)
-        this.templateLoading = false
+        this.importLoading = false
         this.dialogFormVisible = false
       })
     },

+ 1 - 0
src/views/foodModifier/components/ModifierImport.vue

@@ -50,6 +50,7 @@ export default {
         this.foods = res.data.list
         this.foodLoading = false
       }).catch(() => {
+        this.foods = []
         this.foodLoading = false
       })
     },