diff --git a/app/page.jsx b/app/page.jsx
index 5de8d45..eec91d9 100644
--- a/app/page.jsx
+++ b/app/page.jsx
@@ -1,5 +1,6 @@
import Hero from "@/components/homes/home-15/Hero";
import SwiperItems from "@/components/homes/home-15/SwiperItems";
+import CategoryItems from "@/components/homes/home-15/CategoryItems";
import React from "react";
import { getSiteSettings } from "@/utils/siteSettings";
@@ -35,7 +36,7 @@ export default function Page() {
>
{" "}
diff --git a/components/homes/home-15/CategoryItems.jsx b/components/homes/home-15/CategoryItems.jsx
index 1b7010a..8bff923 100644
--- a/components/homes/home-15/CategoryItems.jsx
+++ b/components/homes/home-15/CategoryItems.jsx
@@ -1,172 +1,83 @@
-"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";
+import { fetchComponentData, fetchListViewData } from "@/utils/data";
-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) {
+// 渲染卡片内容
+function renderCardImage(post) {
+ // 视频
+ if (post.video_src || post.videoId) {
+ if (post.video_src && (post.video_src.endsWith('.mp4') || post.video_src.startsWith('/files/'))) {
return (
-
- {post.images.map((img, i) => (
-
-
-
- ))}
-
+
);
}
- // 视频
- 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) {
+ 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;
}
- 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 + "...";
- }
-
- // 日期格式化函数,支持自定义是否显示时分秒
- function formatDate(dateStr, options = {}) {
- if (!dateStr) return "";
- const d = new Date(dateStr);
- if (isNaN(d.getTime())) return dateStr;
- const { showTime = false } = options;
- const yyyy = d.getFullYear();
- const mm = String(d.getMonth() + 1).padStart(2, '0');
- const dd = String(d.getDate()).padStart(2, '0');
- let result = `${yyyy}-${mm}-${dd}`;
- if (showTime) {
- const hh = String(d.getHours()).padStart(2, '0');
- const min = String(d.getMinutes()).padStart(2, '0');
- const ss = String(d.getSeconds()).padStart(2, '0');
- result += ` ${hh}:${min}:${ss}`;
- }
- return result;
- }
+ // 从组件数据中提取字段
+ const { title, subtitle, t3: category_slug, t5: columns, button_text } = data;
return (
- {/* 新增模块标题和副标题显示 */}
{(title || subtitle) && (
{title &&
{title}
}
@@ -174,18 +85,15 @@ export default function CategoryItems() {
)}
- {loading ? (
-
加载中...
- ) : posts.length === 0 ? (
+ {posts.length === 0 ? (
暂无数据
) : (
posts.map((post, idx) => (
- {/* 图片/轮播/视频部分 */}
- {renderCardImage(post, idx)}
+ {renderCardImage(post)}
@@ -194,17 +102,13 @@ export default function CategoryItems() {
- {/* 文字内容 */}
- {post.additional_title}
+ {post.additional_title}
-
+
{post.title}
@@ -218,13 +122,12 @@ export default function CategoryItems() {
))
)}
- {/* 底部更多按钮 */}
{category_slug && (