sd
2025-08-05 72b025d6b43271ae88541ea23c92070b3b2acc96
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
<template>
  <el-dialog :title="`导入${importLabel}`" :visible.sync="visible" width="600px">
    <div class="batch-import-container">
      <!-- 导入类型选择 -->
      <el-form label-width="100px" v-if="showTypeSelector">
        <el-form-item label="样本类型">
          <el-radio-group v-model="importType">
            <el-radio :label="1">正样本</el-radio>
            <el-radio :label="2">负样本</el-radio>
            <el-radio :label="3">待标记样本</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      
      <!-- 上传区域 -->
      <div class="upload-area" @dragover.prevent @drop="onDrop">
        <div class="upload-content">
          <i class="el-icon-upload"></i>
          <div class="upload-text">
            <p>将图片拖放到此处,或</p>
            <el-button type="primary">选择图片</el-button>
          </div>
          <p class="upload-hint">支持JPG/PNG格式,最多100张图片,单张最大5MB</p>
        </div>
        <input 
          type="file" 
          ref="fileInput" 
          class="file-input" 
          multiple 
          accept="image/*" 
          @change="handleFileChange"
        >
      </div>
      
      <!-- 图片预览区 -->
      <div class="image-preview">
        <div class="preview-header">
          <span>已选择图片 ({{ fileList.length }})</span>
          <el-button 
            type="text" 
            :disabled="fileList.length === 0" 
            @click="fileList = []"
          >
            清空
          </el-button>
        </div>
        
        <div class="preview-content">
          <div 
            class="preview-item" 
            v-for="(file, index) in fileList" 
            :key="index"
          >
            <img :src="getPreviewUrl(file)" alt="预览图">
            <div class="image-info">
              <span class="file-name">{{ file.name }}</span>
              <span class="file-size">{{ formatSize(file.size) }}</span>
            </div>
            <i 
              class="el-icon-delete" 
              title="删除"
              @click="removeFile(index)"
            ></i>
          </div>
          
          <div class="empty-hint" v-if="fileList.length === 0">
            暂未选择图片
          </div>
        </div>
      </div>
    </div>
    
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button 
        type="primary" 
        :disabled="fileList.length === 0" 
        @click="submitImport"
      >
        导 入
      </el-button>
    </div>
  </el-dialog>
</template>
 
<script>
export default {
  name: 'BatchImport',
  props: {
    // 是否显示类型选择器(用于区分导入按钮和下拉导入)
    showTypeSelector: {
      type: Boolean,
      default: true
    },
    // 预设的导入类型(1-正样本、2-负样本、3-待标记)
    presetType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      visible: false,
      importType: this.presetType || 1,
      fileList: []
    };
  },
  computed: {
    importLabel() {
      const labels = {1: '正样本', 2: '负样本', 3: '待标记样本'};
      return this.presetType ? labels[this.presetType] : labels[this.importType];
    }
  },
  methods: {
    open() {
      this.visible = true;
      this.fileList = [];
      if (!this.presetType) this.importType = 1;
    },
    
    close() {
      this.visible = false;
    },
    
    onDrop(e) {
      e.preventDefault();
      this.addFiles(e.dataTransfer.files);
    },
    
    handleFileChange(e) {
      this.addFiles(e.target.files);
      // 重置input以便可以再次选择相同文件
      e.target.value = null;
    },
    
    addFiles(fileList) {
      const files = Array.from(fileList);
      // 过滤非图片文件
      const validFiles = files.filter(file => file.type.startsWith('image/'));
      
      // 限制文件数量 (最多100张)
      if (this.fileList.length + validFiles.length > 100) {
        this.$message.warning('最多支持100张图片导入');
        return;
      }
      
      validFiles.forEach(file => {
        // 检查文件大小 (5MB以内)
        if (file.size > 5 * 1024 * 1024) {
          this.$message.warning(`文件 ${file.name} 超过5MB限制`);
          return;
        }
        this.fileList.push(file);
      });
    },
    
    getPreviewUrl(file) {
      return URL.createObjectURL(file);
    },
    
    formatSize(size) {
      if (size < 1024) return size + ' B';
      if (size < 1024 * 1024) return (size / 1024).toFixed(1) + ' KB';
      return (size / (1024 * 1024)).toFixed(1) + ' MB';
    },
    
    removeFile(index) {
      this.fileList.splice(index, 1);
    },
    
    submitImport() {
      const finalImportType = this.presetType || this.importType;
      const fileNames = this.fileList.map(f => f.name);
      
      this.$emit('import', {
        type: finalImportType,
        files: this.fileList
      });
      
    //   this.$message.success(`成功导入 ${fileNames.length} 张图片`);
      this.close();
    }
  }
};
</script>
 
<style scoped>
.batch-import-container {
  padding: 10px;
}
 
.upload-area {
  position: relative;
  border: 2px dashed #dcdfe6;
  border-radius: 6px;
  padding: 20px;
  text-align: center;
  margin-bottom: 20px;
  cursor: pointer;
  transition: border-color 0.3s;
}
 
.upload-area:hover {
  border-color: #409EFF;
}
 
.upload-content {
  pointer-events: none;
}
 
.upload-content i {
  font-size: 48px;
  color: #c0c4cc;
  margin-bottom: 15px;
}
 
.upload-text {
  margin-bottom: 10px;
}
 
.upload-hint {
  color: #909399;
  font-size: 12px;
  margin-top: 10px;
}
 
.file-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
 
.image-preview {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  max-height: 300px;
  overflow-y: auto;
}
 
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
}
 
.preview-content {
  padding: 10px;
}
 
.preview-item {
  position: relative;
  display: flex;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #f1f1f1;
}
 
.preview-item:hover {
  background-color: #f8f8f8;
}
 
.preview-item img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 4px;
  margin-right: 10px;
}
 
.image-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}
 
.file-name {
  font-size: 14px;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 380px;
}
 
.file-size {
  color: #909399;
  font-size: 12px;
}
 
.preview-item .el-icon-delete {
  color: #f56c6c;
  font-size: 16px;
  cursor: pointer;
}
 
.empty-hint {
  text-align: center;
  padding: 30px;
  color: #909399;
}
</style>