"use client"; import React, { useEffect, useState,useRef } from "react"; import { Image, ImageViewer, Dialog } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleUp, faClose } from "@fortawesome/free-solid-svg-icons"; import { useRouter } from "next/navigation"; import baseRequest from "@/utils/baseRequest"; import Player from "next-video/player"; export default function Photos({ isUnlocked, mediaVisibleRange, mediaAmount, media, type, data, }) { const [seeAllPhotos, setSeeAllPhotos] = useState(false); const [currentPhotos, setCurrentPhotos] = useState([]); const [photos, setPhotos] = useState([]); const currentIndex = useRef(); const router = useRouter(); const base = baseRequest(); useEffect(() => { if (media) { let imgArr = media.images.map((item) => ({ type: "img", url: item.urls?.[0], })); let videoArr = media.videos.map((item) => ({ type: "video", url: item.cover_urls?.[0], mp4: item.urls?.[0], })); let arr = [...imgArr, ...videoArr]; let newPhotos = [...arr]; if (mediaAmount && !isUnlocked) { console.log("newPhotos", newPhotos); newPhotos = Array(mediaAmount) .fill(null) .map((item, index) => { return newPhotos[index] && mediaVisibleRange ? newPhotos[index] : { mp4: newPhotos[0]?.type === "video", url: newPhotos[0]?.url, type: "hid", }; }); } setPhotos(newPhotos); if (newPhotos.length > 9) { newPhotos = newPhotos.slice(0, 9); } // console.log("newPhotos", newPhotos); setCurrentPhotos(newPhotos); } }, [media]); const showPhotos = (photos, index) => { currentIndex.current=index; const mediaDom = document.createElement("div"); mediaDom.style.width = "100vw"; document.body.appendChild(mediaDom); mediaDom.className = `${ photos[index]?.type == "hid" ? "mediaDom photos-body" : "mediaDom" }`; const interval = setInterval(() => { const bodyBox = document.getElementsByClassName("photos-bodyBox")[0]; if(bodyBox){ bodyBox.firstChild.lastChild.style.transform = `translateX(-${ (currentIndex.current) * (window.innerWidth + 16) }px)`; // console.log(bodyBox.firstChild.lastChild.style.transform); } }, 400); ImageViewer.Multi.show({ images: photos.map((item) => item?.url), defaultIndex: index, renderFooter: (image, index) => { return renderFooter(photos[index]?.type == "hid", mediaDom); }, getContainer: mediaDom, onIndexChange: (index) => { currentIndex.current=index; mediaDom.className = `${ photos[index]?.type == "hid" ? "mediaDom photos-body" : "mediaDom" }`; }, afterClose: () => { mediaDom.remove(); clearInterval(interval); }, classNames: { body: "photos-bodyBox" }, }); }; const handleShowVideo = (video) => { Dialog.className = "videoMask"; Dialog.show({ title: "", content: (
Dialog.clear()} >
{/* */} {/*
{/*
saveFile(video.mp4)} >
*/}
), bodyStyle: { background: "none", maxHeight: "none", height: "100%", }, }); }; const handleSeeAllPhotos = (e) => { e.stopPropagation(); e.preventDefault(); // console.log(e); setSeeAllPhotos((old) => { if (old) { const newPhotos = [...photos]; setCurrentPhotos(newPhotos.slice(0, 9)); return false; } else { setCurrentPhotos(photos); return true; } }); }; const renderFooter = (hidden, fatherDom) => { return hidden ? (
{ fatherDom && fatherDom.remove(); router.push( "/webView/" + encodeURIComponent( "/zone/pay/" + data?.zid + "/h5_zone_moment/" + data?.id + "?base=" + encodeURIComponent(JSON.stringify(base)) ) ); }} > 此内容暂未解锁,立即解锁
) : null; }; return ( <>
1 ? "grid-cols-3 gap-1.5" : "grid-cols-1" }`} > {currentPhotos.map((item, index) => { return (
{ if (item?.type == "video") { if (!isUnlocked && type == "space") { const mediaDom = document.createElement("div"); document.body.appendChild(mediaDom); ImageViewer.show({ image: item.url, maxZoom: 8, classNames: { body: "customize-body" }, getContainer: mediaDom, renderFooter: () => renderFooter(true, mediaDom), }); return; } handleShowVideo(item); } else { showPhotos(photos, index); } }} > {
1 ? "min-h-[24vw]" : "min-h-[38vw]" }`} >
} width={currentPhotos.length > 1 ? "24vw" : "100%"} height={currentPhotos.length > 1 ? "24vw" : "auto"} className={`rounded max-w-full ${ item?.type == "hid" && type == "space" ? "imageBlur" : "" }`} fit="cover" src={item?.url} />
} {item?.mp4 && (
)} {/* {index==8 && currentPhotos.length>9 && (
+{currentPhotos.length}
)} */} {index == currentPhotos.length - 1 && photos.length > 9 && (!seeAllPhotos ? (
+{currentPhotos.length}
) : (
))}
); })} ); }