This commit is contained in:
2024-07-17 16:29:36 +08:00
parent 5c320ebc54
commit 218c6ee931

View File

@ -26,6 +26,9 @@ import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { getToken } from "@/utils/auth";
import {
uploadimg
} from '@/api/system/editor.js'
import Video from "@/utils/quillVideo";
Quill.register(Video, true);
export default {
@ -219,9 +222,19 @@ export default {
return isLt1M && isVideo;
},
//
onSuccessVideo(res) {
async onSuccessVideo(res) {
if (res.code === 200) {
this.insertVideoLink(process.env.VUE_APP_BASE_API + res.imgUrl);
const dataURL = await this.getVideoBase64(process.env.VUE_APP_BASE_API + res.imgUrl);
const imgUrl = await this.getFileFromBase64(dataURL, "kefu.jpeg");
const fd1 = new FormData();
//
fd1.append("file", imgUrl);
let CoverImg = await uploadimg(fd1).then(res => {
if (res.code == 200) {
return res.url
}
})
await this.insertVideoLink(process.env.VUE_APP_BASE_API + res.imgUrl, CoverImg);
} else {
this.$message.error(res.msg);
}
@ -230,8 +243,41 @@ export default {
onErrorVideo() {
this.$message.error("上传失败");
},
// base64
getVideoBase64(url) {
return new Promise(function (resolve) {
let dataURL = "";
const video = document.createElement("video");
video.setAttribute("crossOrigin", "anonymous"); //
video.setAttribute("src", url);
video.setAttribute("preload", "auto");
video.addEventListener("loadeddata", function () {
const canvas = document.createElement("canvas");
console.log("video.clientWidth", video.videoWidth); //
console.log("video.clientHeight", video.videoHeight); //
const width = video.videoWidth || 400; // canvas
const height = video.videoHeight || 240; // 400 240
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(video, 0, 0, width, height); // canvas
dataURL = canvas.toDataURL("image/jpeg"); // base64
resolve(dataURL);
});
});
},
// base64
getFileFromBase64(base64URL, filename) {
var arr = base64URL.split(","),
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: "image/png" });
},
//
insertVideoLink(videoLink) {
insertVideoLink(videoLink, CoverImg) {
if (!videoLink) return this.$message.error("视频地址不能为空!");
this.dialogFormVisible = false;
//
@ -239,7 +285,7 @@ export default {
// range null
let index = range ? range : 0;
//
this.Quill.insertEmbed(index, "video", { url: videoLink });
this.Quill.insertEmbed(index, "video", { url: videoLink, poster: CoverImg });
//
this.Quill.setSelection(index + 1);
},