|
@@ -1,5 +1,5 @@
|
|
|
<template>
|
|
|
- <el-button type="primary" :loading="loading" v-if="!src">
|
|
|
+ <el-button type="primary" :loading="loading" v-if="!props.src">
|
|
|
<label class="file" for="file"> 上传素材 </label>
|
|
|
<input
|
|
|
style="display: none"
|
|
@@ -27,7 +27,7 @@
|
|
|
p-id="4267"
|
|
|
/>
|
|
|
</svg>
|
|
|
- <el-image style="width: 120px; height: 120px" :src="src" fit="contain" />
|
|
|
+ <el-image style="width: 120px; height: 120px" :src="srcText" fit="contain" />
|
|
|
<svg
|
|
|
t="1732603337032"
|
|
|
v-if="fileType === 'video'"
|
|
@@ -63,35 +63,29 @@ import { defineProps, ref } from 'vue'
|
|
|
const emit = defineEmits(['change', 'getCalc'])
|
|
|
const props = defineProps<{ src: string }>()
|
|
|
const loading = ref(false)
|
|
|
-const src = ref(props.src)
|
|
|
+const srcText = ref('')
|
|
|
const fileType = ref('')
|
|
|
+let oriUrl = props.src ? props.src : ''
|
|
|
|
|
|
const isImg = (url: string) => {
|
|
|
return /\.(jpg|jpeg|png|gif)$/.test(url)
|
|
|
}
|
|
|
|
|
|
-// 判定如果是图片则直接输出图片,如果是视频则输出第一帧图片
|
|
|
-const getImage = (file: File | string) => {
|
|
|
- const video = document.createElement('video')
|
|
|
- fileType.value = 'video'
|
|
|
- if (typeof file === 'string') {
|
|
|
- video.setAttribute('crossorigin', 'anonymous')
|
|
|
- video.src = file
|
|
|
- } else {
|
|
|
- if (file.type.indexOf('image') !== -1) {
|
|
|
- src.value = URL.createObjectURL(file)
|
|
|
- fileType.value = 'image'
|
|
|
- const img = new Image()
|
|
|
- img.onload = function () {
|
|
|
- emit('getCalc', img.width, img.height)
|
|
|
- }
|
|
|
- img.src = URL.createObjectURL(file)
|
|
|
- return
|
|
|
- }
|
|
|
- // 视频 获取第一帧
|
|
|
- // 播放本地视频文件
|
|
|
- video.src = URL.createObjectURL(file)
|
|
|
+const imgInfo = (file: string) => {
|
|
|
+ fileType.value = 'image'
|
|
|
+ const img = new Image()
|
|
|
+ img.src = file
|
|
|
+ img.onload = () => {
|
|
|
+ emit('getCalc', img.width, img.height)
|
|
|
+ srcText.value = img.src
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+const videoInfo = (file: string) => {
|
|
|
+ fileType.value = 'video'
|
|
|
+ const video = document.createElement('video')
|
|
|
+ video.setAttribute('crossorigin', 'anonymous')
|
|
|
+ video.src = file
|
|
|
video.onloadeddata = function () {
|
|
|
// 跳转到第二秒
|
|
|
video.currentTime = 1
|
|
@@ -104,27 +98,41 @@ const getImage = (file: File | string) => {
|
|
|
canvas.height = video.videoHeight
|
|
|
const ctx = canvas.getContext('2d')
|
|
|
ctx?.drawImage(video, 0, 0, video.videoWidth, video.videoHeight)
|
|
|
- src.value = canvas.toDataURL()
|
|
|
+ srcText.value = canvas.toDataURL()
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
-if (src.value) {
|
|
|
- const isImgUrl = isImg(src.value)
|
|
|
- fileType.value = isImgUrl ? 'image' : 'video'
|
|
|
- getImage(src.value)
|
|
|
-}
|
|
|
-
|
|
|
-let oriUrl = props.src ? props.src : ''
|
|
|
-let times = 0
|
|
|
-
|
|
|
const close = () => {
|
|
|
- src.value = ''
|
|
|
+ srcText.value = ''
|
|
|
fileType.value = ''
|
|
|
oriUrl = ''
|
|
|
emit('getCalc', 0, 0)
|
|
|
emit('change', '')
|
|
|
}
|
|
|
|
|
|
+const getImage = (file: File | string) => {
|
|
|
+ const FType = typeof file
|
|
|
+ if (FType === 'string') {
|
|
|
+ const isImage: boolean = isImg(file as string)
|
|
|
+ // 判定如果是图片则直接输出图片,如果是视频则输出第一帧图片
|
|
|
+ if (isImage) imgInfo(file as string)
|
|
|
+ else videoInfo(file as string)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ const readerFile = file as File
|
|
|
+ if (readerFile.type.indexOf('image') !== -1) {
|
|
|
+ imgInfo(URL.createObjectURL(readerFile))
|
|
|
+ } else {
|
|
|
+ videoInfo(URL.createObjectURL(readerFile))
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const playVideo = () => {
|
|
|
+ window.open(oriUrl)
|
|
|
+}
|
|
|
+
|
|
|
+let times = 0
|
|
|
// 传参为文件
|
|
|
const chagne = (e: Event) => {
|
|
|
if (!e.target) return
|
|
@@ -149,13 +157,13 @@ const chagne = (e: Event) => {
|
|
|
loading.value = false
|
|
|
})
|
|
|
.catch(() => {
|
|
|
+ let t: undefined | number
|
|
|
if (times++ < 5) {
|
|
|
ElMessage({
|
|
|
message: '正在重新上传',
|
|
|
type: 'info',
|
|
|
})
|
|
|
const t = setTimeout(function () {
|
|
|
- console.log(times)
|
|
|
clearTimeout(t)
|
|
|
chagne(e)
|
|
|
}, 1000)
|
|
@@ -165,15 +173,14 @@ const chagne = (e: Event) => {
|
|
|
type: 'info',
|
|
|
})
|
|
|
inputElement.value = ''
|
|
|
+ if (t) clearTimeout(t)
|
|
|
loading.value = false
|
|
|
times = 0
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-const playVideo = () => {
|
|
|
- window.open(oriUrl)
|
|
|
-}
|
|
|
+if (props.src) getImage(props.src)
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|