查看详情
{post.title}
{getSummary(post.subtitle)}
"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";
import axios from "axios";
export default function CategoryItems() {
const [data, setData] = useState(null);
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
// 统一字段映射,便于团队查阅和维护
const title = data?.title;
const subtitle = data?.subtitle;
const pagetype = data?.t1;
const category = data?.t2;
const category_slug = data?.t3;
const count = data?.t4;
const columns = data?.t5;
const button_text = data?.button_text;
useEffect(() => {
async function fetchComponentData() {
try {
setLoading(true);
const res = await axios.get("/api/get-component-data", {
params: { component_name: "CategoryItems" },
});
setData(res.data.data);
} catch (err) {
setError("获取CategoryItems数据失败");
} finally {
setLoading(false);
}
}
fetchComponentData();
}, []);
useEffect(() => {
async function fetchPosts() {
if (!data) return;
setLoading(true);
try {
// 使用统一映射变量
const params = new URLSearchParams({ pagetype: 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();
}, [data, pagetype, category, count]);
if (!data) return null;
// 渲染卡片内容
function renderCardImage(post, idx) {
// 多图轮播
if (Array.isArray(post.images) && post.images.length > 1) {
return (
{subtitle}
}{getSummary(post.subtitle)}