|
23 | 23 |
|
24 | 24 | // 检测文件类型 |
25 | 25 | const isVideo = computed(() => { |
26 | | - if (!currentFile.value) return false |
27 | | - console.log(mediaSrc.value) |
28 | | - return currentFile.value.type.startsWith('video/') |
| 26 | + // 如果有当前文件,根据文件类型判断 |
| 27 | + if (currentFile.value) { |
| 28 | + console.log(mediaSrc.value) |
| 29 | + return currentFile.value.type.startsWith('video/') |
| 30 | + } |
| 31 | + // 如果没有当前文件但有媒体源,根据URL判断 |
| 32 | + if (mediaSrc.value) { |
| 33 | + return mediaSrc.value.includes('.mp4') || mediaSrc.value.includes('.webm') || |
| 34 | + mediaSrc.value.includes('.avi') || mediaSrc.value.includes('.mov') || |
| 35 | + mediaSrc.value.includes('video/') |
| 36 | + } |
| 37 | + return false |
29 | 38 | }) |
30 | 39 |
|
31 | 40 | // const isGif = computed(() => { |
|
34 | 43 | // }) |
35 | 44 |
|
36 | 45 | const isImage = computed(() => { |
37 | | - if (!currentFile.value) return !isVideo.value |
38 | | - return currentFile.value.type.startsWith('image/') |
| 46 | + // 如果有当前文件,根据文件类型判断 |
| 47 | + if (currentFile.value) { |
| 48 | + return currentFile.value.type.startsWith('image/') |
| 49 | + } |
| 50 | + // 如果没有当前文件但有媒体源,根据URL判断 |
| 51 | + if (mediaSrc.value) { |
| 52 | + // 如果不是视频,且有媒体源,则默认为图片 |
| 53 | + return !isVideo.value |
| 54 | + } |
| 55 | + return false |
39 | 56 | }) |
40 | 57 |
|
41 | 58 | const handleFileChange = async (e: Event) => { |
|
45 | 62 | emit('change', files) |
46 | 63 | if (files) { |
47 | 64 | const file = files[0] |
| 65 | + if (!file) { |
| 66 | + showLoading.value = false |
| 67 | + return |
| 68 | + } |
48 | 69 | currentFile.value = file |
49 | | -
|
50 | 70 | // 创建预览URL |
51 | 71 | mediaSrc.value = URL.createObjectURL(file) |
52 | 72 | emit('update:modelValue', mediaSrc.value) |
|
81 | 101 | } |
82 | 102 | } |
83 | 103 |
|
| 104 | + const videoRef = ref<HTMLVideoElement | null>(null) |
| 105 | + const handleVideoLoad = () => { |
| 106 | + if (videoRef.value) { |
| 107 | + videoRef.value.controls = false |
| 108 | + } |
| 109 | + } |
| 110 | +
|
84 | 111 | onMounted(() => { |
85 | 112 | let previewPrc = props.previewPrc as string |
86 | 113 | if (previewPrc.includes('.mp4')) { |
87 | | - previewPrc = `${previewPrc}?x-oss-process=video/snapshot,t_0000,f_jpg,w_300,h_600` |
| 114 | + previewPrc = `${previewPrc}?x-oss-process=video/snapshot,t_0000,f_jpg` |
88 | 115 | } |
89 | 116 | mediaSrc.value = previewPrc |
90 | 117 | }) |
|
115 | 142 | :src="mediaSrc" |
116 | 143 | class="block object-cover w-full h-full rounded-lg absolute left-0 top-0 z-10" |
117 | 144 | muted |
118 | | - controls |
| 145 | + ref="videoRef" |
| 146 | + @load="handleVideoLoad" |
119 | 147 | /> |
120 | 148 |
|
121 | 149 | <!-- 图片预览 --> |
|
0 commit comments