import React, { useState } from "react";
import { DotLoading, Image, ImageViewer } from "antd-mobile";
import { uploadImage, uploadVideo } from "@/utils/upload";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAdd, faClose } from "@fortawesome/free-solid-svg-icons";
export default function UploadImgs({assets, getImgs }) {
const maxCount = 6;
const [fileList, setFileList] = useState([]);
const [loading, setLoading] = useState(false);
const handleUploadImage = async (e) => {
let file = e.target.files[0];
if (!file) return;
setLoading(true);
if (file.type.indexOf("image/") != -1) {
// const image = await uploadImage(file);
// getImgs((old) => [...old, image.id]);
const newFiles = [
...assets,
file,
];
const newAssets = newFiles.map(item=>({ type: "img", src: URL.createObjectURL(item) }));
setFileList(newAssets);
getImgs(newFiles);
} else if (file.type.indexOf("video/") != -1) {
const videoD = document.getElementById("videoD");
const videoC = document.getElementById("videoC");
// console.log("videoC", videoC);
const ctx = videoC?.getContext("2d");
// 设置Canvas的宽度和高度与视频一致
videoC.width = videoD.videoWidth;
videoC.height = videoD.videoHeight;
// 在Canvas上绘制当前视频帧
ctx.drawImage(videoD, 0, 0, videoC.width, videoC.height);
// 将Canvas转换为图片URL
const frameImageUrl = videoC.toDataURL();
// 输出图片URL
// console.log(frameImageUrl);
// console.log("ddddd", file, {
// type: "video",
// src: frameImageUrl,
// });
// const video = await uploadVideo(file);
// getImgs((old) => [...old, video.id]);
// setFileList((old) => [...old, video]);
const newFiles = [
...assets,
file,
];
const newAssets = newFiles.map(item=>({ type: "video", src: URL.createObjectURL(item) }));
setFileList(newAssets);
}
setLoading(false);
};
const handleRemoveItem = (index) => {
// console.log(index);
let newArr = [...fileList];
newArr.splice(index, 1);
setFileList(newArr);
};
const showPhotos = (images, index) => {
ImageViewer.Multi.show({
images: images.map(
(item) => "https://file.wishpaldev.tech/" + item?.src_id
),
defaultIndex: index,
});
};
return (
//
上传中