import Link from "next/link";
import Image from "next/image";
import { fetchComponentData, fetchListViewData } from "@/utils/data";
// 渲染卡片内容
function renderCardImage(post) {
// 视频
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 (
);
}
}
// 单图
const img = post.image || (Array.isArray(post.images) && post.images[0]);
if (img) {
return (
);
}
return null;
}
function getSummary(text, maxLen = 58) {
if (!text) return "";
if (/[\u4e00-\u9fa5]/.test(text)) {
return text.length > maxLen ? text.slice(0, maxLen) + "..." : text;
}
if (text.length <= maxLen) return text;
let cut = text.slice(0, maxLen);
if (!/\s/.test(text[maxLen])) {
const lastSpace = cut.lastIndexOf(" ");
if (lastSpace > 0) cut = cut.slice(0, lastSpace);
}
return cut + "...";
}
export default async function CategoryItems() {
// 获取数据
const componentDataResult = await fetchComponentData("CategoryItems");
const data = componentDataResult.data;
let posts = [];
if (data) {
const { t1: pagetype, t2: category, t4: count } = data;
const postsResult = await fetchListViewData({ pagetype, category, count });
posts = postsResult.data || [];
}
if (!data) {
return null;
}
// 从组件数据中提取字段
const { title, subtitle, t3: category_slug, t5: columns, button_text } = data;
return (
{(title || subtitle) && (
{title &&
{title}
}
{subtitle &&
{subtitle}
}
)}
{posts.length === 0 ? (
暂无数据
) : (
posts.map((post, idx) => (
{renderCardImage(post)}
查看详情
{post.additional_title}
{post.title}
{getSummary(post.subtitle)}
))
)}
{category_slug && (
)}
);
}