"use client"; import { useEffect, useState } from "react"; import { Swiper, SwiperSlide } from "swiper/react"; import { Navigation, Pagination } from "swiper/modules"; import Link from "next/link"; import Image from "next/image"; export default function PageItems({ page_slug = "", page_size = 8, columns = 4 }) { const [posts, setPosts] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [loading, setLoading] = useState(false); // 静态映射,避免 Tailwind JIT 无法识别动态类名 const colClass = { 1: "lg:grid-cols-1", 2: "lg:grid-cols-2", 3: "lg:grid-cols-3", 4: "lg:grid-cols-4", 5: "lg:grid-cols-5", 6: "lg:grid-cols-6", }[columns] || "lg:grid-cols-4"; useEffect(() => { async function fetchData() { setLoading(true); try { const res = await fetch("/api/get-page-data", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slug_list: [page_slug], page: currentPage, page_size }), }); const json = await res.json(); if (Array.isArray(json.data)) { setPosts(json.data); setTotalPages(Math.ceil((json.total || 0) / page_size)); } else { setPosts([]); setTotalPages(1); } } catch (e) { setPosts([]); setTotalPages(1); } setLoading(false); } fetchData(); }, [currentPage, page_slug, page_size]); // 分页控件 function PaginationComp() { if (totalPages <= 1) return null; return ( ); } // 渲染卡片内容(只返回图片部分,结构在下方article里) function renderCardImage(post, idx) { // 多图轮播 if (Array.isArray(post.images) && post.images.length > 1) { return ( {post.images.map((img, i) => ( {post.title ))} ); } // 视频 if (post.video_src || post.videoId) { if (post.video_src && (post.video_src.endsWith('.mp4') || post.video_src.startsWith('/files/'))) { return ( ); } const vid = post.videoId || (post.video_src && post.video_src.includes('youtube') ? post.video_src.split('embed/')[1] : null); if (vid) { return (