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({ 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 assets = [ ...fileList, { type: "img", src: URL.createObjectURL(file) }, ]; setFileList(assets); } 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 assets = [ ...fileList, { type: "video", src: URL.createObjectURL(file) }, ]; setFileList(assets); } 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 ( // { // Toast.show(`最多选择 ${maxCount} 张图片,你多选了 ${exceed} 张`); // }} // > //
+
//
{fileList.map((item, index) => { return (
showPhotos(fileList, index)} />
handleRemoveItem(index)} >
); })} {loading && (

上传中

)}
); } // export async function mockUpload(file) { // await sleep(3000) // return { // url: URL.createObjectURL(file), // } // }