新增文章库
This commit is contained in:
1 parent
90bd40ffde
commit
4e6d6e5307
12 files changed
+1119
-4
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* article/data.php —— 文章库 JSON 数据接口
|
||||
* data.php?mode=list 文章元数据列表(按分区分组,含分区名)
|
||||
* data.php?mode=content&id=N 单篇文章完整数据(含 markdown 正文)
|
||||
* 说明:仅返回前台可见(enabled=1)文章;隐文不对外。
|
||||
*/
|
||||
require_once dirname(__DIR__) . '/includes/db.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
|
||||
$pdo = db();
|
||||
|
||||
function json_out(array $data): void
|
||||
{
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
$mode = (string)($_GET['mode'] ?? 'list');
|
||||
|
||||
if ($mode === 'content') {
|
||||
$id = (int)($_GET['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
json_out(['ok' => false, 'msg' => '缺少文章 id']);
|
||||
}
|
||||
$st = $pdo->prepare('SELECT id, part, title, summary, markdown, pinned, created_at, updated_at
|
||||
FROM articles WHERE id = ? AND enabled = 1 LIMIT 1');
|
||||
$st->execute([$id]);
|
||||
$row = $st->fetch();
|
||||
if (!$row) {
|
||||
json_out(['ok' => false, 'msg' => '文章不存在或已隐藏']);
|
||||
}
|
||||
json_out(['ok' => true, 'article' => $row]);
|
||||
}
|
||||
|
||||
// ---- 默认:list 分组元数据(分区顺序取各分区首篇 id 序;分区内置顶 → sort → id) ----
|
||||
$rows = $pdo->query('SELECT id, part, title, summary, pinned, sort, created_at, updated_at
|
||||
FROM articles WHERE enabled = 1 ORDER BY id ASC')->fetchAll();
|
||||
$parts = [];
|
||||
if ($rows) {
|
||||
$partOrder = [];
|
||||
$byPart = [];
|
||||
foreach ($rows as $r) {
|
||||
$part = trim((string)$r['part']);
|
||||
if ($part === '') {
|
||||
$part = '未分类';
|
||||
}
|
||||
if (!isset($byPart[$part])) {
|
||||
$byPart[$part] = [];
|
||||
$partOrder[] = $part;
|
||||
}
|
||||
$byPart[$part][] = $r;
|
||||
}
|
||||
foreach ($partOrder as $part) {
|
||||
$items = $byPart[$part];
|
||||
usort($items, static function (array $a, array $b): int {
|
||||
if ((int)$a['pinned'] !== (int)$b['pinned']) {
|
||||
return (int)$b['pinned'] - (int)$a['pinned'];
|
||||
}
|
||||
if ((int)$a['sort'] !== (int)$b['sort']) {
|
||||
return (int)$a['sort'] <=> (int)$b['sort'];
|
||||
}
|
||||
return (int)$a['id'] <=> (int)$b['id'];
|
||||
});
|
||||
$parts[] = ['part' => $part, 'articles' => $items];
|
||||
}
|
||||
}
|
||||
json_out(['ok' => true, 'parts' => $parts]);
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
/**
|
||||
* article/index.php —— 文章区(文章库)
|
||||
* 结构:顶部栏(返回首页 + 当页名 + 分区/段落开关)
|
||||
* 内容三栏:左 15% 文章标题栏(按分区) / 中 15% 当前文章段落目录 / 右 70% 正文(Markdown 渲染)
|
||||
* 文章正文存 SQLite;图片上传到 data/articles/img/,正文以 data/articles/img/ 站点相对路径引用,
|
||||
* 渲染时自动补全前缀。三栏宽度可拖拽调节,标题栏 / 段落栏可分别隐藏。
|
||||
* 打开页面为「先列表后阅读」:默认右栏展示全部分区文章列表,点击文章进入阅读。
|
||||
*/
|
||||
$P = '../';
|
||||
require_once dirname(__DIR__) . '/includes/layout.php';
|
||||
|
||||
$pdo = db();
|
||||
$noteText = setting_get('article_note', '');
|
||||
|
||||
// 全部可见文章(按 id 稳定取回,分区顺序取各分区首篇 id 序;分区内置顶 → sort → id)
|
||||
$all = $pdo->query('SELECT id, part, title, summary, sort, created_at, updated_at
|
||||
FROM articles WHERE enabled = 1 ORDER BY id ASC')->fetchAll();
|
||||
$rows = [];
|
||||
$partOrder = [];
|
||||
$byPart = [];
|
||||
foreach ($all as $r) {
|
||||
$part = trim((string)$r['part']);
|
||||
if ($part === '') {
|
||||
$part = '未分类';
|
||||
}
|
||||
if (!isset($byPart[$part])) {
|
||||
$byPart[$part] = [];
|
||||
$partOrder[] = $part;
|
||||
}
|
||||
$byPart[$part][] = $r;
|
||||
}
|
||||
foreach ($partOrder as $part) {
|
||||
$items = $byPart[$part];
|
||||
usort($items, static function (array $a, array $b): int {
|
||||
if ((int)$a['pinned'] !== (int)$b['pinned']) {
|
||||
return (int)$b['pinned'] - (int)$a['pinned'];
|
||||
}
|
||||
if ((int)$a['sort'] !== (int)$b['sort']) {
|
||||
return (int)$a['sort'] <=> (int)$b['sort'];
|
||||
}
|
||||
return (int)$a['id'] <=> (int)$b['id'];
|
||||
});
|
||||
$rows[] = ['part' => $part, 'articles' => $items];
|
||||
}
|
||||
$total = count($all);
|
||||
|
||||
// 当前阅读文章
|
||||
$cur = null;
|
||||
$cid = (int)($_GET['id'] ?? 0);
|
||||
if ($cid > 0) {
|
||||
$st = $pdo->prepare('SELECT * FROM articles WHERE id = ? AND enabled = 1 LIMIT 1');
|
||||
$st->execute([$cid]);
|
||||
$cur = $st->fetch();
|
||||
if (!$cur) {
|
||||
$cur = null; // 无效或已隐藏:回退到列表
|
||||
}
|
||||
}
|
||||
|
||||
layout_head($cur ? (string)$cur['title'] : '文章库');
|
||||
?>
|
||||
<style>
|
||||
/* 文章区专用:将外层正文区铺满视口 */
|
||||
html, body { height: 100%; }
|
||||
</style>
|
||||
<div class="art-app js-art-app">
|
||||
<!-- 顶部栏:返回首页 / 当页名(列表为文章库、阅读时显示文章标题)/ 分区·段落开关 -->
|
||||
<header class="art-topbar">
|
||||
<div class="art-topbar-nav">
|
||||
<a class="btn btn-sm" href="../index.php" title="返回站点首页">← 首页</a>
|
||||
<?php if ($cur): ?>
|
||||
<a class="btn btn-sm" href="index.php" title="返回文章区首页(文章列表)">← 文章区首页</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="art-topbar-title"><?= $cur ? he((string)$cur['title']) : '📚 文章库' ?></div>
|
||||
<div class="art-topbar-acts">
|
||||
<button type="button" class="btn btn-sm js-art-toggle" data-col="title" title="显示 / 隐藏左侧文章标题栏">☰<span class="art-tg-txt">标题栏</span></button>
|
||||
<button type="button" class="btn btn-sm js-art-toggle" data-col="toc" title="显示 / 隐藏中间段落目录">≡<span class="art-tg-txt">段落</span></button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<?php if ($noteText !== ''): ?>
|
||||
<div class="art-note"><?= he($noteText) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="art-frame js-art-frame">
|
||||
<!-- 左:文章标题栏(按分区) -->
|
||||
<aside class="art-col art-col-title js-col-title">
|
||||
<div class="art-col-head">
|
||||
<span>文章目录</span>
|
||||
<button type="button" class="art-mini js-art-toggle" data-col="title" title="隐藏标题栏">✕</button>
|
||||
</div>
|
||||
<nav class="art-nav js-art-nav">
|
||||
<?php if (!$rows): ?>
|
||||
<p class="tip" style="padding:10px">暂无文章</p>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($rows as $g): ?>
|
||||
<div class="art-nav-part">
|
||||
<div class="art-nav-part-name"><?= he($g['part']) ?></div>
|
||||
<?php foreach ($g['articles'] as $a): ?>
|
||||
<a class="art-nav-item<?= ($cur && (int)$cur['id'] === (int)$a['id']) ? ' active' : '' ?>"
|
||||
href="?id=<?= (int)$a['id'] ?>" title="<?= he((string)$a['summary']) ?>"><?= he((string)$a['title']) ?></a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
<div class="art-gutter js-gutter" data-var="--w-title"></div>
|
||||
|
||||
<!-- 中:段落目录 -->
|
||||
<aside class="art-col art-col-toc js-col-toc">
|
||||
<div class="art-col-head">
|
||||
<span>段落目录</span>
|
||||
<button type="button" class="art-mini js-art-toggle" data-col="toc" title="隐藏段落栏">✕</button>
|
||||
</div>
|
||||
<nav class="art-toc js-toc-nav"></nav>
|
||||
</aside>
|
||||
|
||||
<div class="art-gutter js-gutter" data-var="--w-toc"></div>
|
||||
|
||||
<!-- 右:正文 / 列表 -->
|
||||
<section class="art-body js-art-body" id="artBody">
|
||||
<?php if ($cur): ?>
|
||||
<?php
|
||||
$md = (string)$cur['markdown'];
|
||||
$updated = (string)($cur['updated_at'] ?? '');
|
||||
$part = (string)$cur['part'];
|
||||
?>
|
||||
<div class="art-article js-article" data-id="<?= (int)$cur['id'] ?>">
|
||||
<div class="art-article-head">
|
||||
<div class="art-article-part"><?= he($part) ?></div>
|
||||
<h1 class="art-article-title js-art-title"><?= he((string)$cur['title']) ?></h1>
|
||||
<?php if (trim((string)($cur['summary'] ?? '')) !== ''): ?>
|
||||
<p class="art-article-sum"><?= he(trim((string)$cur['summary'])) ?></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($updated !== ''): ?>
|
||||
<div class="art-article-meta">更新于 <?= he($updated) ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="art-md js-art-md" id="artMd"></div>
|
||||
<textarea id="artRaw" hidden><?= he($md) ?></textarea>
|
||||
<script type="text/plain" id="artErrTpl"><div class="err-msg">正文渲染失败:无法加载 Markdown 解析库,请检查网络后刷新。</div></script>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="art-list">
|
||||
<div class="art-list-head">
|
||||
<h1>全部文章 <span class="tip">(共 <?= $total ?> 篇)</span></h1>
|
||||
<p class="tip">点击任意文章开始阅读;左侧「文章目录」可随时切换文章,中间「段落目录」可快速跳转。</p>
|
||||
</div>
|
||||
<?php if (!$rows): ?>
|
||||
<div class="panel-card"><p class="tip">文章库还没有内容,站长可在后台「文章管理」中发布。</p></div>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($rows as $g): ?>
|
||||
<div class="art-part-block">
|
||||
<h2 class="art-part-title"><?= he($g['part']) ?></h2>
|
||||
<div class="art-cards">
|
||||
<?php foreach ($g['articles'] as $a): ?>
|
||||
<a class="art-card" href="?id=<?= (int)$a['id'] ?>">
|
||||
<span class="art-card-title"><?= he((string)$a['title']) ?></span>
|
||||
<?php if (trim((string)($a['summary'] ?? '')) !== ''): ?>
|
||||
<span class="art-card-sum"><?= he(trim((string)$a['summary'])) ?></span>
|
||||
<?php endif; ?>
|
||||
<span class="art-card-date"><?= he((string)($a['updated_at'] ?? '')) ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php layout_theme_fab(); ?>
|
||||
<script>
|
||||
window.HP_ART_DATA = {
|
||||
curId: <?= $cur ? (int)$cur['id'] : 0 ?>,
|
||||
listMode: <?= $cur ? 0 : 1 ?>
|
||||
};
|
||||
</script>
|
||||
<script src="../assets/js/common.js"></script>
|
||||
<script src="../assets/js/article.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user