hejianhao
2026-04-02 0940d56748a73525790c350c393a5be960aab62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!-- 异常检测项目批量导入时的类别选择对话框 -->
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="450px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleCancel"
  >
    <div class="anomaly-category-content">
      <!-- 导入信息 -->
      <div class="import-info">
        <el-icon class="info-icon"><InfoFilled /></el-icon>
        <span>即将导入 <strong>{{ imageCount }}</strong> 张图片</span>
      </div>
 
      <!-- 类别选择 -->
      <div class="category-section">
        <div class="section-label">
          <el-checkbox v-model="enableCategorySelection" size="large">
            为图片添加类别标注
          </el-checkbox>
        </div>
        
        <!-- 类别单选按钮组 -->
        <el-radio-group 
          v-model="selectedCategoryKey" 
          class="category-radio-group"
          :disabled="!enableCategorySelection"
        >
          <el-radio
            v-for="category in existingCategories"
            :key="category.key"
            :label="category.key"
            class="category-radio-item"
          >
            <div class="category-option">
              <span 
                class="category-color-dot" 
                :style="{ backgroundColor: category.color }"
              ></span>
              <span class="category-title">{{ category.title }}</span>
              <span class="category-hint" v-if="category.key === 0">
                (正常样本)
              </span>
            </div>
          </el-radio>
        </el-radio-group>
 
        <!-- 类别说明 -->
        <div class="category-tips" v-if="enableCategorySelection">
          <el-alert
            :title="getCategoryTips()"
            type="info"
            :closable="false"
            show-icon
          />
        </div>
        
        <!-- 不选择类别的说明 -->
        <div class="no-category-tips" v-else>
          <el-alert
            title="直接导入图片,不添加任何标注"
            type="warning"
            :closable="false"
            show-icon
          />
        </div>
      </div>
    </div>
 
    <!-- 底部按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确定导入
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, computed, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { InfoFilled } from '@element-plus/icons-vue';
 
// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  imageCount: {
    type: Number,
    default: 0
  },
  existingCategories: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '批量导入异常检测图片'
  },
  projectType: {
    type: String,
    default: 'anomaly' // 'anomaly' 或 'defect'
  }
});
 
// Emits
const emit = defineEmits(['update:visible', 'confirm', 'cancel']);
 
// 响应式数据
const dialogVisible = computed({
  get: () => props.visible,
  set: (val) => emit('update:visible', val)
});
 
const selectedCategoryKey = ref('');
const enableCategorySelection = ref(false);
 
// 获取类别提示信息
const getCategoryTips = () => {
  if (selectedCategoryKey.value === 0) {
    return props.projectType === 'defect' 
      ? '选择 "good" 表示这些是正常样本,用于生成缺陷的基础'
      : '选择 "good" 表示这些是正常样本,用于训练正常模式';
  } else if (selectedCategoryKey.value !== null && selectedCategoryKey.value !== '') {
    return props.projectType === 'defect'
      ? `选择 "${getSelectedCategoryTitle()}" 表示这些图片包含该类型的缺陷`
      : `选择 "${getSelectedCategoryTitle()}" 表示这些图片包含该类型的异常`;
  }
  return '请选择要导入图片的类别';
};
 
// 获取选中类别的标题
const getSelectedCategoryTitle = () => {
  const category = props.existingCategories.find(c => c.key === selectedCategoryKey.value);
  return category ? category.title : '';
};
 
// 方法
const handleConfirm = () => {
  console.log('[AnomalyCategoryDialog] ========== handleConfirm 开始 ==========');
  console.log('[AnomalyCategoryDialog] 启用类别选择:', enableCategorySelection.value);
  console.log('[AnomalyCategoryDialog] 选中的类别key:', selectedCategoryKey.value);
  console.log('[AnomalyCategoryDialog] 待处理图片数量:', props.imageCount);
  console.log('[AnomalyCategoryDialog] 项目类型:', props.projectType);
  console.log('[AnomalyCategoryDialog] 现有类别:', props.existingCategories);
  
  // 如果启用了类别选择
  if (enableCategorySelection.value) {
    if (selectedCategoryKey.value === '' || selectedCategoryKey.value === null || selectedCategoryKey.value === undefined) {
      console.warn('[AnomalyCategoryDialog] 未选择类别,提示用户');
      ElMessage.warning('请选择一个类别');
      return;
    }
 
    const selectedCategory = props.existingCategories.find(
      c => c.key === selectedCategoryKey.value
    );
    
    if (!selectedCategory) {
      console.error('[AnomalyCategoryDialog] 选择的类别不存在,key:', selectedCategoryKey.value);
      ElMessage.error('选择的类别不存在');
      return;
    }
    
    console.log('[AnomalyCategoryDialog] 找到选中的类别:', selectedCategory);
    const result = {
      isNew: false,
      key: selectedCategory.key,
      title: selectedCategory.title,
      color: selectedCategory.color,
      skipAnnotation: false
    };
    console.log('[AnomalyCategoryDialog] 发送confirm事件,结果:', result);
 
    emit('confirm', result);
  } else {
    // 不选择类别,直接导入
    console.log('[AnomalyCategoryDialog] 不选择类别,直接导入');
    const result = {
      skipAnnotation: true
    };
    console.log('[AnomalyCategoryDialog] 发送confirm事件,结果:', result);
    
    emit('confirm', result);
  }
  
  console.log('[AnomalyCategoryDialog] 重置表单');
  resetForm();
};
 
const handleCancel = () => {
  console.log('[AnomalyCategoryDialog] 用户取消操作');
  emit('cancel');
  resetForm();
};
 
const resetForm = () => {
  selectedCategoryKey.value = '';
  enableCategorySelection.value = false;
};
 
// 监听对话框打开
watch(() => props.visible, (newVal) => {
  if (newVal) {
    console.log('[AnomalyCategoryDialog] 对话框打开');
    console.log('[AnomalyCategoryDialog] 图片数量:', props.imageCount);
    console.log('[AnomalyCategoryDialog] 可用类别:', props.existingCategories);
    // 重置表单
    resetForm();
  }
});
 
// 监听类别选择开关
watch(enableCategorySelection, (newVal) => {
  console.log('[AnomalyCategoryDialog] 类别选择开关变化:', newVal);
  if (newVal) {
    // 当启用类别选择时,设置默认选中
    // 如果只有一个类别,自动选中
    if (props.existingCategories.length === 1) {
      selectedCategoryKey.value = props.existingCategories[0].key;
      console.log('[AnomalyCategoryDialog] 只有一个类别,自动选中:', selectedCategoryKey.value);
    }
    // 如果有 "good" 类别(key为0),默认选中它
    else if (props.existingCategories.some(c => c.key === 0)) {
      selectedCategoryKey.value = 0;
      console.log('[AnomalyCategoryDialog] 默认选中good类别, key: 0');
    }
  } else {
    // 当禁用类别选择时,清空选择
    selectedCategoryKey.value = '';
    console.log('[AnomalyCategoryDialog] 禁用类别选择,清空选择');
  }
});
</script>
 
<style scoped lang="scss">
.anomaly-category-content {
  .import-info {
    display: flex;
    align-items: center;
    padding: 12px;
    background: #f0f9ff;
    border-radius: 6px;
    margin-bottom: 20px;
    color: #409eff;
    
    .info-icon {
      margin-right: 8px;
      font-size: 18px;
    }
    
    strong {
      font-weight: 600;
      color: #409eff;
    }
  }
  
  .category-section {
    .section-label {
      font-size: 14px;
      color: #606266;
      margin-bottom: 16px;
      font-weight: 500;
    }
    
    .category-radio-group {
      display: flex;
      flex-direction: column;
      gap: 12px;
      margin-bottom: 20px;
      
      .category-radio-item {
        width: 100%;
        margin: 0;
        
        :deep(.el-radio__input) {
          vertical-align: top;
          margin-top: 3px;
        }
        
        :deep(.el-radio__label) {
          width: calc(100% - 20px);
          padding: 0;
        }
        
        .category-option {
          display: flex;
          align-items: center;
          padding: 10px 12px;
          background: #f5f7fa;
          border-radius: 6px;
          transition: all 0.2s;
          width: 100%;
          
          &:hover {
            background: #e6f1ff;
          }
          
          .category-color-dot {
            width: 16px;
            height: 16px;
            border-radius: 4px;
            margin-right: 10px;
            flex-shrink: 0;
          }
          
          .category-title {
            font-size: 14px;
            font-weight: 500;
            color: #303133;
            text-align: left;
          }
          
          .category-hint {
            font-size: 12px;
            color: #909399;
            margin-left: 8px;
          }
        }
      }
      
      :deep(.el-radio.is-checked) {
        .category-option {
          background: #e6f1ff;
          border: 1px solid #409eff;
          
          .category-title {
            color: #409eff;
          }
        }
      }
    }
    
    .category-tips,
    .no-category-tips {
      :deep(.el-alert) {
        padding: 8px 12px;
        
        .el-alert__title {
          font-size: 13px;
          line-height: 1.4;
        }
      }
    }
    
    .category-radio-group:disabled {
      opacity: 0.5;
      pointer-events: none;
    }
  }
}
 
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>