271 lines
10 KiB
PHP
271 lines
10 KiB
PHP
<?php
|
||
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';
|
||
|
||
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;
|
||
}
|
||
|
||
$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'] : [];
|
||
}
|
||
|
||
// 分类排序: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;
|
||
}
|
||
}
|
||
|
||
/** @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 htmlspecialchars($baseUrl); ?>/">主页</a></li>
|
||
<li class="current">新闻中心</li>
|
||
</ol>
|
||
</nav>
|
||
</div>
|
||
</div><!-- End Page Title -->
|
||
|
||
<div class="container">
|
||
<div class="row">
|
||
|
||
<!-- 左侧:新闻分类 -->
|
||
<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
|
||
$childId = (int) ($childCat['id'] ?? 0);
|
||
$childName = (string) ($childCat['name'] ?? '未命名');
|
||
$childLink = $baseUrlBlog . '/blog?' . http_build_query(['cate' => $childId, 'page' => 1]);
|
||
?>
|
||
<li>
|
||
<a
|
||
href="<?php echo htmlspecialchars($childLink); ?>"
|
||
class="<?php echo $selectedCateId === $childId ? 'active' : ''; ?>"><?php echo htmlspecialchars($childName); ?></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>
|
||
<?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>
|
||
</div>
|
||
|
||
</main>
|
||
|
||
<?php require_once __DIR__ . '/footer.php'; ?>
|