Преглед изворни кода

fix(公告表单): 修复客户黑名单数据处理和分类选项更新问题作后自动更新分类字典

yz пре 2 недеља
родитељ
комит
ac2d457cf8

+ 15 - 4
src/components/announcement/announcement-form-mixin.js

@@ -391,12 +391,23 @@ export default {
      */
     'formData.customerBlacklist': {
       handler(newVal) {
-        if (Array.isArray(newVal)) {
-          this.selectedCustomerIds = newVal.map(item => item.ID || item.id).filter(id => id != null);
+        // 兼容 newVal 不是数组或数组中存在 undefined/null 的情况,同时允许直接传入 ID 数组
+        if (!Array.isArray(newVal)) {
+          this.selectedCustomerIds = [];
+          return;
         }
+        const ids = newVal
+          .filter(item => item != null)
+          .map(item => {
+            if (typeof item === 'number' || typeof item === 'string') return item; // 直接是 ID
+            // 兼容不同字段命名:ID/id/value
+            return (item && (item.ID ?? item.id ?? item.value)) ?? null;
+          })
+          .filter(id => id != null);
+    
+        this.selectedCustomerIds = ids;
       },
-      deep: true,
-      immediate: true
+      deep: true
     }
   },
 

+ 24 - 0
src/views/announcement/mixins/announcementIndex.js

@@ -385,6 +385,18 @@ export default {
         this.loadCategoryOptions();
     },
 
+    watch: {
+        announcementFormVisible(val) {
+            if (val === true) {
+                // 打开表单时,立即重新加载分类列表(表单与列表都会受益)
+                this.loadCategoryOptions();
+            } else {
+                // 关闭表单后,avue-crud 会通过 v-if 重新渲染,需等 DOM 恢复后再刷新字典
+                this.$nextTick(() => this.loadCategoryOptions());
+            }
+        }
+    },
+
     methods: {
         // 导入常量工具函数
         calculateRolesMask,
@@ -427,6 +439,10 @@ export default {
                 const categoryColumn = this.option.column.find((/** @type {TableColumn} */ col) => col.prop === 'categoryName');
                 if (categoryColumn) {
                     categoryColumn.dicData = this.categoryOptions;
+                    // 重新初始化 avue-crud 的字典,确保搜索下拉及时更新
+                    if (this.$refs && this.$refs.crud && typeof this.$refs.crud.dicInit === 'function') {
+                        this.$nextTick(() => this.$refs.crud.dicInit());
+                    }
                 }
             } catch (error) {
                 console.error('加载分类选项失败:', error);
@@ -442,6 +458,10 @@ export default {
                 const categoryColumn = this.option.column.find((/** @type {TableColumn} */ col) => col.prop === 'categoryName');
                 if (categoryColumn) {
                     categoryColumn.dicData = this.categoryOptions;
+                    // 重新初始化 avue-crud 的字典,确保搜索下拉及时更新
+                    if (this.$refs && this.$refs.crud && typeof this.$refs.crud.dicInit === 'function') {
+                        this.$nextTick(() => this.$refs.crud.dicInit());
+                    }
                 }
             }
         },
@@ -564,6 +584,8 @@ export default {
          * @this {import('@/views/announcement/types').AnnouncementComponent & import('vue').default}
          */
         refreshChange() {
+            // 刷新列表与分类字典
+            this.loadCategoryOptions();
             this.onLoad(this.page, this.query);
         },
 
@@ -810,6 +832,8 @@ export default {
             this.announcementFormVisible = false;
             this.isEditMode = false;
             this.editAnnouncementId = null;
+            // 刷新分类字典
+            this.loadCategoryOptions();
             // 刷新列表数据
             this.onLoad(this.page);
         }