feat: 新增创作中心(/studio),支持数据概览、内容管理、草稿箱、写文章

- 新增 StudioController,依赖 studioUseCase 接口,遵循 ISP 接口隔离
- PostRepo 新增 FindByUserIDAndStatus 方法,按用户+状态分页查询
- PostService 新增 ListByUser、GetOverview 及 StudioOverview 统计
- /studio 页面路由(概览/管理/草稿箱/写文章/数据分析)+ /api/studio API
- 复用 Vditor 编辑器,studio-editor.js 对接 /api/studio/posts 端点
- 新增 studio.css(B站风格三栏布局+统计卡片+状态筛选tabs)
- 模板引擎注册 add/subtract 函数,分页模板中直接使用
This commit is contained in:
2026-05-30 19:55:29 +08:00
parent bacfd4945d
commit 7659aee468
18 changed files with 1781 additions and 2 deletions

View File

@ -0,0 +1,639 @@
/* ============================================================
MetaLab Studio — 创作中心专用样式
============================================================ */
/* ---------- 布局 ---------- */
.studio-wrapper {
display: flex;
min-height: calc(100vh - 120px);
}
/* ---------- 左侧导航 ---------- */
.studio-sidebar {
width: 220px;
min-width: 220px;
background: var(--color-surface);
border-right: 1px solid var(--color-border);
padding: 24px 0;
flex-shrink: 0;
}
.sidebar-brand {
font-size: 18px;
font-weight: 700;
color: var(--color-primary);
padding: 0 24px 20px;
border-bottom: 1px solid var(--color-border);
margin-bottom: 12px;
}
.sidebar-nav {
display: flex;
flex-direction: column;
}
.sidebar-item {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 24px;
font-size: 15px;
color: var(--color-text-light);
transition: var(--transition);
border-left: 3px solid transparent;
}
.sidebar-item:hover {
background: #f0f4ff;
color: var(--color-accent);
}
.sidebar-item.active {
background: #e8f0fe;
color: var(--color-accent);
border-left-color: var(--color-accent);
font-weight: 600;
}
.sidebar-icon {
font-size: 18px;
width: 24px;
text-align: center;
}
/* ---------- 主内容区 ---------- */
.studio-main {
flex: 1;
padding: 32px 40px;
overflow-y: auto;
}
.studio-header {
margin-bottom: 28px;
}
.studio-header h1 {
font-size: 24px;
font-weight: 700;
color: var(--color-primary);
margin-bottom: 6px;
}
.studio-subtitle {
font-size: 14px;
color: var(--color-secondary);
}
.header-counts {
display: flex;
gap: 16px;
margin-top: 8px;
}
.header-count {
font-size: 13px;
padding: 2px 10px;
border-radius: 12px;
background: #f0f0f0;
color: var(--color-text-light);
}
.header-count.approved { background: #e8f5e9; color: #2e7d32; }
.header-count.pending { background: #fff3e0; color: #e65100; }
.header-count.draft { background: #e3f2fd; color: #1565c0; }
.header-count.rejected { background: #ffebee; color: #c62828; }
/* ---------- 统计卡片 ---------- */
.stat-cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 16px;
margin-bottom: 28px;
}
.stat-card {
background: var(--color-surface);
border-radius: var(--radius);
padding: 20px;
box-shadow: var(--shadow);
display: flex;
align-items: center;
gap: 16px;
transition: var(--transition);
}
.stat-card:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0,0,0,.12);
}
.stat-card-icon {
font-size: 32px;
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
background: #f5f5f5;
border-radius: 12px;
}
.stat-card-body {
display: flex;
flex-direction: column;
}
.stat-card-value {
font-size: 28px;
font-weight: 700;
color: var(--color-primary);
line-height: 1.2;
}
.stat-card-label {
font-size: 13px;
color: var(--color-secondary);
margin-top: 2px;
}
.stat-card-approved .stat-card-icon { background: #e8f5e9; }
.stat-card-approved .stat-card-value { color: #2e7d32; }
.stat-card-pending .stat-card-icon { background: #fff3e0; }
.stat-card-pending .stat-card-value { color: #e65100; }
.stat-card-draft .stat-card-icon { background: #e3f2fd; }
.stat-card-draft .stat-card-value { color: #1565c0; }
.stat-card-rejected .stat-card-icon { background: #ffebee; }
.stat-card-rejected .stat-card-value { color: #c62828; }
/* ---------- 通用区块 ---------- */
.studio-section {
background: var(--color-surface);
border-radius: var(--radius);
padding: 24px;
box-shadow: var(--shadow);
margin-bottom: 20px;
}
.section-title {
font-size: 16px;
font-weight: 600;
color: var(--color-primary);
margin-bottom: 16px;
padding-bottom: 12px;
border-bottom: 1px solid var(--color-border);
}
/* ---------- 状态条 ---------- */
.status-bar {
display: flex;
height: 32px;
border-radius: 6px;
overflow: hidden;
gap: 2px;
background: #eee;
}
.status-bar-segment {
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: 600;
color: #fff;
min-width: 80px;
}
.status-bar-approved { background: #4caf50; }
.status-bar-draft { background: #42a5f5; }
.status-bar-pending { background: #ff9800; }
.status-bar-rejected { background: #ef5350; }
/* ---------- 快捷操作 ---------- */
.quick-actions {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.quick-action-btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 10px 20px;
border-radius: 8px;
background: #f5f5f5;
color: var(--color-text);
font-size: 14px;
transition: var(--transition);
border: 1px solid var(--color-border);
}
.quick-action-btn:hover {
background: #e8f0fe;
border-color: var(--color-accent);
color: var(--color-accent);
}
.quick-action-btn.primary {
background: var(--color-accent);
color: #fff;
border-color: var(--color-accent);
}
.quick-action-btn.primary:hover {
background: #2980b9;
}
/* ---------- 状态筛选 Tabs ---------- */
.status-tabs {
display: flex;
gap: 0;
margin-bottom: 20px;
background: var(--color-surface);
border-radius: var(--radius);
box-shadow: var(--shadow);
overflow: hidden;
}
.status-tab {
padding: 10px 20px;
font-size: 14px;
color: var(--color-text-light);
transition: var(--transition);
border-bottom: 2px solid transparent;
}
.status-tab:hover {
color: var(--color-accent);
background: #f8f9fa;
}
.status-tab.active {
color: var(--color-accent);
font-weight: 600;
border-bottom-color: var(--color-accent);
background: #e8f0fe;
}
/* ---------- 帖子列表 ---------- */
.post-list {
background: var(--color-surface);
border-radius: var(--radius);
box-shadow: var(--shadow);
overflow: hidden;
}
.post-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid var(--color-border);
transition: var(--transition);
}
.post-item:last-child {
border-bottom: none;
}
.post-item:hover {
background: #f8f9fa;
}
.post-item-main {
flex: 1;
min-width: 0;
}
.post-item-title {
font-size: 15px;
font-weight: 600;
color: var(--color-primary);
display: block;
margin-bottom: 6px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.post-item-title:hover {
color: var(--color-accent);
}
.draft-title {
color: var(--color-secondary);
}
.post-item-meta {
display: flex;
align-items: center;
gap: 12px;
font-size: 13px;
color: var(--color-secondary);
}
.post-status-badge {
display: inline-block;
padding: 1px 8px;
border-radius: 10px;
font-size: 12px;
font-weight: 600;
}
.status-approved { background: #e8f5e9; color: #2e7d32; }
.status-pending { background: #fff3e0; color: #e65100; }
.status-draft { background: #e3f2fd; color: #1565c0; }
.status-rejected { background: #ffebee; color: #c62828; }
.status-locked { background: #f3e5f5; color: #6a1b9a; }
.post-reject-hint {
color: #c62828;
font-style: italic;
}
.post-item-actions {
display: flex;
gap: 8px;
flex-shrink: 0;
margin-left: 16px;
}
.action-btn {
padding: 6px 14px;
border-radius: 6px;
font-size: 13px;
border: 1px solid var(--color-border);
background: #f5f5f5;
color: var(--color-text-light);
cursor: pointer;
transition: var(--transition);
white-space: nowrap;
}
.action-btn:hover {
background: #e8f0fe;
border-color: var(--color-accent);
color: var(--color-accent);
}
.action-btn.primary {
background: var(--color-accent);
color: #fff;
border-color: var(--color-accent);
}
.action-btn.primary:hover {
background: #2980b9;
}
.action-btn.danger {
color: var(--color-danger);
border-color: #ffcdd2;
}
.action-btn.danger:hover {
background: #ffebee;
border-color: var(--color-danger);
}
/* ---------- 分页 ---------- */
.pagination {
display: flex;
align-items: center;
justify-content: center;
gap: 16px;
padding: 20px;
}
.page-btn {
padding: 6px 16px;
border-radius: 6px;
border: 1px solid var(--color-border);
background: var(--color-surface);
color: var(--color-text);
font-size: 14px;
transition: var(--transition);
}
.page-btn:hover {
background: #e8f0fe;
border-color: var(--color-accent);
color: var(--color-accent);
}
.page-info {
font-size: 14px;
color: var(--color-secondary);
}
/* ---------- 空状态 ---------- */
.studio-empty {
text-align: center;
padding: 60px 20px;
background: var(--color-surface);
border-radius: var(--radius);
box-shadow: var(--shadow);
}
.studio-empty p {
font-size: 16px;
color: var(--color-secondary);
margin-bottom: 20px;
}
.btn-primary {
display: inline-block;
padding: 10px 24px;
background: var(--color-accent);
color: #fff;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
transition: var(--transition);
}
.btn-primary:hover {
background: #2980b9;
color: #fff;
}
/* ---------- 占位提示 ---------- */
.studio-placeholder {
text-align: center;
padding: 40px;
color: var(--color-secondary);
font-size: 15px;
}
/* ---------- 错误提示 ---------- */
.studio-error {
background: #ffebee;
color: #c62828;
padding: 16px 20px;
border-radius: var(--radius);
margin-bottom: 20px;
font-size: 14px;
}
/* ============================================================
编辑器样式(复用自 posts/new.html 的编辑器布局)
============================================================ */
.editor-layout {
width: 100%;
}
.editor-form {
max-width: 100%;
}
.editor-header {
margin-bottom: 16px;
}
.editor-title-input {
width: 100%;
padding: 14px 0;
border: none;
border-bottom: 2px solid var(--color-border);
font-size: 22px;
font-weight: 600;
outline: none;
background: transparent;
color: var(--color-primary);
transition: var(--transition);
}
.editor-title-input:focus {
border-bottom-color: var(--color-accent);
}
.editor-title-input::placeholder {
color: #ccc;
}
.editor-main {
display: flex;
gap: 20px;
margin-bottom: 16px;
}
.editor-pane {
flex: 1;
min-width: 0;
}
.editor-sidebar {
width: 200px;
flex-shrink: 0;
}
.editor-sidebar-section {
background: var(--color-surface);
border-radius: var(--radius);
padding: 16px;
box-shadow: var(--shadow);
margin-bottom: 12px;
}
.editor-sidebar-section h4 {
font-size: 13px;
font-weight: 600;
color: var(--color-primary);
margin-bottom: 10px;
padding-bottom: 8px;
border-bottom: 1px solid var(--color-border);
}
.tips-list {
list-style: none;
font-size: 12px;
color: var(--color-secondary);
line-height: 2;
}
.tips-list code {
background: #f0f0f0;
padding: 1px 5px;
border-radius: 3px;
font-size: 11px;
}
.editor-statusbar {
display: flex;
align-items: center;
padding: 12px 0;
border-top: 1px solid var(--color-border);
gap: 16px;
}
.status-item {
font-size: 13px;
color: var(--color-secondary);
}
.status-spacer {
flex: 1;
}
.btn-submit {
padding: 8px 24px;
}
/* ---------- 响应式 ---------- */
@media (max-width: 768px) {
.studio-wrapper {
flex-direction: column;
}
.studio-sidebar {
width: 100%;
min-width: 100%;
border-right: none;
border-bottom: 1px solid var(--color-border);
padding: 12px 0;
}
.sidebar-nav {
flex-direction: row;
overflow-x: auto;
padding: 0 12px;
}
.sidebar-item {
padding: 8px 16px;
border-left: none;
border-bottom: 2px solid transparent;
white-space: nowrap;
}
.sidebar-item.active {
border-left: none;
border-bottom-color: var(--color-accent);
}
.stat-cards {
grid-template-columns: repeat(2, 1fr);
}
.post-item {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.post-item-actions {
margin-left: 0;
}
.editor-main {
flex-direction: column;
}
.editor-sidebar {
width: 100%;
}
}

View File

@ -0,0 +1,209 @@
/**
* Studio Editor — 创作中心 Vditor 编辑器
* 基于 editor.js使用 /api/studio/posts 端点
*/
let vditor = null
let draftTimer = null
let submitting = false
function getCsrfToken() {
var match = document.cookie.match(/(?:^|;\s*)mlb_csrf=([^;]*)/)
return match ? match[1] : ''
}
document.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('vditor')
if (!container) return
const postId = document.getElementById('postId')?.value
const isEdit = !!postId
let initialMD = ''
const editBodyEl = document.getElementById('editBody')
if (editBodyEl) {
initialMD = editBodyEl.value
}
vditor = new Vditor('vditor', {
mode: 'wysiwyg',
cdn: '/static/vditor',
height: '100%',
lang: 'zh_CN',
placeholder: '在此输入正文...',
toolbar: [
'headings', 'bold', 'italic', 'strike', '|',
'line', 'code', 'inline-code', 'link', 'quote', '|',
'list', 'ordered-list', 'check', 'outdent', 'indent', '|',
'upload', 'table', '|',
'undo', 'redo', '|',
'fullscreen', 'code-theme', '|',
'outline', 'preview', 'devtools',
],
customWysiwygToolbar(type, popover) {},
counter: { enable: true, type: 'text' },
hint: {
extend: [
{ key: '[zone:event:', value: '[zone:event:活动ID]' },
{ key: '[zone:game:', value: '[zone:game:游戏Slug]' },
{ key: '[zone:poll:', value: '[zone:poll:投票ID]' },
{ key: '[zone:resource:', value: '[zone:resource:资源ID]' },
],
},
outline: { enable: false, position: 'right' },
preview: {
theme: { current: 'light', path: '/static/vditor/dist/css/content-theme' },
hljs: {
style: 'github-dark',
enable: true,
langs: [
'javascript', 'typescript', 'python', 'java', 'go', 'rust',
'c', 'cpp', 'csharp', 'php', 'ruby', 'swift', 'kotlin',
'html', 'css', 'scss', 'xml', 'json', 'yaml', 'markdown',
'sql', 'bash', 'shell', 'powershell', 'dockerfile', 'nginx',
'diff', 'http', 'graphql', 'makefile',
],
},
markdown: { codeBlockPreview: true },
},
upload: {
url: '/api/posts/upload-image',
fieldName: 'file',
max: 5 * 1024 * 1024,
accept: 'image/jpg,image/jpeg,image/png,image/gif,image/webp',
},
value: initialMD,
after() {
if (!isEdit) {
setTimeout(() => restoreDraft(), 100)
}
updateWordCount()
},
input(value) {
scheduleDraft(value)
updateWordCount()
},
})
document.getElementById('postForm')?.addEventListener('submit', handleSubmit)
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault()
document.getElementById('postForm')?.dispatchEvent(new Event('submit'))
}
})
})
function updateWordCount() {
const el = document.getElementById('wordCount')
if (!el || !vditor) return
const counterEl = document.querySelector('.vditor-counter')
if (counterEl) {
el.textContent = counterEl.textContent
}
}
function scheduleDraft(md) {
clearTimeout(draftTimer)
draftTimer = setTimeout(() => saveDraft(md), 2000)
}
function saveDraft(md) {
const title = document.getElementById('postTitle')?.value || ''
if (!md.trim() && !title.trim()) return
try {
localStorage.setItem('draft_post_title', title)
localStorage.setItem('draft_post_body_md', md)
const statusEl = document.getElementById('draftStatus')
if (statusEl) {
statusEl.style.display = ''
setTimeout(() => { statusEl.style.display = 'none' }, 2000)
}
} catch (e) { /* ignore */ }
}
function restoreDraft() {
try {
const title = localStorage.getItem('draft_post_title')
const md = localStorage.getItem('draft_post_body_md')
if (title) document.getElementById('postTitle').value = title
if (md && vditor) {
vditor.setValue(md)
updateWordCount()
}
} catch (e) { /* ignore */ }
}
function clearDraft() {
try {
localStorage.removeItem('draft_post_title')
localStorage.removeItem('draft_post_body_md')
} catch (e) { /* ignore */ }
}
async function handleSubmit(e) {
e.preventDefault()
if (submitting) return
submitting = true
const submitBtn = document.querySelector('.btn-submit')
if (submitBtn) {
submitBtn.disabled = true
submitBtn.textContent = '提交中...'
}
try {
const postId = document.getElementById('postId')?.value
const title = document.getElementById('postTitle')?.value.trim()
if (!title) { alert('请输入标题'); return }
const body = vditor ? vditor.getValue() : ''
if (!body.trim()) { alert('请输入正文'); return }
const isEdit = !!postId
const url = isEdit ? '/api/studio/posts/' + postId : '/api/studio/posts'
const method = isEdit ? 'PUT' : 'POST'
const resp = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getCsrfToken(),
},
body: JSON.stringify({ title, body }),
})
const data = await resp.json()
if (data.success) {
clearDraft()
const newPostId = data.data?.id || data.data?.ID || postId
if (newPostId) {
window.location.href = '/posts/' + newPostId
} else {
window.location.href = '/studio/posts'
}
} else {
alert(data.message || '操作失败')
}
} catch (err) {
alert('请求失败,请重试')
} finally {
submitting = false
if (submitBtn) {
submitBtn.disabled = false
const isEdit = !!document.getElementById('postId')?.value
submitBtn.textContent = isEdit ? '保存修改' : '发布帖子'
}
}
}