wwf
7 小时以前 a1d7e81859f554f3a53680cc35f0f49bf1f77098
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<template>
  <div
    class="relative inline-block"
    @mouseenter="showTooltipHandler"
    @mouseleave="hideTooltipHandler"
  >
    <!-- 文件上传按钮 -->
    <el-button
      v-if="!disabled"
      circle
      size="small"
      class="upload-btn relative transition-all-200ms"
      :class="{ 'has-files': fileList.length > 0 }"
      @click="triggerFileInput"
      :disabled="fileList.length >= limit"
    >
      <Icon icon="ep:paperclip" :size="16" />
      <!-- 文件数量徽章 -->
      <span
        v-if="fileList.length > 0"
        class="absolute -top-1 -right-1 bg-red-500 text-white text-10px px-1 rounded-8px min-w-4 h-4 flex items-center justify-center leading-none font-medium"
      >
        {{ fileList.length }}
      </span>
    </el-button>
 
    <!-- 隐藏的文件输入框 -->
    <input
      ref="fileInputRef"
      type="file"
      multiple
      style="display: none"
      :accept="acceptTypes"
      @change="handleFileSelect"
    />
 
    <!-- Hover 显示的文件列表 -->
    <div
      v-if="fileList.length > 0 && showTooltip"
      class="file-tooltip"
      @mouseenter="showTooltipHandler"
      @mouseleave="hideTooltipHandler"
    >
      <div class="tooltip-arrow"></div>
      <div class="max-h-200px overflow-y-auto file-list">
        <div
          v-for="(file, index) in fileList"
          :key="index"
          class="flex items-center justify-between p-2 mb-1 bg-gray-50 rounded-6px text-12px transition-all-200ms last:mb-0 hover:bg-gray-100"
          :class="{ 'opacity-70': file.uploading }"
        >
          <div class="flex items-center flex-1 min-w-0">
            <Icon :icon="getFileIcon(file.name)" class="text-blue-500 mr-2 flex-shrink-0" />
            <span
              class="font-medium text-gray-900 mr-1 overflow-hidden text-ellipsis whitespace-nowrap flex-1"
              >{{ file.name }}</span
            >
            <span class="text-gray-500 flex-shrink-0 text-11px"
              >({{ formatFileSize(file.size) }})</span
            >
          </div>
          <div class="flex items-center gap-1 flex-shrink-0 ml-2">
            <el-progress
              v-if="file.uploading"
              :percentage="file.progress || 0"
              :show-text="false"
              size="small"
              class="w-60px"
            />
            <el-button
              v-else-if="!disabled"
              link
              type="danger"
              size="small"
              @click="removeFile(index)"
            >
              <Icon icon="ep:close" :size="12" />
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { useUpload } from '@/components/UploadFile/src/useUpload'
import { formatFileSize, getFileIcon } from '@/utils/file'
 
export interface FileItem {
  name: string
  size: number
  url?: string
  uploading?: boolean
  progress?: number
  raw?: File
}
 
defineOptions({ name: 'MessageFileUpload' })
 
const props = defineProps({
  modelValue: {
    type: Array as PropType<string[]>,
    default: () => []
  },
  limit: {
    type: Number,
    default: 5
  },
  maxSize: {
    type: Number,
    default: 10 // MB
  },
  acceptTypes: {
    type: String,
    default: '.jpg,.jpeg,.png,.gif,.webp,.pdf,.doc,.docx,.txt,.xls,.xlsx,.ppt,.pptx,.csv,.md'
  },
  disabled: {
    type: Boolean,
    default: false
  }
})
 
const emit = defineEmits(['update:modelValue', 'upload-success', 'upload-error'])
 
const fileInputRef = ref<HTMLInputElement>()
const fileList = ref<FileItem[]>([]) // 内部管理文件列表
const uploadedUrls = ref<string[]>([]) // 已上传的 URL 列表
const showTooltip = ref(false) // 控制 tooltip 显示
const hideTimer = ref<NodeJS.Timeout | null>(null) // 隐藏延迟定时器
const message = useMessage()
const { httpRequest } = useUpload()
 
/** 监听 v-model 变化 */
watch(
  () => props.modelValue,
  (newVal) => {
    uploadedUrls.value = [...newVal]
    // 如果外部清空了 URLs,也清空内部文件列表
    if (newVal.length === 0) {
      fileList.value = []
    }
  },
  { immediate: true, deep: true }
)
 
/** 触发文件选择 */
const triggerFileInput = () => {
  fileInputRef.value?.click()
}
 
/** 显示 tooltip */
const showTooltipHandler = () => {
  if (hideTimer.value) {
    clearTimeout(hideTimer.value)
    hideTimer.value = null
  }
  showTooltip.value = true
}
 
/** 隐藏 tooltip */
const hideTooltipHandler = () => {
  hideTimer.value = setTimeout(() => {
    showTooltip.value = false
    hideTimer.value = null
  }, 300) // 300ms 延迟隐藏
}
 
/** 处理文件选择 */
const handleFileSelect = (event: Event) => {
  const target = event.target as HTMLInputElement
  const files = Array.from(target.files || [])
  if (files.length === 0) {
    return
  }
  // 检查总文件数是否超过限制
  if (files.length + fileList.value.length > props.limit) {
    message.error(`最多只能上传 ${props.limit} 个文件`)
    target.value = '' // 清空输入
    return
  }
  // 处理每个文件
  files.forEach((file) => {
    if (file.size > props.maxSize * 1024 * 1024) {
      message.error(`文件 ${file.name} 大小超过 ${props.maxSize}MB`)
      return
    }
    const fileItem: FileItem = {
      name: file.name,
      size: file.size,
      uploading: true,
      progress: 0,
      raw: file
    }
    fileList.value.push(fileItem)
    // 立即开始上传
    uploadFile(fileItem)
  })
 
  // 清空 input 值,允许重复选择相同文件
  target.value = ''
}
 
/** 上传文件 */
const uploadFile = async (fileItem: FileItem) => {
  try {
    // 模拟上传进度
    const progressInterval = setInterval(() => {
      if (fileItem.progress! < 90) {
        fileItem.progress = (fileItem.progress || 0) + Math.random() * 10
      }
    }, 100)
 
    // 调用上传接口
    // const formData = new FormData()
    // formData.append('file', fileItem.raw!)
    const response = await httpRequest({
      file: fileItem.raw!,
      filename: fileItem.name
    } as any)
    fileItem.uploading = false
    fileItem.progress = 100
    fileItem.url = (response as any).data
    // 添加到 URL 列表
    uploadedUrls.value.push(fileItem.url!)
 
    clearInterval(progressInterval)
 
    emit('upload-success', fileItem)
    updateModelValue()
  } catch (error) {
    fileItem.uploading = false
    message.error(`文件 ${fileItem.name} 上传失败`)
    emit('upload-error', error)
 
    // 移除上传失败的文件
    const index = fileList.value.indexOf(fileItem)
    if (index > -1) {
      removeFile(index)
    }
  }
}
 
/** 删除文件 */
const removeFile = (index: number) => {
  // 从 URL 列表中移除
  const removedFile = fileList.value[index]
  fileList.value.splice(index, 1)
  if (removedFile.url) {
    const urlIndex = uploadedUrls.value.indexOf(removedFile.url)
    if (urlIndex > -1) {
      uploadedUrls.value.splice(urlIndex, 1)
    }
  }
 
  updateModelValue()
}
 
/** 更新 v-model */
const updateModelValue = () => {
  emit('update:modelValue', [...uploadedUrls.value])
}
 
// 暴露方法
defineExpose({
  triggerFileInput,
  clearFiles: () => {
    fileList.value = []
    uploadedUrls.value = []
    updateModelValue()
  }
})
 
// 组件销毁时清理定时器
onUnmounted(() => {
  if (hideTimer.value) {
    clearTimeout(hideTimer.value)
  }
})
</script>
 
<style scoped>
/* 上传按钮样式 */
.upload-btn {
  --el-button-bg-color: transparent;
  --el-button-border-color: transparent;
  --el-button-hover-bg-color: var(--el-fill-color-light);
  --el-button-hover-border-color: transparent;
  color: var(--el-text-color-regular);
}
 
.upload-btn.has-files {
  color: var(--el-color-primary);
  --el-button-hover-bg-color: var(--el-color-primary-light-9);
}
 
.file-tooltip {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  min-width: 240px;
  max-width: 320px;
  padding: 8px;
  animation: fadeInDown 0.2s ease;
}
 
.tooltip-arrow {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid var(--el-border-color-light);
}
 
/* Tooltip 箭头伪元素 */
.tooltip-arrow::after {
  content: '';
  position: absolute;
  bottom: 1px;
  left: -4px;
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 4px solid white;
}
 
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
 
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
 
/* 滚动条样式 */
.file-list::-webkit-scrollbar {
  width: 4px;
}
 
.file-list::-webkit-scrollbar-track {
  background: transparent;
}
 
.file-list::-webkit-scrollbar-thumb {
  background: var(--el-border-color-light);
  border-radius: 2px;
}
 
.file-list::-webkit-scrollbar-thumb:hover {
  background: var(--el-border-color);
}
/* 滚动条样式 */
.file-list::-webkit-scrollbar {
  width: 4px;
}
 
.file-list::-webkit-scrollbar-track {
  background: transparent;
}
 
.file-list::-webkit-scrollbar-thumb {
  background: var(--el-border-color-light);
  border-radius: 2px;
}
 
.file-list::-webkit-scrollbar-thumb:hover {
  background: var(--el-border-color);
}
</style>