视频圈发布时不传封面默认一张图片

This commit is contained in:
曹磊 2025-03-31 09:20:37 +08:00
parent 8694b6101f
commit c27913c586
1 changed files with 106 additions and 40 deletions

View File

@ -58,6 +58,22 @@
<view class="tabbar" @click="submit()">发布
</view>
</view>
<!-- <view>
<video
v-if="videoSrc"
:src="videoSrc"
controls
style="width: 0; height: 0"
id="myVideo"
></video>
<canvas
:style="{ border: '1px solid red' }"
canvas-id="myCanvas"
id="myCanvas"
:width="canvasWidth"
:height="canvasHeight"
></canvas>
</view> -->
</view>
</template>
@ -75,7 +91,10 @@
//
headImg: [],
videos: [],
videosList: ''
videosList: '',
canvasWidth: 295,
canvasHeight: 355,
videoSrc: '',
}
},
onLoad(e) {},
@ -103,14 +122,15 @@
})
return
}
// if (!this.form.headImg) {
if (!this.form.headImg) {
// uni.showToast({
// title: '',
// icon: 'none',
// duration: 1000
// })
// return;
// }
this.form.headImg = "https://wx.sjajk.com/file/uploadPath/logo2.png";
}
let data = {
createBy: uni.getStorageSync("userId"),
filePath: this.videosList,
@ -136,7 +156,7 @@
}
});
},
addvideo(e) {
async addvideo(e) {
let that = this
uni.chooseVideo({
count: 1,
@ -147,7 +167,7 @@
that.$queue.showLoading("上传中...");
uni.getFileInfo({
filePath: that.videos[i],
success: (infoRes) => {
success: async (infoRes) => {
const size = infoRes.size;
if (size / 1024 / 1024 > 10) {
// 2MB
@ -156,8 +176,9 @@
title: '视频超出10MB限制,请重新选择'
})
} else {
//
this.onLoadedMetadata(res.tempFilePath);
this.videoSrc = res.tempFilePath; //2025-03-28
//ctx.drawImagectx.draw
// await this.onLoadedMetadata(); //2025-03-28
// 2MB
uni.uploadFile({ //
@ -184,25 +205,23 @@
})
},
onLoadedMetadata(tempFilePath) {
async onLoadedMetadata() {
let that = this
const canvas = document.createElement('canvas');
// Canvas
canvas.width = 295;
canvas.height = 355;
console.log(canvas);
const context = canvas.getContext('2d');
const video = document.createElement('video');
video.src = tempFilePath;
//
video.play().then(() => {
const video = uni.createVideoContext('myVideo', that); //2025-03-28
video.play()
setTimeout(function(){
video.pause();
// canvas
context.drawImage(video, 0, 0, canvas.width, canvas.height);
// canvasbase64
const base64Image = canvas.toDataURL('image/png');
// base64Image
that.checkImageSize(base64Image, (isValid) => {
}, 100)
const ctx = uni.createCanvasContext('myCanvas', that) //2025-03-28
ctx.drawImage(video, 0, 0, 295, 355);
ctx.draw(true, () => {
console.log('✅ 标准回调执行');
//
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
quality: 0.8,
success: res => {
that.checkImageSize(res.tempFilePath, (isValid) => {
if (isValid) {
//
console.log('图片校验通过,可以上传');
@ -210,8 +229,55 @@
console.log('图片超出大小限制,请重新选择');
}
});
}).catch(error => {
console.error('无法播放视频:', error);
resolve()
},
fail: err => {
console.error('截图失败:', err)
reject(err)
}
}, this)
});
// if (!this.checkCanvasReady()) return;
// //
// ctx.beginPath();
// ctx.arc(150, 150, 50, 0, Math.PI * 2);
// ctx.fillStyle = '#00ff00';
// ctx.fill();
// // H5
// await this.h5Draw(ctx);
},
checkCanvasReady() {
// DOM
const canvas = document.getElementById('myCanvas');
if (!canvas) {
console.error('Canvas 元素未找到');
return false;
}
return true;
},
async h5Draw(ctx) {
return new Promise((resolve) => {
let callbackExecuted = false;
//
ctx.draw(true, () => {
callbackExecuted = true;
console.log('✅ 标准回调执行');
resolve();
});
//
setTimeout(() => {
if (!callbackExecuted) {
console.log('⏱️ 强制通过定时器完成');
resolve();
}
}, 100);
});
},