feat: 实现内容系统 MVC(Post CRUD + 审核流转)

按 content-system.md 设计文档,实现帖子内容系统的最小可行 MVC。

新增:
- Model: Post 模型,含 5 种状态(draft/pending/approved/rejected/locked)
- Repository: post_repo.go,分页查询(前台 approved 列表 + 后台全状态筛选)
- Service: post_service.go,核心业务逻辑(状态流转、Markdown 渲染 Goldmark)
- Controller: post_controller + admin_post_controller,SSR 页面 6 个 + API 13 个
- Template: 列表/详情/编辑器/404 + 管理员列表,各配 CSS/JS
- 路由: /posts, /posts/:id, /posts/new, /posts/:id/edit, REST API, Admin API
- gomod: + github.com/yuin/goldmark v1.8.2

修改:
- Main: AutoMigrate Post 表
- Router: deps 注册 PostService/Controller,路由注册
- Nav: 新增“帖子”导航链接
- Admin Sidebar: 新增“内容管理”入口

设计:
- 审核开关复用 audit.enabled,开启时帖子为 draft,关闭后直接 approved
- 仅 approved 公开可见,作者/admin 可预览非公开状态帖子
- 严格分层 ISP 接口: Controller → postUseCase, Service → postStore, Repo → *PostRepo
- 状态保护: pending/locked 禁止编辑,rejected 编辑后自动重置为 draft
This commit is contained in:
2026-05-27 18:22:48 +08:00
parent fc65872882
commit 03a01a09be
26 changed files with 1803 additions and 3 deletions

View File

@ -6,6 +6,7 @@
</button>
<ul class="nav-links" id="navLinks">
<li><a href="/">首页</a></li>
<li><a href="/posts">帖子</a></li>
{{if .IsLoggedIn}}
<li>
<a href="/messages" class="nav-messages" id="messagesBell" title="消息中心">

View File

@ -0,0 +1,14 @@
{{template "layout/header.html" .}}
<body>
{{template "layout/nav.html" .}}
<div class="container">
<div class="error-404">
<h1>404</h1>
<p>帖子不存在或已被删除</p>
<a href="/posts" class="btn btn-primary">返回帖子列表</a>
</div>
</div>
{{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script>
</body>
</html>

View File

@ -0,0 +1,58 @@
{{template "layout/header.html" .}}
<body>
{{template "layout/nav.html" .}}
<div class="container posts-container">
<div class="posts-header">
<h1>社区帖子</h1>
{{if .IsLoggedIn}}
<a href="/posts/new" class="btn btn-primary">撰写帖子</a>
{{end}}
</div>
{{if .Error}}
<div class="error-message">{{.Error}}</div>
{{end}}
{{if .Keyword}}
<div class="search-hint">搜索:<strong>{{.Keyword}}</strong></div>
{{end}}
<div class="posts-list">
{{if eq (len .Posts) 0}}
<div class="empty-state">暂无帖子</div>
{{else}}
{{range .Posts}}
<article class="post-card">
<h2 class="post-card-title">
<a href="/posts/{{.ID}}">{{.Title}}</a>
</h2>
<div class="post-card-meta">
<span class="post-author">{{.AuthorName}}</span>
<span class="post-time">{{.CreatedAt.Format "2006-01-02 15:04"}}</span>
</div>
<p class="post-card-summary">{{printf "%.200s" .Body}}</p>
</article>
{{end}}
{{end}}
</div>
{{if gt .TotalPages 1}}
<div class="pagination">
{{if gt .Page 1}}
<a href="?page={{.PrevPage}}{{if .Keyword}}&keyword={{.Keyword}}{{end}}" class="page-link">上一页</a>
{{end}}
<span class="page-info">第 {{.Page}} / {{.TotalPages}} 页 (共 {{.Total}} 条)</span>
{{if lt .Page .TotalPages}}
<a href="?page={{.NextPage}}{{if .Keyword}}&keyword={{.Keyword}}{{end}}" class="page-link">下一页</a>
{{end}}
</div>
{{end}}
</div>
{{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script>
</body>
</html>

View File

@ -0,0 +1,39 @@
{{template "layout/header.html" .}}
<body>
{{template "layout/nav.html" .}}
<div class="container editor-container">
<h1 class="editor-title">{{if .Post}}编辑帖子{{else}}撰写帖子{{end}}</h1>
<form id="postForm" class="post-form">
<input type="hidden" id="postId" value="{{if .Post}}{{.Post.ID}}{{end}}">
<div class="form-group">
<label for="postTitle">标题</label>
<input type="text" id="postTitle" class="form-input" value="{{if .Post}}{{.Post.Title}}{{end}}" maxlength="200" required>
</div>
<div class="form-group">
<label for="postBody">正文 (Markdown)</label>
<textarea id="postBody" class="form-textarea" rows="20" required>{{if .Post}}{{.Post.Body}}{{end}}</textarea>
</div>
<div class="editor-actions">
<button type="button" id="previewBtn" class="btn btn-secondary">预览</button>
<button type="submit" class="btn btn-primary">{{if .Post}}保存修改{{else}}发布帖子{{end}}</button>
</div>
</form>
<div id="previewArea" class="post-preview" style="display:none;">
<h3>预览</h3>
<div id="previewContent" class="post-detail-body"></div>
</div>
</div>
{{template "layout/footer.html" .}}
{{if .ExtraJS}}<script src="{{.ExtraJS}}"></script>{{end}}
<script src="/static/js/common.js"></script>
</body>
</html>

View File

@ -0,0 +1,50 @@
{{template "layout/header.html" .}}
<body>
{{template "layout/nav.html" .}}
<div class="container post-detail">
<article class="post-article">
<header class="post-detail-header">
<h1 class="post-detail-title">{{.Post.Title}}</h1>
<div class="post-detail-meta">
<span class="post-author">{{.Post.AuthorName}}</span>
<span class="post-time">{{.Post.CreatedAt.Format "2006-01-02 15:04"}}</span>
{{if ne .Post.Status "approved"}}
<span class="post-status post-status-{{.Post.Status}}">{{printf "%s" .Post.Status}}</span>
{{end}}
</div>
</header>
<div class="post-detail-body">
{{.Post.BodyHTML}}
{{/* 如果 BodyHTML 为空,直接显示原文 */}}
</div>
{{if not .Post.BodyHTML}}
<div class="post-detail-body-raw">{{.Post.Body}}</div>
{{end}}
{{if .Post.RejectReason}}
<div class="post-reject-reason">
<strong>退回理由:</strong>{{.Post.RejectReason}}
</div>
{{end}}
</article>
{{if .IsLoggedIn}}
<div class="post-actions">
{{if and $.Post (or (eq $.Post.UserID $.UID) $.CanAccessAdmin) (ne $.Post.Status "pending") (ne $.Post.Status "locked")}}
<a href="/posts/{{$.Post.ID}}/edit" class="btn btn-secondary">编辑</a>
{{end}}
{{if $.CanAccessAdmin}}
<a href="/admin/posts" class="btn btn-secondary">管理</a>
{{end}}
</div>
{{end}}
</div>
{{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script>
</body>
</html>

View File

@ -0,0 +1,327 @@
/* ========== 帖子列表 ========== */
.posts-container {
max-width: 960px;
margin: 0 auto;
padding: 24px 16px;
}
.posts-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 24px;
}
.posts-header h1 {
font-size: 28px;
font-weight: 700;
color: #111;
margin: 0;
}
.post-card {
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 10px;
padding: 20px 24px;
margin-bottom: 16px;
transition: box-shadow 0.2s;
}
.post-card:hover {
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.post-card-title a {
font-size: 20px;
font-weight: 600;
color: #111;
text-decoration: none;
}
.post-card-title a:hover {
color: #6366f1;
}
.post-card-meta {
display: flex;
gap: 16px;
margin-top: 8px;
font-size: 13px;
color: #6b7280;
}
.post-card-summary {
margin-top: 12px;
color: #4b5563;
font-size: 14px;
line-height: 1.6;
}
/* ========== 帖子详情 ========== */
.post-detail {
max-width: 800px;
margin: 0 auto;
padding: 32px 16px;
}
.post-detail-header {
margin-bottom: 32px;
padding-bottom: 20px;
border-bottom: 1px solid #e5e7eb;
}
.post-detail-title {
font-size: 32px;
font-weight: 700;
color: #111;
margin: 0 0 12px 0;
line-height: 1.3;
}
.post-detail-meta {
display: flex;
gap: 16px;
align-items: center;
font-size: 14px;
color: #6b7280;
}
.post-detail-body {
font-size: 16px;
line-height: 1.8;
color: #1f2937;
}
.post-detail-body h1,
.post-detail-body h2,
.post-detail-body h3 {
margin-top: 24px;
margin-bottom: 12px;
}
.post-detail-body p {
margin-bottom: 12px;
}
.post-detail-body pre {
background: #f3f4f6;
padding: 16px;
border-radius: 8px;
overflow-x: auto;
font-size: 14px;
line-height: 1.5;
}
.post-detail-body code {
background: #f3f4f6;
padding: 2px 6px;
border-radius: 4px;
font-size: 0.9em;
}
.post-detail-body img {
max-width: 100%;
height: auto;
border-radius: 8px;
}
.post-detail-body blockquote {
border-left: 4px solid #6366f1;
padding-left: 16px;
color: #6b7280;
margin: 16px 0;
}
.post-status {
padding: 2px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.post-status-draft { background: #f3f4f6; color: #6b7280; }
.post-status-pending { background: #fef3c7; color: #d97706; }
.post-status-approved { background: #d1fae5; color: #059669; }
.post-status-rejected { background: #fef2f2; color: #dc2626; }
.post-status-locked { background: #fee2e2; color: #b91c1c; }
.post-reject-reason {
margin-top: 20px;
padding: 12px 16px;
background: #fef2f2;
border: 1px solid #fecaca;
border-radius: 8px;
color: #b91c1c;
font-size: 14px;
}
.post-actions {
margin-top: 24px;
display: flex;
gap: 12px;
}
/* ========== 编辑器 ========== */
.editor-container {
max-width: 900px;
margin: 0 auto;
padding: 32px 16px;
}
.editor-title {
font-size: 28px;
font-weight: 700;
margin-bottom: 24px;
}
.post-form .form-group {
margin-bottom: 20px;
}
.post-form label {
display: block;
font-size: 14px;
font-weight: 500;
color: #374151;
margin-bottom: 6px;
}
.form-input {
width: 100%;
padding: 10px 14px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 16px;
box-sizing: border-box;
}
.form-textarea {
width: 100%;
padding: 12px 14px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 15px;
font-family: 'Menlo', 'Consolas', monospace;
line-height: 1.6;
resize: vertical;
box-sizing: border-box;
}
.editor-actions {
display: flex;
gap: 12px;
margin-top: 16px;
}
.post-preview {
margin-top: 32px;
padding: 24px;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 10px;
}
.post-preview h3 {
margin-top: 0;
color: #6b7280;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
/* ========== 404 ========== */
.error-404 {
text-align: center;
padding: 80px 0;
}
.error-404 h1 {
font-size: 72px;
color: #d1d5db;
margin: 0;
}
.error-404 p {
font-size: 18px;
color: #6b7280;
margin: 12px 0 24px;
}
/* ========== 空状态 ========== */
.empty-state {
text-align: center;
padding: 60px 0;
color: #9ca3af;
font-size: 16px;
}
.search-hint {
margin-bottom: 16px;
color: #6b7280;
font-size: 14px;
}
/* ========== 按钮 ========== */
.btn {
display: inline-block;
padding: 10px 20px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
text-decoration: none;
transition: background 0.2s;
}
.btn-primary {
background: #6366f1;
color: #fff;
}
.btn-primary:hover { background: #4f46e5; }
.btn-secondary {
background: #f3f4f6;
color: #374151;
}
.btn-secondary:hover { background: #e5e7eb; }
/* ========== 分页 ========== */
.pagination {
display: flex;
justify-content: center;
align-items: center;
gap: 16px;
margin-top: 32px;
padding-top: 20px;
border-top: 1px solid #e5e7eb;
}
.page-link {
padding: 8px 16px;
border: 1px solid #d1d5db;
border-radius: 8px;
color: #374151;
text-decoration: none;
font-size: 14px;
}
.page-link:hover { background: #f3f4f6; }
.page-info {
font-size: 14px;
color: #6b7280;
}
/* ========== 错误消息 ========== */
.error-message {
padding: 12px 16px;
background: #fef2f2;
border: 1px solid #fecaca;
border-radius: 8px;
color: #b91c1c;
margin-bottom: 16px;
}

View File

@ -0,0 +1,85 @@
(function() {
var form = document.getElementById('postForm');
if (!form) return;
var postId = document.getElementById('postId');
var titleEl = document.getElementById('postTitle');
var bodyEl = document.getElementById('postBody');
var previewBtn = document.getElementById('previewBtn');
var previewArea = document.getElementById('previewArea');
var previewContent = document.getElementById('previewContent');
var isEdit = postId && postId.value;
// 获取 CSRF token
function getCSRFToken() {
var meta = document.querySelector('meta[name="csrf-token"]');
return meta ? meta.getAttribute('content') : '';
}
// 预览
if (previewBtn) {
previewBtn.addEventListener('click', function() {
var body = bodyEl.value.trim();
if (!body) { alert('请输入正文'); return; }
fetch('/api/posts/preview', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCSRFToken()
},
body: JSON.stringify({ body: body })
})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.success) {
previewContent.innerHTML = data.data.html;
previewArea.style.display = 'block';
} else {
alert(data.message || '预览失败');
}
})
.catch(function() { alert('预览请求失败'); });
});
}
// 提交
form.addEventListener('submit', function(e) {
e.preventDefault();
var title = titleEl.value.trim();
var body = bodyEl.value.trim();
if (!title) { alert('请输入标题'); return; }
if (!body) { alert('请输入正文'); return; }
var url = '/api/posts';
var method = 'POST';
if (isEdit) {
url = '/api/posts/' + postId.value;
method = 'PUT';
}
fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCSRFToken()
},
body: JSON.stringify({ title: title, body: body })
})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.success) {
if (data.data && data.data.id) {
window.location.href = '/posts/' + data.data.id;
} else {
window.location.href = '/posts';
}
} else {
alert(data.message || '操作失败');
}
})
.catch(function() { alert('请求失败'); });
});
})();