增加企业产品分类

This commit is contained in:
2026-03-21 14:12:21 +08:00
parent 9281c1dc35
commit 2a6b14f698
13 changed files with 1139 additions and 277 deletions
+243 -134
View File
@@ -1,162 +1,271 @@
<?php
$pageTitle = '新闻中心 - Nova';
$pageDescription = 'Nova Bootstrap Template 的新闻中心页面';
$pageKeywords = 'blog, news, nova';
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
$pageTitle = '新闻中心 - 云泽';
$pageDescription = '云泽的新闻中心页面';
$pageKeywords = 'blog, news, 云泽';
require_once __DIR__ . '/header.php';
?>
<?php
// 本页使用本地分页:根据 query 参数 ?page=n 只展示 newsList 的切片
$allNewsList = is_array($newsList ?? null) ? $newsList : [];
$currentPage = (int)($_GET['page'] ?? 1);
if ($currentPage < 1) {
$currentPage = 1;
use think\facade\Request;
// 确保 baseUrl 不带查询参数,避免分页链接出现双 ? 的问题
$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? '';
$baseUrlBlog = $scheme . '://' . $host;
// 新闻列表:服务端分页
// Nginx 未传递 QUERY_STRING,从 REQUEST_URI 解析
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
$queryString = '';
if (strpos($requestUri, '?') !== false) {
$queryString = substr($requestUri, strpos($requestUri, '?') + 1);
}
parse_str($queryString, $queryArr);
$selectedCateId = isset($queryArr['cate']) ? (int) $queryArr['cate'] : 0;
if ($selectedCateId < 0) {
$selectedCateId = 0;
}
$pageSize = 8; // 每页展示 8 条(grid 中每条占 col-lg-6,所以刚好四行)
$totalCount = count($allNewsList);
$totalPages = (int)ceil($totalCount / $pageSize);
if ($totalPages < 1) {
$totalPages = 1;
$listPage = isset($queryArr['page']) ? max(1, (int) $queryArr['page']) : 1;
$pageSize = 8;
$host = $_SERVER['HTTP_HOST'] ?? '';
$blogNewsQuery = [
'baseUrl' => $host,
'page' => $listPage,
'page_size' => $pageSize,
];
if ($selectedCateId > 0) {
$blogNewsQuery['cate'] = $selectedCateId;
}
$blogNewsUrl = $apiUrl . '/getCenterNews?' . http_build_query($blogNewsQuery);
$blogNewsRes = fetchApiData($blogNewsUrl);
// count:当前「全部」或某分类下的条数(用于分页);total:当前租户下新闻总条数
$listTotal = array_key_exists('count', $blogNewsRes ?? [])
? (int) $blogNewsRes['count']
: (int) ($blogNewsRes['total'] ?? 0);
$allNewsList = is_array($blogNewsRes['list'] ?? null) ? $blogNewsRes['list'] : [];
$totalPages = max(1, (int) ceil($listTotal / $pageSize));
if ($listPage > $totalPages && $listTotal > 0) {
$listPage = $totalPages;
$blogNewsQuery['page'] = $listPage;
$blogNewsUrl = $apiUrl . '/getCenterNews?' . http_build_query($blogNewsQuery);
$blogNewsRes = fetchApiData($blogNewsUrl);
$listTotal = array_key_exists('count', $blogNewsRes ?? [])
? (int) $blogNewsRes['count']
: (int) ($blogNewsRes['total'] ?? 0);
$allNewsList = is_array($blogNewsRes['list'] ?? null) ? $blogNewsRes['list'] : [];
}
if ($currentPage > $totalPages) {
$currentPage = $totalPages;
// 分类排序:sort 升序,其次 id
$categories = is_array($articleCategoryList ?? null) ? $articleCategoryList : [];
usort($categories, static function ($a, $b) {
$sa = (int) ($a['sort'] ?? 0);
$sb = (int) ($b['sort'] ?? 0);
if ($sa !== $sb) {
return $sa <=> $sb;
}
return ((int) ($a['id'] ?? 0)) <=> ((int) ($b['id'] ?? 0));
});
// 构建分类父子结构
$rootCategories = [];
$childrenMap = [];
foreach ($categories as $cat) {
$catId = (int) ($cat['id'] ?? 0);
$catCid = (int) ($cat['cid'] ?? 0);
if ($catId <= 0) continue;
if ($catCid === 0) {
$rootCategories[] = $cat;
} else {
if (!isset($childrenMap[$catCid])) {
$childrenMap[$catCid] = [];
}
$childrenMap[$catCid][] = $cat;
}
}
$newsListForPage = array_slice($allNewsList, ($currentPage - 1) * $pageSize, $pageSize);
/** @return array<string, int|string> */
$blogPageQuery = static function (int $lp, int $cateId): array {
return ['cate' => $cateId, 'page' => $lp];
};
?>
<!-- Page Title -->
<div class="page-title dark-background" data-aos="fade" style="background-image: url(<?php echo $baseUrl; ?>/themes/template3/assets/img/blog-page-title-bg.jpg);">
<div class="container">
<h1>新闻中心</h1>
<nav class="breadcrumbs">
<ol>
<li><a href="<?php echo $baseUrl; ?>/">主页</a></li>
<li class="current">新闻中心</li>
</ol>
</nav>
</div>
</div><!-- End Page Title -->
<!-- Page Title -->
<div class="page-title dark-background" data-aos="fade" style="background-image: url(<?php echo $baseUrl; ?>/themes/template3/assets/img/blog-page-title-bg.jpg);">
<div class="container">
<h1>新闻中心</h1>
<nav class="breadcrumbs">
<ol>
<li><a href="<?php echo htmlspecialchars($baseUrl); ?>/">主页</a></li>
<li class="current">新闻中心</li>
</ol>
</nav>
</div>
</div><!-- End Page Title -->
<div class="container">
<div class="row">
<div class="container">
<div class="row">
<div class="col-lg-12">
<!-- Blog Posts Section -->
<section id="blog-posts" class="blog-posts section">
<div class="container">
<div class="row gy-4">
<?php if (!empty($newsList)): ?>
<?php foreach ($newsListForPage as $i => $item): ?>
<!-- 左侧:新闻分类 -->
<aside class="col-lg-3 col-md-4 blog-layout-sidebar">
<nav class="blog-category-nav" aria-label="新闻分类">
<h2 class="nav-title">新闻分类</h2>
<ul>
<li>
<a
href="<?php echo htmlspecialchars($baseUrlBlog); ?>/blog?<?php echo http_build_query(['cate' => 0, 'page' => 1]); ?>"
class="<?php echo $selectedCateId === 0 ? 'active' : ''; ?>">全部</a>
</li>
<?php foreach ($rootCategories as $rootCat): ?>
<?php
$rootId = (int) ($rootCat['id'] ?? 0);
$rootName = (string) ($rootCat['name'] ?? '未命名');
$rootLink = $baseUrlBlog . '/blog?' . http_build_query(['cate' => $rootId, 'page' => 1]);
$children = $childrenMap[$rootId] ?? [];
$hasChildren = !empty($children);
$isParentActive = $selectedCateId === $rootId;
?>
<li>
<a
href="<?php echo htmlspecialchars($rootLink); ?>"
class="<?php echo $isParentActive ? 'active' : ''; ?>"><?php echo htmlspecialchars($rootName); ?></a>
<?php if ($hasChildren): ?>
<ul class="children">
<?php foreach ($children as $childCat): ?>
<?php
$title = $item['title'] ?? '无标题';
$author = $item['author'] ?? '';
$category = $item['category'] ?? ($item['category_name'] ?? '');
$publishDateRaw = $item['publish_date'] ?? '';
$postDate = !empty($publishDateRaw) ? date('Y年m月d日', strtotime($publishDateRaw)) : '';
$thumbUrl = $item['thumb'] ?? ($item['image'] ?? '');
if (!empty($thumbUrl)) {
if (strpos($thumbUrl, 'http') === 0) {
$imgSrc = $thumbUrl;
} elseif (strpos($thumbUrl, '/') === 0) {
$imgSrc = $apiUrl . $thumbUrl;
} else {
$imgSrc = $apiUrl . '/' . $thumbUrl;
}
} else {
$imgIndex = ($i % 6) + 1; // blog-1.jpg ~ blog-6.jpg
$imgSrc = $baseUrl . "/themes/template3/assets/img/blog/blog-{$imgIndex}.jpg";
}
// 文章详情链接必须走前台域名,否则 api.yunzer.cn 上租户解析不生效
$articleUrl = $baseUrl . '/article_detail/' . ($item['id'] ?? 0);
$childId = (int) ($childCat['id'] ?? 0);
$childName = (string) ($childCat['name'] ?? '未命名');
$childLink = $baseUrlBlog . '/blog?' . http_build_query(['cate' => $childId, 'page' => 1]);
?>
<div class="col-lg-3 col-md-6">
<article>
<div class="post-media">
<img src="<?php echo htmlspecialchars($imgSrc); ?>" alt="" class="post-media-img" />
</div>
<p class="post-category"><?php echo htmlspecialchars($category); ?></p>
<h2 class="title">
<a href="<?php echo htmlspecialchars($articleUrl); ?>">
<?php echo htmlspecialchars($title); ?>
</a>
</h2>
<div class="d-flex align-items-center">
<img src="<?php echo $baseUrl; ?>/themes/template3/assets/img/blog/blog-author.jpg" alt="" class="img-fluid post-author-img flex-shrink-0">
<div class="post-meta">
<p class="post-author"><?php echo htmlspecialchars($author); ?></p>
<p class="post-date">
<time datetime="<?php echo htmlspecialchars($publishDateRaw); ?>">
<?php echo htmlspecialchars($postDate); ?>
</time>
</p>
</div>
</div>
</article>
</div><!-- End post list item -->
<?php endforeach; ?>
<?php else: ?>
<div class="col-12 text-center">
<p>暂无新闻数据</p>
</div>
<?php endif; ?>
</div>
</div>
</section><!-- /Blog Posts Section -->
<!-- Blog Pagination Section -->
<section id="blog-pagination" class="blog-pagination section">
<div class="container">
<div class="d-flex justify-content-center">
<ul>
<?php
$prevPage = max(1, $currentPage - 1);
$nextPage = min($totalPages, $currentPage + 1);
$showMax = 10;
$showTotal = min($totalPages, $showMax);
?>
<li><a href="<?php echo $baseUrl; ?>/blog?page=<?php echo $prevPage; ?>"><i class="bi bi-chevron-left"></i></a></li>
<?php for ($p = 1; $p <= $showTotal; $p++): ?>
<li>
<a
href="<?php echo $baseUrl; ?>/blog?page=<?php echo $p; ?>"
<?php echo ($p === $currentPage) ? 'class="active"' : ''; ?>
>
<?php echo $p; ?>
</a>
href="<?php echo htmlspecialchars($childLink); ?>"
class="<?php echo $selectedCateId === $childId ? 'active' : ''; ?>"><?php echo htmlspecialchars($childName); ?></a>
</li>
<?php endfor; ?>
<li><a href="<?php echo $baseUrl; ?>/blog?page=<?php echo $nextPage; ?>"><i class="bi bi-chevron-right"></i></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<!-- 右侧:当前分类下的文章列表 -->
<div class="col-lg-9 col-md-8">
<section id="blog-posts" class="blog-posts section">
<div class="container h520 px-0">
<div class="row gy-4">
<?php if (!empty($allNewsList)): ?>
<?php foreach ($allNewsList as $i => $item): ?>
<?php
$title = $item['title'] ?? '无标题';
$author = $item['author'] ?? '';
$cateKey = (int) ($item['cate'] ?? 0);
$category = $item['category'] ?? ($item['category_name'] ?? '');
$publishDateRaw = $item['publish_date'] ?? '';
$postDate = !empty($publishDateRaw) ? date('Y年m月d日', strtotime((string) $publishDateRaw)) : '';
$thumbUrl = $item['thumb'] ?? ($item['image'] ?? '');
if (!empty($thumbUrl)) {
if (strpos($thumbUrl, 'http') === 0) {
$imgSrc = $thumbUrl;
} elseif (strpos($thumbUrl, '/') === 0) {
$imgSrc = $apiUrl . $thumbUrl;
} else {
$imgSrc = $apiUrl . '/' . $thumbUrl;
}
} else {
$imgIndex = ($i % 6) + 1;
$imgSrc = $baseUrl . "/themes/template3/assets/img/blog/blog-{$imgIndex}.jpg";
}
$articleUrl = $baseUrl . '/article_detail/' . ($item['id'] ?? 0);
?>
<div class="col-lg-3 col-md-6">
<article>
<div class="post-media">
<img src="<?php echo htmlspecialchars($imgSrc); ?>" alt="" class="post-media-img" />
</div>
<!-- <p class="post-category"><?php echo htmlspecialchars($category); ?></p> -->
<h2 class="title">
<a href="<?php echo htmlspecialchars($articleUrl); ?>">
<?php echo htmlspecialchars($title); ?>
</a>
</h2>
<div class="d-flex align-items-center">
<img src="<?php echo $baseUrl; ?>/themes/template3/assets/img/blog/blog-author.jpg" alt="" class="img-fluid post-author-img flex-shrink-0">
<div class="post-meta">
<p class="post-author"><?php echo htmlspecialchars($author); ?></p>
<p class="post-date">
<time datetime="<?php echo htmlspecialchars($publishDateRaw); ?>">
<?php echo htmlspecialchars($postDate); ?>
</time>
</p>
</div>
</div>
</article>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="col-12 text-center py-5">
<p class="mb-0"><?php echo $selectedCateId > 0 ? '该分类下暂无新闻' : '暂无新闻数据'; ?></p>
</div>
</div>
</section><!-- /Blog Pagination Section -->
<?php endif; ?>
</div>
</div>
</section><!-- /Blog Posts Section -->
<?php if (!empty($allNewsList) && $totalPages >= 1): ?>
<section id="blog-pagination" class="blog-pagination section">
<div class="container px-0">
<div class="d-flex justify-content-center">
<ul>
<?php
$prevPage = max(1, $listPage - 1);
$nextPage = min($totalPages, $listPage + 1);
$showMax = 10;
$showTotal = min($totalPages, $showMax);
$prevHref = $baseUrlBlog . '/blog?' . http_build_query($blogPageQuery($prevPage, $selectedCateId));
$nextHref = $baseUrlBlog . '/blog?' . http_build_query($blogPageQuery($nextPage, $selectedCateId));
?>
<li><a href="<?php echo htmlspecialchars($prevHref); ?>"><i class="bi bi-chevron-left"></i></a></li>
<?php for ($p = 1; $p <= $showTotal; $p++): ?>
<?php $pHref = $baseUrlBlog . '/blog?' . http_build_query($blogPageQuery($p, $selectedCateId)); ?>
<li>
<a
href="<?php echo htmlspecialchars($pHref); ?>"
<?php echo ($p === $listPage) ? 'class="active"' : ''; ?>>
<?php echo $p; ?>
</a>
</li>
<?php endfor; ?>
<li><a href="<?php echo htmlspecialchars($nextHref); ?>"><i class="bi bi-chevron-right"></i></a></li>
</ul>
</div>
</div>
</section><!-- /Blog Pagination Section -->
<?php endif; ?>
</div>
</div>
</main>
</div>
</div>
</main>
<?php require_once __DIR__ . '/footer.php'; ?>