tiefen_space_h5/components/Photos/index.js

352 lines
12 KiB
JavaScript
Raw Normal View History

2024-06-25 20:18:37 +08:00
"use client";
import React, { useEffect, useState, useRef } from "react";
2024-07-19 14:14:40 +08:00
import { Image, ImageViewer, Dialog } from "antd-mobile";
2024-06-29 12:07:37 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2024-08-05 13:10:33 +08:00
import { faAngleUp, faClose } from "@fortawesome/free-solid-svg-icons";
import { useRouter } from "next/navigation";
2024-07-22 14:38:59 +08:00
import baseRequest from "@/utils/baseRequest";
2024-08-07 19:33:31 +08:00
import { handleShowVideo } from "@/utils/tools/handleFuns";
2024-08-07 18:39:16 +08:00
// import dynamic from 'next/dynamic'
2024-08-14 19:59:03 +08:00
import ImagesMask from "@/components/ImagesMask";
2024-08-07 18:39:16 +08:00
// const ImagesMask = dynamic(() =>
// import('@/components/ImagesMask'), { ssr: false }
// )
2024-08-05 13:10:33 +08:00
export default function Photos({
isUnlocked,
mediaVisibleRange,
mediaAmount,
media,
type,
data,
}) {
2024-06-29 12:07:37 +08:00
const [seeAllPhotos, setSeeAllPhotos] = useState(false);
2024-07-03 19:59:39 +08:00
const [currentPhotos, setCurrentPhotos] = useState([]);
const [photos, setPhotos] = useState([]);
const [visible, setVisible] = useState(false);
2024-08-05 18:04:26 +08:00
const currentIndex = useRef();
const swiper = useRef(null);
2024-08-07 18:08:33 +08:00
const imagesMaskRef = useRef(null);
const router = useRouter();
2024-07-22 14:38:59 +08:00
const base = baseRequest();
2024-08-07 18:08:33 +08:00
2024-06-29 12:07:37 +08:00
useEffect(() => {
2024-07-03 19:59:39 +08:00
if (media) {
2024-07-15 20:00:44 +08:00
let imgArr = media.images.map((item) => ({
type: "img",
url: item.urls?.[0],
2024-08-09 22:10:02 +08:00
// fullwidth:item.w>item.h
2024-08-13 13:41:42 +08:00
w: item.w,
h: item.h,
2024-07-15 20:00:44 +08:00
}));
let videoArr = media.videos.map((item) => ({
type: "video",
url: item.cover_urls?.[0],
mp4: item.urls?.[0],
2024-08-13 13:41:42 +08:00
w: item.cover_w,
h: item.cover_h,
2024-08-09 22:10:02 +08:00
// fullwidth:item.cover_w>item.cover_h
2024-07-15 20:00:44 +08:00
}));
let arr = [...imgArr, ...videoArr];
2024-07-03 19:59:39 +08:00
let newPhotos = [...arr];
2024-07-15 20:00:44 +08:00
if (mediaAmount && !isUnlocked) {
2024-08-05 18:59:30 +08:00
// console.log("newPhotos", newPhotos);
2024-07-15 20:00:44 +08:00
newPhotos = Array(mediaAmount)
.fill(null)
.map((item, index) => {
2024-08-05 13:10:33 +08:00
return newPhotos[index] && mediaVisibleRange
? newPhotos[index]
: {
mp4: newPhotos[0]?.type === "video",
url: newPhotos[0]?.url,
type: "hid",
2024-08-13 13:41:42 +08:00
w: newPhotos[0]?.w,
h: newPhotos[0]?.h,
2024-08-05 13:10:33 +08:00
};
2024-07-15 20:00:44 +08:00
});
}
setPhotos(newPhotos);
if (newPhotos.length > 9) {
2024-07-03 19:59:39 +08:00
newPhotos = newPhotos.slice(0, 9);
}
2024-07-22 16:41:33 +08:00
// console.log("newPhotos", newPhotos);
2024-07-03 19:59:39 +08:00
setCurrentPhotos(newPhotos);
2024-06-29 12:07:37 +08:00
}
2024-07-03 19:59:39 +08:00
}, [media]);
2024-08-13 13:41:42 +08:00
useEffect(() => {
2024-08-13 12:34:50 +08:00
// window.addEventListener('resize', (e)=>{
// // alert(window.innerHeight)
// setMaskHeight(window.innerHeight)
// });
2024-08-13 13:41:42 +08:00
return () => {
2024-08-13 12:34:50 +08:00
// window.removeEventListener("resize")
2024-08-13 13:41:42 +08:00
};
}, []);
2024-06-25 20:18:37 +08:00
const showPhotos = (photos, index) => {
currentIndex.current = index;
2024-08-14 19:59:03 +08:00
// 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);
// 添加左右切换按钮
2024-08-07 18:43:13 +08:00
// const btns = document.createElement("div");
// btns.className = "imagesBtnsControllers";
// const btnL = document.createElement("div");
// btnL.className = "imagesBtnsControllerLeft";
// btnL.addEventListener("click", () => {
// if (currentIndex.current > 0) {
// currentIndex.current = currentIndex.current - 1;
// handleShowImages(currentIndex.current, photos, btns,interval);
// if (!currentIndex.current) {
// btnL.style.opacity = 0.5;
// btnR.style.opacity = 1;
// } else {
// btnR.style.opacity = 1;
// btnL.style.opacity = 1;
// }
// }
// });
// const btnR = document.createElement("div");
// btnR.className = "imagesBtnsControllerRight";
// btnR.addEventListener("click", () => {
// if (currentIndex.current < currentPhotos.length - 1) {
// currentIndex.current = currentIndex.current + 1;
// handleShowImages(currentIndex.current, photos, btns,interval);
// if (currentIndex.current == currentPhotos.length - 1) {
// btnR.style.opacity = 0.5;
// btnL.style.opacity = 1;
// } else {
// btnR.style.opacity = 1;
// btnL.style.opacity = 1;
// }
// }
// });
// btns.append(btnL, btnR);
// handleShowImages(index, photos, btns,interval);
2024-08-14 19:59:03 +08:00
// handleShowImages(index, photos, interval);
// setVisible(true)
2024-08-14 19:59:03 +08:00
console.log(imagesMaskRef.current, photos);
imagesMaskRef.current.show(photos, index, data);
};
2024-08-14 19:59:03 +08:00
// const handleShowImages = (index, photos, interval) => {
// const mediaDom = document.createElement("div");
// mediaDom.style.width = "100vw";
// document.body.appendChild(mediaDom);
// mediaDom.className = `${
// photos[index]?.type == "hid" ? "mediaDom photos-body" : "mediaDom"
// }`;
// // mediaDom.appendChild(btns);
// document.body.style.overflow = "hidden";
// ImageViewer.clear();
// ImageViewer.Multi.show({
// images: photos.map((item) => item?.url),
// defaultIndex: index,
// renderFooter: (image, index) => {
// return renderFooter(photos[index]?.type == "hid", mediaDom);
// },
2024-08-05 18:04:26 +08:00
2024-08-14 19:59:03 +08:00
// getContainer: mediaDom,
// onIndexChange: (index) => {
// currentIndex.current = index;
// mediaDom.className = `${
// photos[index]?.type == "hid" ? "mediaDom photos-body" : "mediaDom"
// }`;
// const leftBtn = document.getElementsByClassName(
// "imagesBtnsControllerLeft"
// )[0];
// const rightBtn = document.getElementsByClassName(
// "imagesBtnsControllerRight"
// )[0];
// if (leftBtn && rightBtn) {
// if (!index) {
// leftBtn.style.opacity = 0.5;
// rightBtn.style.opacity = 1;
// } else if (index == currentPhotos.length - 1) {
// leftBtn.style.opacity = 1;
// rightBtn.style.opacity = 0.5;
// } else {
// leftBtn.style.opacity = 1;
// rightBtn.style.opacity = 1;
// }
// }
// },
// afterClose: () => {
// mediaDom.remove();
// clearInterval(interval);
// document.body.style.overflow = "auto";
// },
// classNames: { body: "photos-bodyBox" },
// });
// };
2024-06-29 12:07:37 +08:00
const handleSeeAllPhotos = (e) => {
e.stopPropagation();
e.preventDefault();
2024-07-22 16:41:33 +08:00
// console.log(e);
2024-06-29 12:07:37 +08:00
setSeeAllPhotos((old) => {
if (old) {
const newPhotos = [...photos];
setCurrentPhotos(newPhotos.slice(0, 9));
return false;
} else {
setCurrentPhotos(photos);
return true;
}
});
2024-06-25 20:18:37 +08:00
};
const renderFooter = (hidden, fatherDom) => {
return hidden ? (
<div className="flex justify-center">
<div
2024-08-13 14:42:58 +08:00
className="rounded-full text-base -mt-[50vh] h-max w-max px-6 py-3 bg-primary"
onClick={() => {
fatherDom && fatherDom.remove();
2024-07-22 14:38:59 +08:00
router.push(
"/webView/" +
encodeURIComponent(
"/zone/pay/" +
data?.zid +
"/h5_zone_moment/" +
data?.id +
"?base=" +
2024-07-24 17:12:11 +08:00
encodeURIComponent(JSON.stringify(base))
2024-07-22 14:38:59 +08:00
)
);
}}
2024-08-13 13:41:42 +08:00
style={{ marginTop: "calc(-85px )" }}
>
此内容暂未解锁立即解锁
</div>
</div>
) : null;
};
2024-06-25 20:18:37 +08:00
return (
<>
2024-07-15 20:00:44 +08:00
<div
2024-08-09 19:39:17 +08:00
className={`${
2024-08-13 13:41:42 +08:00
currentPhotos.length > 1
? "grid grid-cols-3 gap-1.5 min-h-[24vw]"
: "auto"
2024-07-15 20:00:44 +08:00
}`}
>
2024-06-29 12:07:37 +08:00
{currentPhotos.map((item, index) => {
2024-06-25 20:18:37 +08:00
return (
<div
2024-08-13 14:42:58 +08:00
className={`relative w-max`}
2024-08-09 22:10:02 +08:00
// style={{ width: }}
2024-06-25 20:18:37 +08:00
key={index}
onClick={() => {
2024-07-15 20:00:44 +08:00
if (item?.type == "video") {
2024-07-22 14:38:59 +08:00
if (!isUnlocked && type == "space") {
const mediaDom = document.createElement("div");
document.body.appendChild(mediaDom);
ImageViewer.show({
image: item.url,
maxZoom: 8,
classNames: { body: "customize-body" },
2024-07-22 14:38:59 +08:00
getContainer: mediaDom,
renderFooter: () => renderFooter(true, mediaDom),
});
return;
}
2024-07-03 19:59:39 +08:00
handleShowVideo(item);
2024-06-29 12:07:37 +08:00
} else {
showPhotos(photos, index);
}
2024-06-25 20:18:37 +08:00
}}
>
2024-07-15 20:00:44 +08:00
{
2024-08-05 13:10:33 +08:00
<div
2024-08-09 22:10:02 +08:00
className={`overflow-hidden rounded h-full ${
currentPhotos.length > 1 ? "h-[24vw]" : ""
2024-08-05 13:10:33 +08:00
}`}
>
2024-07-15 20:00:44 +08:00
<Image
// lazy={true}
placeholder={
2024-08-09 20:13:46 +08:00
<div className="w-full min-h-[24vw] bg-[#1d1d1d] rounded"></div>
2024-07-15 20:00:44 +08:00
}
2024-08-13 13:41:42 +08:00
width={
currentPhotos.length > 1
? "24vw"
: item.w > item.h
? `calc(100vw - 72px - 2rem)`
: `calc(46vw * ${item.w / item.h})`
}
height={
currentPhotos.length > 1
? "24vw"
: item.w > item.h
? `calc((100vw - 72px - 2rem) * ${item.h / item.w})`
: "46vw"
}
2024-07-15 20:00:44 +08:00
className={`rounded max-w-full ${
2024-08-05 13:10:33 +08:00
item?.type == "hid" && type == "space" ? "imageBlur" : ""
2024-07-15 20:00:44 +08:00
}`}
2024-08-13 13:41:42 +08:00
fit={currentPhotos.length > 1 ? "cover" : "contain"}
2024-07-15 20:00:44 +08:00
src={item?.url}
/>
</div>
}
2024-08-02 22:12:54 +08:00
{item?.mp4 && (
2024-08-07 21:28:17 +08:00
<div className="absolute top-0 w-full h-full rounded flex justify-center items-center">
2024-06-29 12:07:37 +08:00
<Image
className=""
2024-07-06 11:05:19 +08:00
width={98}
height={98}
2024-08-05 13:10:33 +08:00
src={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL + "/icons/play.png"
}
2024-08-07 21:28:17 +08:00
placeholder={
<div className="w-full h-full min-h-96 bg-[#1d1d1d] rounded"></div>
}
2024-06-29 12:07:37 +08:00
/>
</div>
)}
{/* {index==8 && currentPhotos.length>9 && (
<div className="absolute top-0 w-full h-full flex justify-center items-center bg-[#33333348]">
<span className="text-2xl">+{currentPhotos.length}</span>
</div>
)} */}
{index == currentPhotos.length - 1 &&
photos.length > 9 &&
2024-08-05 13:10:33 +08:00
(!seeAllPhotos ? (
2024-06-29 12:07:37 +08:00
<div
2024-08-13 12:34:50 +08:00
className="absolute top-0 w-full h-full flex justify-center items-center bg-[#33333348]"
2024-06-29 12:07:37 +08:00
onClick={handleSeeAllPhotos}
>
<span className="text-2xl">+{currentPhotos.length}</span>
</div>
2024-08-05 13:10:33 +08:00
) : (
2024-07-25 19:52:30 +08:00
<div
2024-08-07 21:28:17 +08:00
className="absolute top-0 h-full flex justify-center items-center bg-[#33333348]"
2024-07-25 19:52:30 +08:00
key="closeBtn"
onClick={handleSeeAllPhotos}
>
2024-08-05 13:10:33 +08:00
<FontAwesomeIcon
icon={faAngleUp}
size="xl"
className="h-14"
/>
2024-07-25 19:52:30 +08:00
</div>
))}
2024-06-25 20:18:37 +08:00
</div>
);
})}
</div>
2024-08-14 19:59:03 +08:00
<ImagesMask ref={imagesMaskRef} />
{/* <Viewer
visible={visible}
onClose={() => { setVisible(false); } }
images={currentPhotos.map((it,index)=>({src:it.url,alt:index}))}
/> */}
2024-06-25 20:18:37 +08:00
</>
);
}