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

@ -39,6 +39,10 @@
审核管理
</a>
{{end}}
<a href="/admin/posts" class="sidebar-item {{if eq .CurrentPath "/admin/posts"}}active{{end}}">
<span class="sidebar-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg></span>
内容管理
</a>
{{if .CanManageSettings}}
<a href="/admin/site-settings" class="sidebar-item {{if eq .CurrentPath "/admin/site-settings"}}active{{end}}">
<span class="sidebar-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg></span>

View File

@ -0,0 +1,83 @@
{{template "admin/layout/base.html" .}}
<div class="page-header">
<h1>内容管理</h1>
<p class="page-desc">管理所有社区帖子</p>
</div>
<div class="admin-filter-bar">
<form class="filter-form" method="GET" action="/admin/posts">
<input type="text" name="keyword" value="{{.Keyword}}" placeholder="搜索标题..." class="form-input filter-input">
<select name="status" class="form-select filter-select">
<option value="">全部状态</option>
<option value="draft" {{if eq .Status "draft"}}selected{{end}}>草稿</option>
<option value="pending" {{if eq .Status "pending"}}selected{{end}}>待审核</option>
<option value="approved" {{if eq .Status "approved"}}selected{{end}}>已发布</option>
<option value="rejected" {{if eq .Status "rejected"}}selected{{end}}>已退回</option>
<option value="locked" {{if eq .Status "locked"}}selected{{end}}>已锁定</option>
</select>
<button type="submit" class="btn btn-primary">搜索</button>
</form>
</div>
{{if .Error}}
<div class="error-message">{{.Error}}</div>
{{end}}
<table class="admin-table">
<thead>
<tr>
<th>ID</th>
<th>标题</th>
<th>作者</th>
<th>状态</th>
<th>时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{if eq (len .Posts) 0}}
<tr><td colspan="6" class="empty">暂无帖子</td></tr>
{{end}}
{{range .Posts}}
<tr class="post-row">
<td>{{.ID}}</td>
<td><a href="/posts/{{.ID}}" target="_blank" class="post-link">{{.Title}}</a></td>
<td>{{.AuthorName}}</td>
<td><span class="status-badge status-{{.Status}}">{{.Status}}</span></td>
<td>{{.CreatedAt.Format "2006-01-02 15:04"}}</td>
<td class="actions-cell">
<div class="action-btns">
{{if eq .Status "pending"}}
<button class="btn btn-sm btn-success post-approve" data-id="{{.ID}}">通过</button>
<button class="btn btn-sm btn-warning post-reject" data-id="{{.ID}}">退回</button>
{{end}}
{{if eq .Status "approved"}}
<button class="btn btn-sm btn-warning post-reject" data-id="{{.ID}}">退回</button>
{{end}}
{{if and (ne .Status "locked") (ne .Status "pending")}}
<button class="btn btn-sm btn-danger post-lock" data-id="{{.ID}}">锁定</button>
{{end}}
{{if eq .Status "locked"}}
<button class="btn btn-sm btn-success post-unlock" data-id="{{.ID}}">解锁</button>
{{end}}
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{if gt .TotalPages 1}}
<div class="pagination">
{{if gt .Page 1}}
<a href="?page={{.PrevPage}}{{if .Keyword}}&keyword={{.Keyword}}{{end}}{{if .Status}}&status={{.Status}}{{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}}{{if .Status}}&status={{.Status}}{{end}}" class="page-link">下一页</a>
{{end}}
</div>
{{end}}
<script src="/admin/static/js/posts.js"></script>
{{template "admin/layout/footer.html" .}}