"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"; const PAGE_SIZE = 8; // 每页8条,4列2行 const PAGE_SLUG = "case"; // 博客slug常量 export default function Blogs({ parentClass = "xl:w-10/12 lg:w-10/12 w-full flex-[0_0_auto] !px-[15px] max-w-full !mx-auto", marginTop = true, }) { const [posts, setPosts] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [loading, setLoading] = useState(false); 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: 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]); // 分页控件 function PaginationComp() { if (totalPages <= 1) return null; return ( ); } // 渲染卡片内容 function renderCard(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) { // 支持YouTube或本地视频 if (post.video_src && (post.video_src.endsWith('.mp4') || post.video_src.startsWith('/files/'))) { return ( ); } // YouTube const vid = post.videoId || (post.video_src && post.video_src.includes('youtube') ? post.video_src.split('embed/')[1] : null); if (vid) { return (