"use client"; import React, { useState, useEffect, useRef, forwardRef, useImperativeHandle, } from "react"; import { Mask, Image } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleLeft, faAngleRight, faDownload, } from "@fortawesome/free-solid-svg-icons"; import { createRoot } from "react-dom/client"; import { useRouter } from "next/navigation"; import baseRequest from "@/utils/baseRequest"; const root = createRoot(document?.getElementById("maskDomBox")); function ImagesMask({}, ref) { const [visible, setVisible] = useState(false); const [images, setImages] = useState([]); const [data, setData] = useState(null); const scrollRef = useRef(null); const defaultIndex = useRef(null); const router = useRouter(); useEffect(() => { root.render(
); // root.render(
xxxx
); }, [visible]); useImperativeHandle(ref, () => ({ show: (arr, index, data) => { defaultIndex.current = index; setImages(arr); setData(data); setVisible((old) => { // console.log(arr, index, old); return !old; }); // Mask.show // const maskDomBox = document.getElementById("maskDomBox"); }, close: () => setVisible(false), })); return <>; } const ImagesMaskContaint = forwardRef( ( { visible, images, setVisible, setImages, router, data, defaultIndex = 0 }, ref ) => { const [currentIndex, setCurrentIndex] = useState(0); const [direction, setDirection] = useState("left"); const [initialX, setInitialX] = useState(0); const [xOffset, setXOffset] = useState(0); const [active, setActive] = useState(false); const base = baseRequest(); const directionX = useRef(0); // const getDistance = (e) => { // e.stopPropagation(); // const distance = ref.current.scrollLeft; // setDistance(distance); // }; useEffect(() => { // setCurrentIndex(defaultIndex); // test3(defaultIndex - 1, "left"); if (defaultIndex) { test3(defaultIndex - 1, "left"); } }, [defaultIndex]); useEffect(() => { if (ref.current) { ref.current.style.transform = `translateX(${ -window.innerWidth * currentIndex }px)`; } }, [currentIndex]); const test1 = (e) => { setInitialX(e.touches[0].clientX - (!currentIndex ? 0 : xOffset)); if (e.touches.length === 1) { // 单点触摸 setActive(true); } }; const test2 = (e) => { if (active) { let X = e.touches[0].clientX - initialX; setDirection( directionX.current > e.touches[0].clientX ? "left" : "right" ); ref.current.style.transform = "translateX(" + X + "px)"; directionX.current = e.touches[0].clientX; } }; const test3 = (index, currentDirection) => { setActive(false); let cxOffset = window.innerWidth; if (currentDirection == "left") { if (index < images.length - 1) { setCurrentIndex(index + 1); } else { ref.current.style.transform = `translateX(${-cxOffset * index}px)`; } } else { if (index > 0) { setCurrentIndex(index - 1); } else { ref.current.style.transform = `translateX(${-cxOffset * index}px)`; } } // setInitialX(currentX); setInitialX(-cxOffset * (index + (currentDirection == "left" ? 1 : -1))); if (currentDirection == "left" && index > images.length - 2) return; setXOffset(-cxOffset * (index + (currentDirection == "left" ? 1 : -1))); }; function downloadImage(url) { let randomNum = ""; for (let i = 0; i < 10; i++) { randomNum += Math.floor(Math.random() * 10); } fetch(url) .then((res) => res.blob()) .then((blob) => { var a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = randomNum + ".jpg"; a.click(); }); } return (
{ setVisible(false), setImages([]), setCurrentIndex(null); // root.unmount(); }} color="#000000d9" >
{images[currentIndex]?.type != "hid" && (
downloadImage(images[currentIndex]?.url)} className="z-50 flex justify-center items-center w-[38px] h-[38px] bg-[#ffffff1a] text-[#fff] rounded-full absolute top-[12px] left-[12px]" >
)}
{currentIndex + 1}/{images.length}
{ setVisible(false), setImages([]), setCurrentIndex(null); // root.unmount(); }} >
test3(currentIndex, direction)} > {images.map((item, index) => { return (
{/* */}
} />
{item.type == "hid" && (
{ router.push( "/webView/" + encodeURIComponent( "/zone/pay/" + data?.zid + "/h5_zone_moment/" + data?.id + "?base=" + encodeURIComponent(JSON.stringify(base)) ) ); }} > 此内容暂未解锁,立即解锁
)}
); })}
1 ? "visible" : "hidden", }} >
{ test3(currentIndex, "right"); }} >
{ test3(currentIndex, "left"); }} >
); } ); export default forwardRef(ImagesMask);