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) { if (typeof window == "undefined") return; 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 ( // { // 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), // } // }