-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
194 lines (168 loc) · 6.33 KB
/
Copy pathindex.php
File metadata and controls
194 lines (168 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* ikaiCMS - 首页
*
* PHP 8.0+
*/
declare(strict_types=1);
require_once __DIR__ . '/includes/init.php';
// 页面信息
$isHomePage = true;
$pageTitle = '';
$pageKeywords = config('site_keywords');
$pageDescription = config('site_description');
// 获取首页数据
$banners = getBanners('home', 5);
$navChannels = getNavChannels();
// SEO: canonical
$siteUrl = rtrim(config('site_url', SITE_URL), '/');
$canonicalUrl = $siteUrl . '/';
// 获取首页展示的栏目(is_home=1)
$homeChannels = channelModel()->getHomeChannels();
// 获取产品分类(用于首页产品区块)
$productCategories = productCategoryModel()->getTopLevel(6);
// 为每个栏目获取内容,并构建 ID => channel 映射
$homeChannelsMap = [];
foreach ($homeChannels as &$hChannel) {
if ($hChannel['type'] === 'product') {
// 产品类型:优先推荐产品,没有则显示最新
$hChannel['contents'] = getProducts(0, 8, 0, ['is_recommend' => true]);
if (empty($hChannel['contents'])) {
$hChannel['contents'] = getProducts(0, 8, 0);
}
$hChannel['is_product'] = true;
$hChannel['categories'] = $productCategories;
} else {
// 其他类型:从内容表获取
$hChannel['contents'] = getContents((int)$hChannel['id'], 6, 0, ['include_children' => true]);
$hChannel['is_product'] = false;
}
$homeChannelsMap[(int)$hChannel['id']] = $hChannel;
}
unset($hChannel);
// 获取关于我们栏目(用于简介区块)
$aboutChannel = getChannelBySlug('about');
// 轮播图高度配置
$bannerHeightPC = (int)config('banner_height_pc', 650);
$bannerHeightMobile = (int)config('banner_height_mobile', 300);
// 主题色
$primaryColor = config('primary_color', '#3B82F6');
// 客户评价数据
$testimonials = json_decode(config('home_testimonials', '[]'), true) ?: [];
// 区块配置(顺序+开关)
$blocksConfig = json_decode(config('home_blocks_config', ''), true);
if (!$blocksConfig) {
// 未配置时使用默认顺序(兼容未升级的情况)
$blocksConfig = [
['type' => 'banner', 'enabled' => true],
['type' => 'about', 'enabled' => true],
['type' => 'stats', 'enabled' => true],
['type' => 'channels', 'enabled' => true],
['type' => 'testimonials', 'enabled' => true],
['type' => 'advantage', 'enabled' => true],
['type' => 'cta', 'enabled' => true],
];
}
// 迁移旧的 channels 整块为独立的 channel:{id} 区块
$channelIdsInConfig = [];
$migratedConfig = [];
foreach ($blocksConfig as $b) {
if ($b['type'] === 'channels') {
// 旧格式:展开为各个栏目独立区块
foreach ($homeChannels as $hc) {
$migratedConfig[] = ['type' => 'channel:' . $hc['id'], 'enabled' => $b['enabled'] ?? true];
$channelIdsInConfig[] = (int)$hc['id'];
}
} else {
$migratedConfig[] = $b;
if (str_starts_with($b['type'], 'channel:')) {
$channelIdsInConfig[] = (int)substr($b['type'], 8);
}
}
}
// 新增的首页栏目自动追加到末尾
foreach ($homeChannels as $hc) {
if (!in_array((int)$hc['id'], $channelIdsInConfig)) {
$migratedConfig[] = ['type' => 'channel:' . $hc['id'], 'enabled' => true];
}
}
$blocksConfig = $migratedConfig;
// 区块模板映射
$blockTemplates = [
'banner' => theme_path('blocks/banner.php'),
'about' => theme_path('blocks/about.php'),
'stats' => theme_path('blocks/stats.php'),
'testimonials' => theme_path('blocks/testimonials.php'),
'advantage' => theme_path('blocks/advantage.php'),
'cta' => theme_path('blocks/cta.php'),
];
// Swiper轮播图资源
$extraCss = '
<link rel="stylesheet" href="/assets/swiper/swiper-bundle.min.css">
<style>
.banner-swiper { height: ' . $bannerHeightMobile . 'px; }
@media (min-width: 768px) { .banner-swiper { height: ' . $bannerHeightPC . 'px; } }
.banner-swiper .swiper-pagination-bullet-active { opacity: 1; background: ' . $primaryColor . '; width: 24px; border-radius: 6px; }
</style>';
$extraJs = '
<script src="/assets/swiper/swiper-bundle.min.js"></script>
<script>
new Swiper(".banner-swiper", {
loop: true,
autoplay: { delay: 5000, disableOnInteraction: false },
effect: "fade",
fadeEffect: { crossFade: true },
pagination: { el: ".swiper-pagination", clickable: true },
navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev" }
});
// Home page product category filter
(function() {
const nav = document.getElementById("productCategoryNav");
const grid = document.getElementById("productGrid");
if (!nav || !grid) return;
const buttons = nav.querySelectorAll(".category-btn");
const items = grid.querySelectorAll(".product-item");
buttons.forEach(btn => {
btn.addEventListener("click", function() {
const cat = this.dataset.category;
// Update button state
buttons.forEach(b => {
b.classList.remove("bg-primary", "text-white");
b.classList.add("bg-gray-100", "text-gray-600");
});
this.classList.remove("bg-gray-100", "text-gray-600");
this.classList.add("bg-primary", "text-white");
// Filter products
items.forEach(item => {
const itemCat = item.dataset.category;
if (cat === "all" || itemCat === cat) {
item.style.display = "";
item.style.opacity = "0";
setTimeout(() => { item.style.opacity = "1"; }, 50);
} else {
item.style.display = "none";
}
});
});
});
})();
</script>';
// 引入头部
require_once theme_path('layouts/header.php');
// 动态渲染首页区块
foreach ($blocksConfig as $block) {
if (empty($block['enabled'])) continue;
$type = $block['type'] ?? '';
if (str_starts_with($type, 'channel:')) {
// 独立栏目区块
$channelId = (int)substr($type, 8);
$currentChannel = $homeChannelsMap[$channelId] ?? null;
if ($currentChannel) {
require theme_path('blocks/channel.php');
}
} elseif (isset($blockTemplates[$type]) && file_exists($blockTemplates[$type])) {
require $blockTemplates[$type];
}
}
require_once theme_path('layouts/footer.php');
?>