tp/public/themes/default/article_list.php
2026-03-12 17:53:36 +08:00

407 lines
13 KiB
PHP

<?php
// 文章列表(每页 10 条,支持翻页)
$rootPath = dirname(__DIR__, 3) . DIRECTORY_SEPARATOR;
require_once $rootPath . 'vendor/autoload.php';
use think\facade\Db;
use think\App;
$app = new App($rootPath);
$app->initialize();
$host = $_SERVER['HTTP_HOST'] ?? '';
$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http';
$baseUrl = $scheme . '://' . $host;
$themeBase = '/themes/default';
function formatImageUrl($path, $baseUrl) {
if (empty($path)) return '';
if (preg_match('/^https?:\/\//', $path)) return $path;
return $baseUrl . (strpos($path, '/') === 0 ? '' : '/') . $path;
}
function stripHtmlText($html) {
if (empty($html)) return '';
$text = preg_replace('/<[^>]+>/', ' ', $html);
$text = preg_replace('/\s+/', ' ', $text);
return trim($text);
}
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$pageSize = 10;
try {
$tenantDomain = Db::name('mete_tenant_domain')
->where('full_domain', $host)
->where('status', 1)
->whereNull('delete_time')
->find();
$tid = $tenantDomain ? (int)$tenantDomain['tid'] : 0;
if ($tid === 0) {
die("未找到相关内容");
}
$setting = Db::name('mete_system_site_setting')->where('tid', $tid)->whereNull('delete_time')->find();
$tenant = Db::name('mete_tenant')->where('id', $tid)->whereNull('delete_time')->find();
$siteInfo = array_merge($tenant ?: [], $setting ?: []);
$baseQuery = Db::name('mete_articles')
->where('tid', $tid)
->whereNull('delete_time');
// 如果存在 status 字段,通常用 2 表示已发布;不存在也不影响(交给数据库报错会影响页面)
// 为避免因不同库结构导致报错,这里不强依赖 status 过滤。
$total = (int)$baseQuery->count();
$totalPages = (int)max(1, ceil($total / $pageSize));
if ($page > $totalPages) $page = $totalPages;
$offset = ($page - 1) * $pageSize;
$articles = $baseQuery
->order('publish_date', 'desc')
->limit($offset, $pageSize)
->select()
->toArray();
} catch (\Exception $e) {
die("系统错误: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html class="no-js" lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title><?php echo htmlspecialchars(($siteInfo['sitename'] ?? '') ?: '新闻中心'); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="<?php echo $themeBase; ?>/assets/css/bootstrap-5.0.0-beta1.min.css" />
<link rel="stylesheet" href="<?php echo $themeBase; ?>/assets/css/LineIcons.2.0.css" />
<link rel="stylesheet" href="<?php echo $themeBase; ?>/assets/css/animate.css" />
<link rel="stylesheet" href="<?php echo $themeBase; ?>/assets/css/lindy-uikit.css" />
<link rel="stylesheet" href="<?php echo $themeBase; ?>/assets/css/all.min.css" />
<style>
:root{
--ink:#0f172a;
--muted:#475569;
--line:rgba(15,23,42,.10);
--card:rgba(255,255,255,.86);
--accent:#5864FF;
--shadow:0 16px 50px rgba(2,6,23,.12);
}
body{
color:var(--ink);
background:
radial-gradient(1100px 420px at 12% 0%, rgba(88,100,255,.20), transparent 55%),
radial-gradient(900px 380px at 86% 10%, rgba(34,197,94,.12), transparent 55%),
linear-gradient(180deg, #fbfdff 0%, #f6f8ff 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
}
.topbar{
margin-top:18px;
border:1px solid var(--line);
border-radius:16px;
background:var(--card);
backdrop-filter: blur(10px);
box-shadow: 0 10px 30px rgba(2,6,23,.06);
}
.topbar .navbar{ padding: 12px 14px; }
.navbar-brand img{ max-width: 260px; }
.back-link{
display:inline-flex;
align-items:center;
gap:8px;
padding:10px 12px;
border-radius:999px;
border:1px solid var(--line);
color:var(--ink);
background:rgba(255,255,255,.55);
text-decoration:none;
transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.back-link:hover{
transform: translateY(-1px);
box-shadow: 0 10px 25px rgba(2,6,23,.10);
background: rgba(255,255,255,.75);
}
.back-link i{ color: var(--accent); }
.page-head{
padding: 34px 0 10px;
text-align:center;
}
.page-title{
font-size: 34px;
font-weight: 800;
letter-spacing: -0.02em;
margin: 0 0 10px;
}
.page-subtitle{
color: var(--muted);
margin: 0;
}
.list-wrap{
padding: 10px 0 70px;
flex: 1;
}
.list-card{
background: var(--card);
border: 1px solid var(--line);
border-radius: 18px;
box-shadow: var(--shadow);
padding: 18px;
backdrop-filter: blur(12px);
}
.news-grid{
display:grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
@media (max-width: 992px){
.news-grid{ grid-template-columns: 1fr; }
.page-title{ font-size: 28px; }
}
.news-item{
display:flex;
gap: 14px;
padding: 14px;
border: 1px solid rgba(15,23,42,.08);
border-radius: 16px;
background: rgba(255,255,255,.70);
text-decoration:none;
color: inherit;
transition: transform .14s ease, box-shadow .14s ease, background .14s ease;
overflow:hidden;
}
.news-item:hover{
transform: translateY(-2px);
box-shadow: 0 16px 45px rgba(2,6,23,.12);
background: rgba(255,255,255,.82);
}
.thumb{
width: 160px;
height: 108px;
border-radius: 14px;
overflow:hidden;
flex: 0 0 auto;
background: rgba(15,23,42,.06);
border: 1px solid rgba(15,23,42,.08);
}
.thumb img{
width:100%;
height:100%;
object-fit: cover;
display:block;
}
@media (max-width: 576px){
.thumb{ width: 120px; height: 86px; }
}
.news-body{ min-width: 0; flex: 1; }
.news-title{
font-size: 16px;
font-weight: 800;
line-height: 1.35;
margin: 0 0 8px;
display:-webkit-box;
line-clamp: 2;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow:hidden;
}
.news-desc{
margin: 0 0 10px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
display:-webkit-box;
line-clamp: 2;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow:hidden;
}
.news-meta{
display:flex;
gap: 12px;
flex-wrap: wrap;
color: var(--muted);
font-size: 13px;
align-items:center;
}
.news-meta i{ color: var(--accent); }
.pager{
display:flex;
justify-content:center;
margin-top: 22px;
}
.pagination{
gap: 8px;
flex-wrap: wrap;
}
.page-link{
border-radius: 12px !important;
border: 1px solid rgba(15,23,42,.10);
color: var(--ink);
background: rgba(255,255,255,.65);
box-shadow: 0 10px 25px rgba(2,6,23,.06);
}
.page-item.active .page-link{
background: var(--accent);
border-color: var(--accent);
color: #fff;
box-shadow: 0 18px 50px rgba(88,100,255,.30);
}
.page-item.disabled .page-link{
background: rgba(255,255,255,.45);
color: rgba(71,85,105,.6);
}
footer{
background: rgba(2,6,23,.92);
padding: 44px 0;
color: rgba(226,232,240,.86);
}
footer p{ margin: 0; }
</style>
</head>
<body>
<div class="container">
<div class="topbar">
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand" href="/">
<img src="<?php echo formatImageUrl($siteInfo['logo'] ?? '', $baseUrl); ?>" alt="Logo" />
</a>
<div class="ms-auto">
<a class="back-link" href="/#newsCenter">
<i class="lni lni-arrow-left"></i>
返回首页
</a>
</div>
</nav>
</div>
</div>
<section class="page-head">
<div class="container">
<h1 class="page-title">新闻中心</h1>
<p class="page-subtitle">最新动态与行业资讯,持续更新。</p>
</div>
</section>
<section class="list-wrap">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-10">
<div class="list-card">
<?php if (empty($articles)): ?>
<div class="text-center py-5" style="color: var(--muted);">
暂无文章
</div>
<?php else: ?>
<div class="news-grid">
<?php foreach ($articles as $a): ?>
<?php
$title = $a['title'] ?? '未命名';
$publishDate = !empty($a['publish_date']) ? date('Y-m-d', strtotime($a['publish_date'])) : '';
$views = $a['views'] ?? ($a['pv'] ?? 0);
$desc = $a['summary'] ?? ($a['description'] ?? '');
if (empty($desc) && !empty($a['content'])) $desc = stripHtmlText($a['content']);
if (mb_strlen($desc) > 120) $desc = mb_substr($desc, 0, 120) . '…';
$thumb = $a['thumb'] ?? ($a['image'] ?? '');
$thumbUrl = $thumb ? formatImageUrl($thumb, $baseUrl) : '';
$id = (int)($a['id'] ?? 0);
?>
<a class="news-item" href="/news/article_detail.php?id=<?php echo $id; ?>">
<div class="thumb" aria-hidden="true">
<?php if ($thumbUrl): ?>
<img src="<?php echo htmlspecialchars($thumbUrl); ?>" alt="<?php echo htmlspecialchars($title); ?>">
<?php endif; ?>
</div>
<div class="news-body">
<h3 class="news-title"><?php echo htmlspecialchars($title); ?></h3>
<p class="news-desc"><?php echo htmlspecialchars($desc ?: '暂无摘要'); ?></p>
<div class="news-meta">
<?php if ($publishDate): ?>
<span><i class="lni lni-calendar"></i> <?php echo htmlspecialchars($publishDate); ?></span>
<?php endif; ?>
<span><i class="lni lni-eye"></i> <?php echo (int)$views; ?></span>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
<div class="pager">
<nav aria-label="Page navigation">
<ul class="pagination">
<?php
$mkUrl = function($p){
return '/news?page=' . (int)$p;
};
$prev = max(1, $page - 1);
$next = min($totalPages, $page + 1);
$window = 2;
$start = max(1, $page - $window);
$end = min($totalPages, $page + $window);
?>
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo $page <= 1 ? '#' : $mkUrl($prev); ?>" tabindex="-1">上一页</a>
</li>
<?php if ($start > 1): ?>
<li class="page-item"><a class="page-link" href="<?php echo $mkUrl(1); ?>">1</a></li>
<?php if ($start > 2): ?>
<li class="page-item disabled"><span class="page-link">…</span></li>
<?php endif; ?>
<?php endif; ?>
<?php for ($p = $start; $p <= $end; $p++): ?>
<li class="page-item <?php echo $p === $page ? 'active' : ''; ?>">
<a class="page-link" href="<?php echo $mkUrl($p); ?>"><?php echo (int)$p; ?></a>
</li>
<?php endfor; ?>
<?php if ($end < $totalPages): ?>
<?php if ($end < $totalPages - 1): ?>
<li class="page-item disabled"><span class="page-link">…</span></li>
<?php endif; ?>
<li class="page-item"><a class="page-link" href="<?php echo $mkUrl($totalPages); ?>"><?php echo (int)$totalPages; ?></a></li>
<?php endif; ?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo $page >= $totalPages ? '#' : $mkUrl($next); ?>">下一页</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="container text-center">
<p>Copyright © 2026 <?php echo htmlspecialchars($siteInfo['companyname'] ?? ''); ?> | <?php echo htmlspecialchars($siteInfo['icp'] ?? ''); ?></p>
</div>
</footer>
<script src="<?php echo $themeBase; ?>/assets/js/bootstrap-5.0.0-beta1.min.js"></script>
</body>
</html>