"use client"; import { useEffect, useState } from "react"; import { Swiper, SwiperSlide } from "swiper/react"; import { Navigation, Pagination, Grid } from "swiper/modules"; import Link from "next/link"; import Image from "next/image"; import PropTypes from "prop-types"; import axios from "axios"; export default function SwiperItems(props) { const [data, setData] = useState(null); const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // 统一字段映射,后台优先,props兜底 const title = data?.title || props.title; const subtitle = data?.subtitle || props.subtitle; const pagetype = data?.t1 || props.pagetype || ""; const category = data?.t2 || props.category || ""; const category_slug = data?.t3 || props.category_slug || ""; const count = data?.t4 || props.count || 8; const columns = data?.t5 || props.columns || 4; const rows = (data?.p1 && !isNaN(Number(data.p1))) ? Number(data.p1) : (props.rows || 1); const button_text = data?.button_text || props.button_text; useEffect(() => { async function fetchComponentData() { try { setLoading(true); const res = await axios.get("/api/get-component-data", { params: { component_name: "SwiperItems" }, }); setData(res.data.data); } catch (err) { setError("获取SwiperItems数据失败"); } finally { setLoading(false); } } fetchComponentData(); }, []); useEffect(() => { async function fetchPosts() { if (!pagetype) return; setLoading(true); try { const params = new URLSearchParams({ pagetype }); if (category) params.append("category", category); if (count !== undefined && count !== null) params.append("count", count); const res = await fetch(`/api/get-listview-data?${params.toString()}`); const json = await res.json(); if (Array.isArray(json.data)) { setPosts(json.data); } else { setPosts([]); } } catch (e) { setPosts([]); } setLoading(false); } fetchPosts(); }, [pagetype, category, count]); if (!data) return null; // 渲染卡片内容 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 (