Procházet zdrojové kódy

修复营养物来源下拉框无反应的bug

wangyang před 4 roky
rodič
revize
5d8d8e83fa

+ 0 - 1
src/store/modules/user.js

@@ -1,6 +1,5 @@
 import { login, logout, getInfo } from '@/api/user'
 import { getToken, setToken, removeToken } from '@/utils/auth'
-import { resetRouter } from '@/router'
 import { Message } from 'element-ui'
 
 const getDefaultState = () => {

+ 8 - 27
src/utils/request.js

@@ -1,5 +1,6 @@
 import axios from 'axios'
 import { MessageBox, Message } from 'element-ui'
+import router from '@/router'
 import store from '@/store'
 import { getToken } from '@/utils/auth'
 
@@ -45,35 +46,15 @@ service.interceptors.response.use(
    */
   response => {
     return response
-    // const res = response.data
-    //
-    // // if the custom code is not 20000, it is judged as an error.
-    // if (res.code !== 20000) {
-    //   Message({
-    //     message: res.message || 'Error',
-    //     type: 'error',
-    //     duration: 5 * 1000
-    //   })
-    //
-    //   // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
-    //   if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
-    //     // to re-login
-    //     MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
-    //       confirmButtonText: 'Re-Login',
-    //       cancelButtonText: 'Cancel',
-    //       type: 'warning'
-    //     }).then(() => {
-    //       store.dispatch('user/resetToken').then(() => {
-    //         location.reload()
-    //       })
-    //     })
-    //   }
-    //   return Promise.reject(new Error(res.message || 'Error'))
-    // } else {
-    //   return res
-    // }
   },
   error => {
+    // 如果为401则返回登录页
+    if (error.response.status === 401) {
+      Message({ message: '请重新登录', type: 'error', duration: 5 * 1000 })
+      store.dispatch('user/resetToken').then(() => {
+        router.replace({ path: '/login' })
+      })
+    }
     // 自行处理400+ 服务端定义的异常
     if (error.response.status < 400 || error.response.status >= 500) {
       Message({

+ 5 - 4
src/views/food/components/NutrientUnit.vue

@@ -10,6 +10,7 @@
       clearable
       v-show="showRowNutrientSource"
       v-model="data.nutrientSource"
+      :value-key="data.nutrientId"
       placeholder="请选择营养素来源"
     >
       <el-option v-for="item in nutrientSources" :key="item" :label="item" :value="item" />
@@ -45,17 +46,17 @@ export default {
           this.nutrientSources = res.data
           if (this.nutrientSources.length > 0){
             if (!data.nutrientSource) {
-              data.nutrientSource = this.nutrientSources[0]
+              this.$set(data, 'nutrientSource', this.nutrientSources[0])
             }
             this.showRowNutrientSource = true
           } else {
             this.showRowNutrientSource = false
-            data.nutrientSource = null
+            this.$set(data, 'nutrientSource', null)
           }
         })
       } else {
         this.showRowNutrientSource = false
-        data.nutrientSource = null
+        this.$set(data, 'nutrientSource', null)
       }
     },
     queryNutrientUnits(query, nutrientId, cb) {
@@ -66,7 +67,7 @@ export default {
           cb(units)
         })
       }
-    },
+    }
   },
   watch: {
     edit(value) {

+ 4 - 4
src/views/food/nutrient.vue

@@ -595,7 +595,7 @@ export default {
         getNutrientSources(this.params.nutrientId, { unit: this.params.unit }).then(res => {
           this.nutrientSources = res.data
           if (this.nutrientSources.length > 0){
-            this.params.nutrientSource = this.nutrientSources[0]
+            this.$set(this.params, "nutrientSource", this.nutrientSources[0])
             this.showNutrientSource = true
           } else {
             this.showNutrientSource = false
@@ -613,17 +613,17 @@ export default {
           this.nutrientSources = res.data
           if (this.nutrientSources.length > 0){
             if (!row.nutrientSource) {
-              row.nutrientSource = this.nutrientSources[0]
+              this.$set(row, 'nutrientSource', this.nutrientSources[0])
             }
             this.showRowNutrientSource = true
           } else {
             this.showRowNutrientSource = false
-            row.nutrientSource = null
+            this.$set(row, 'nutrientSource', null)
           }
         })
       } else {
         this.showRowNutrientSource = false
-        row.nutrientSource = null
+        this.$set(row, 'nutrientSource', null)
       }
     }
   }