feat: implement frontend-backend configuration linkage
- Create public config API for frontend consumption - Add configuration fetching to homepage - Implement module show/hide logic based on config - Add support for Services items filtering - Add support for Products featured products and pricing display - Add support for News display count, categories, and sort order - Fix table name from 'configs' to 'siteConfig' in API route - Update type definitions for proper TypeScript support
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense, useEffect } from 'react';
|
||||
import { Suspense, useEffect, useState } from 'react';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import dynamic from 'next/dynamic';
|
||||
import { HeroSection } from "@/components/sections/hero-section";
|
||||
@@ -46,9 +46,35 @@ const NewsSection = dynamic(
|
||||
}
|
||||
);
|
||||
|
||||
interface SiteConfig {
|
||||
feature_services?: { enabled: boolean; items: string[] };
|
||||
feature_products?: { enabled: boolean; showPricing: boolean; featuredProducts: string[] };
|
||||
feature_news?: { enabled: boolean; displayCount: number; categories: string[]; sortOrder: 'asc' | 'desc' };
|
||||
}
|
||||
|
||||
function HomeContent() {
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const [config, setConfig] = useState<SiteConfig>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchConfig = async () => {
|
||||
try {
|
||||
const res = await fetch('/api/config');
|
||||
const data = await res.json();
|
||||
if (data.success) {
|
||||
setConfig(data.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取配置失败:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchConfig();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const section = searchParams.get('section');
|
||||
if (section) {
|
||||
@@ -63,15 +89,23 @@ function HomeContent() {
|
||||
}
|
||||
return undefined;
|
||||
}, [searchParams]);
|
||||
|
||||
|
||||
if (loading) {
|
||||
return <SectionSkeleton />;
|
||||
}
|
||||
|
||||
const showServices = config.feature_services?.enabled !== false;
|
||||
const showProducts = config.feature_products?.enabled !== false;
|
||||
const showNews = config.feature_news?.enabled !== false;
|
||||
|
||||
return (
|
||||
<main className="min-h-screen bg-white dark:bg-(--color-bg-primary)">
|
||||
<HeroSection />
|
||||
<ServicesSection />
|
||||
<ProductsSection />
|
||||
{showServices && <ServicesSection config={config.feature_services} />}
|
||||
{showProducts && <ProductsSection config={config.feature_products} />}
|
||||
<CasesSection />
|
||||
<AboutSection />
|
||||
<NewsSection />
|
||||
{showNews && <NewsSection config={config.feature_news} />}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user