Jelajahi Sumber

代码优化

Guos 2 hari lalu
induk
melakukan
dd5038e0ee
1 mengubah file dengan 41 tambahan dan 44 penghapusan
  1. 41 44
      src/views/hisStore/storeProduct/index.vue

+ 41 - 44
src/views/hisStore/storeProduct/index.vue

@@ -1298,19 +1298,13 @@ export default {
     businessArr:function(val) {
       this.form.business = val.join(',');
     },
+    // 监听商品分类变化
     'form.cateId': {
-      handler(newVal) {
+      handler(newVal, oldVal) {
         // 分类变化时动态修改验证规则
         if (newVal == 224) {
           this.rules.indications = [];
           this.rules.dosage = [];
-        } else {
-          // this.rules.indications = [
-          //   { required: true, message: "功能主治不能为空", trigger: "blur" }
-          // ];
-          // this.rules.dosage = [
-          //   { required: true, message: "用法用量不能为空", trigger: "blur" }
-          // ];
         }
         const cateName = this.cateIdToNameMap[newVal];
         if(cateName !== undefined && cateName.includes('器械')){
@@ -1325,35 +1319,30 @@ export default {
             this.$refs.form.clearValidate(['indications', 'dosage']);
           }
         });
+
+        // 只有当两个值都存在时才进行检查(排除初始化情况)
+        if (newVal && this.form.storeId && this.isFormInitialized) {
+          this.debounceCheckStoreLicense();
+        }
       },
       immediate: true
     },
-    // 合并监听 form.cateId 和 form.storeId
-    form: {
+
+    // 监听所属店铺变化
+    'form.storeId': {
       handler(newVal, oldVal) {
-        // 只有当两个值都存在且至少有一个发生变化时才进行检查
-        if (newVal.cateId && newVal.storeId) {
-          // 避免在初始化时重复调用,只在值真正改变时调用
-          const oldCateId = oldVal ? oldVal.cateId : null;
-          const oldStoreId = oldVal ? oldVal.storeId : null;
-
-          // 只有当至少一个值发生变化时才检查
-          if (newVal.cateId !== oldCateId || newVal.storeId !== oldStoreId) {
-            // 添加防抖,避免短时间内多次调用
-            clearTimeout(this.licenseCheckTimer);
-            this.licenseCheckTimer = setTimeout(() => {
-              this.checkStoreLicensePermission();
-            }, 300);
-          }
+        // 只有当两个值都存在时才进行检查(排除初始化情况)
+        if (newVal && this.form.cateId && this.isFormInitialized) {
+          this.debounceCheckStoreLicense();
         }
-      },
-      deep: true
+      }
     }
   },
   data() {
     return {
-      isCheckingLicense: false, // 正在检查许可证标志
-      licenseCheckTimer: null, // 添加防抖定时器
+      storeLicenseErrorMessage: '', // 店铺许可证错误信息
+      isFormInitialized: false, // 表单是否已初始化完成
+      licenseCheckTimer: null,  // 防抖定时器
       storeLicenseCheckFailed: false, // 店铺许可证接口检查是否失败
       auditLogs:[],
       titleValue:null,
@@ -1691,9 +1680,14 @@ export default {
   //   }
   // },
   methods: {
-
+    // 防抖检查店铺许可证权限
+    debounceCheckStoreLicense() {
+      clearTimeout(this.licenseCheckTimer);
+      this.licenseCheckTimer = setTimeout(() => {
+        this.checkStoreLicensePermission();
+      }, 300);
+    },
     // 检查店铺许可证权限
-    // 修改 checkStoreLicensePermission 方法
     checkStoreLicensePermission() {
       // 确保分类ID和店铺ID都存在
       if (!this.form.cateId || !this.form.storeId) {
@@ -1704,32 +1698,37 @@ export default {
       if (this.isCheckingLicense) {
         return;
       }
-
       this.isCheckingLicense = true;
-
       checkStoreLicense(this.form.storeId, this.form.cateId).then(response => {
         if (response.code === 200) {
           if (!response.data.flag) {
             // 权限检查失败
-            this.$message.error(response.data.message || '当前店铺无权限经营该类商品');
+            const errorMessage = response.data.message || '当前店铺无权限经营该类商品';
+            this.$message.error(errorMessage);
             this.storeLicenseCheckFailed = true;
+            this.storeLicenseErrorMessage = errorMessage;
           } else {
-            // 权限检查通过
+            // 权限检查通过,清除之前的错误提示
             this.storeLicenseCheckFailed = false;
+            this.storeLicenseErrorMessage = ''; // 清除错误信息
+            // 如果之前有显示错误信息的地方,也会被清除
           }
         } else {
-          this.$message.error('店铺许可证检查接口调用失败');
+          const errorMessage = '店铺许可证检查接口调用失败';
+          this.$message.error(errorMessage);
+          this.storeLicenseCheckFailed = true;
+          this.storeLicenseErrorMessage = errorMessage;
         }
       }).catch(error => {
         console.error('店铺许可证检查失败:', error);
-        this.$message.error('店铺许可证检查失败');
+        const errorMessage = '店铺许可证检查失败';
+        this.$message.error(errorMessage);
         this.storeLicenseCheckFailed = true;
+        this.storeLicenseErrorMessage = errorMessage;
       }).finally(() => {
         this.isCheckingLicense = false;
       });
     },
-
-
     // 成分关键字检查
     checkForbiddenKeywords() {
       // Clear error first
@@ -2039,6 +2038,7 @@ export default {
     },
     // 表单重置
     reset() {
+      this.isFormInitialized = false; // 重置初始化标志
       this.ingredientError = '';
       this.form = {
         ingredient: null,
@@ -2178,7 +2178,7 @@ export default {
     handleAdd() {
       this.reset();
       this.clearExpireErrors(); // 清空过期错误提示
-
+      this.isFormInitialized = true; // 新增时直接设置为已初始化
       this.open = true;
       this.title = "添加商品";
       setTimeout(() => {
@@ -2324,11 +2324,8 @@ export default {
         this.title = "修改商品";
         // 加载数据后立即校验过期状态
         this.$nextTick(() => {
-          // 清除之前的定时器
-          if (this.licenseCheckTimer) {
-            clearTimeout(this.licenseCheckTimer);
-          }
-          // 直接调用检查方法
+          this.isFormInitialized = true;
+          // 首次加载后检查一次
           if (this.form.storeId && this.form.cateId) {
             this.checkStoreLicensePermission();
           }