'use client'; import { useState, useEffect, useRef } from 'react'; import { Swiper, SwiperSlide } from "swiper/react"; import { Navigation, Pagination, Thumbs, FreeMode } from "swiper/modules"; // 导入Swiper样式 import "swiper/css"; import "swiper/css/navigation"; import "swiper/css/pagination"; import "swiper/css/thumbs"; import "swiper/css/free-mode"; const ProductImageGallery = ({ data }) => { const [currentImageIndex, setCurrentImageIndex] = useState(0); const [thumbsSwiper, setThumbsSwiper] = useState(null); const thumbsSwiperRef = useRef(null); // 如果没有attachments,不显示组件 if (!data?.attachments || data.attachments.length === 0) { return null; } // 找到主图在attachments中的索引 const getMainImageIndex = () => { if (!data.image) return 0; const mainImageIndex = data.attachments.findIndex( attachment => attachment.file_url === data.image ); return mainImageIndex >= 0 ? mainImageIndex : 0; }; const mainImageIndex = getMainImageIndex(); const currentImage = data.attachments[currentImageIndex]?.file_url || data.attachments[0]?.file_url; // 专门的缩略图滚动函数 const scrollThumbnailToIndex = (index) => { console.log('滚动缩略图到索引:', index); // 调试信息 if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { const swiper = thumbsSwiperRef.current.swiper; console.log('Swiper实例存在,开始滚动'); // 调试信息 // 使用更简单的方法:直接滚动到指定索引 try { swiper.slideTo(index, 300); console.log('滚动成功,目标索引:', index); } catch (error) { console.error('滚动失败:', error); } } else { console.log('Swiper实例不存在,thumbsSwiperRef:', thumbsSwiperRef.current); } }; const changeMainImage = (direction) => { console.log('changeMainImage 被调用,方向:', direction); // 调试信息 if (data.attachments.length <= 1) return; let newIndex; if (direction === 'next') { newIndex = (currentImageIndex + 1) % data.attachments.length; } else { newIndex = (currentImageIndex - 1 + data.attachments.length) % data.attachments.length; } console.log('新的索引:', newIndex, '当前索引:', currentImageIndex); // 调试信息 setCurrentImageIndex(newIndex); // 自动滚动缩略图到对应位置 setTimeout(() => { console.log('开始执行滚动,索引:', newIndex); // 调试信息 scrollThumbnailToIndex(newIndex); }, 100); }; const changeMainImageByIndex = (index) => { setCurrentImageIndex(index); // 智能滚动缩略图到可见区域 setTimeout(() => { scrollThumbnailToIndex(index); }, 100); }; // 初始化当前索引为主图索引 useEffect(() => { setCurrentImageIndex(mainImageIndex); }, [mainImageIndex]); // 键盘导航支持 useEffect(() => { const handleKeyDown = (event) => { if (data.attachments.length <= 1) return; switch (event.key) { case 'ArrowLeft': event.preventDefault(); changeMainImage('prev'); break; case 'ArrowRight': event.preventDefault(); changeMainImage('next'); break; case 'Home': event.preventDefault(); setCurrentImageIndex(0); setTimeout(() => scrollThumbnailToIndex(0), 100); break; case 'End': event.preventDefault(); setCurrentImageIndex(data.attachments.length - 1); setTimeout(() => scrollThumbnailToIndex(data.attachments.length - 1), 100); break; default: break; } }; // 添加键盘事件监听器 document.addEventListener('keydown', handleKeyDown); return () => { document.removeEventListener('keydown', handleKeyDown); }; }, [data.attachments.length]); // 缩略图导航控制 - 移除这些函数,不再需要 // const goToPrevThumbs = () => { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // thumbsSwiperRef.current.swiper.slidePrev(); // } // }; // const goToNextThumbs = () => { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // thumbsSwiperRef.current.swiper.slideNext(); // } // } // }; return (