feat: 实现后台系列管理功能
This commit is contained in:
1 parent
592d4e1f6d
commit
2780add6a8
2 files changed
+367
No files matched your search
@@ -0,0 +1,126 @@
|
|||||||
|
import { fail } from '@sveltejs/kit';
|
||||||
|
import type { Actions, PageServerLoad } from './$types';
|
||||||
|
|
||||||
|
function makeSlug(value: string): string {
|
||||||
|
return value
|
||||||
|
.toLowerCase()
|
||||||
|
.trim()
|
||||||
|
.replace(/[^\w\u4e00-\u9fa5\s-]/g, '')
|
||||||
|
.replace(/\s+/g, '-')
|
||||||
|
.replace(/-+/g, '-');
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUniqueConstraintError(err: unknown): boolean {
|
||||||
|
return err instanceof Error && err.message.includes('UNIQUE constraint failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ platform }) => {
|
||||||
|
const db = platform?.env?.DB;
|
||||||
|
if (!db) return { series: [] };
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { results } = await db
|
||||||
|
.prepare(
|
||||||
|
`SELECT s.id, s.name, s.slug, s.description, COUNT(p.id) AS post_count
|
||||||
|
FROM series s
|
||||||
|
LEFT JOIN posts p ON p.series_id = s.id
|
||||||
|
GROUP BY s.id, s.name, s.slug, s.description
|
||||||
|
ORDER BY s.name COLLATE NOCASE ASC`
|
||||||
|
)
|
||||||
|
.all();
|
||||||
|
|
||||||
|
return {
|
||||||
|
series: results as unknown as Array<{
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
slug: string;
|
||||||
|
description: string | null;
|
||||||
|
post_count: number;
|
||||||
|
}>
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching series:', err);
|
||||||
|
return { series: [] };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions: Actions = {
|
||||||
|
create: async ({ request, platform }) => {
|
||||||
|
const db = platform?.env?.DB;
|
||||||
|
if (!db) return fail(500, { error: '数据库未连接' });
|
||||||
|
|
||||||
|
const formData = await request.formData();
|
||||||
|
const name = formData.get('name')?.toString().trim() || '';
|
||||||
|
const slug = makeSlug(formData.get('slug')?.toString() || name);
|
||||||
|
const description = formData.get('description')?.toString().trim() || null;
|
||||||
|
|
||||||
|
if (!name || !slug) return fail(400, { error: '系列名称和 URL 别名不能为空' });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const created = await db
|
||||||
|
.prepare(
|
||||||
|
'INSERT INTO series (name, slug, description) VALUES (?, ?, ?) RETURNING id, name, slug, description'
|
||||||
|
)
|
||||||
|
.bind(name, slug, description)
|
||||||
|
.first();
|
||||||
|
return { success: true, series: created };
|
||||||
|
} catch (err: unknown) {
|
||||||
|
console.error('Error creating series:', err);
|
||||||
|
if (isUniqueConstraintError(err)) {
|
||||||
|
return fail(400, { error: '系列名称或 URL 别名已存在' });
|
||||||
|
}
|
||||||
|
return fail(500, { error: '创建系列失败' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
update: async ({ request, platform }) => {
|
||||||
|
const db = platform?.env?.DB;
|
||||||
|
if (!db) return fail(500, { error: '数据库未连接' });
|
||||||
|
|
||||||
|
const formData = await request.formData();
|
||||||
|
const id = Number(formData.get('id'));
|
||||||
|
const name = formData.get('name')?.toString().trim() || '';
|
||||||
|
const slug = makeSlug(formData.get('slug')?.toString() || '');
|
||||||
|
const description = formData.get('description')?.toString().trim() || null;
|
||||||
|
|
||||||
|
if (!Number.isInteger(id) || id <= 0 || !name || !slug) {
|
||||||
|
return fail(400, { error: '系列信息不完整' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await db
|
||||||
|
.prepare('UPDATE series SET name = ?, slug = ?, description = ? WHERE id = ?')
|
||||||
|
.bind(name, slug, description, id)
|
||||||
|
.run();
|
||||||
|
return { success: true };
|
||||||
|
} catch (err: unknown) {
|
||||||
|
console.error('Error updating series:', err);
|
||||||
|
if (isUniqueConstraintError(err)) {
|
||||||
|
return fail(400, { error: '系列名称或 URL 别名已存在' });
|
||||||
|
}
|
||||||
|
return fail(500, { error: '更新系列失败' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
delete: async ({ request, platform }) => {
|
||||||
|
const db = platform?.env?.DB;
|
||||||
|
if (!db) return fail(500, { error: '数据库未连接' });
|
||||||
|
|
||||||
|
const formData = await request.formData();
|
||||||
|
const id = Number(formData.get('id'));
|
||||||
|
if (!Number.isInteger(id) || id <= 0) return fail(400, { error: '系列 ID 无效' });
|
||||||
|
|
||||||
|
try {
|
||||||
|
await db.batch([
|
||||||
|
db
|
||||||
|
.prepare('UPDATE posts SET series_id = NULL, series_weight = NULL WHERE series_id = ?')
|
||||||
|
.bind(id),
|
||||||
|
db.prepare('DELETE FROM series WHERE id = ?').bind(id)
|
||||||
|
]);
|
||||||
|
return { success: true };
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error deleting series:', err);
|
||||||
|
return fail(500, { error: '删除系列失败' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,241 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { page } from '$app/stores';
|
||||||
|
import { resolve } from '$app/paths';
|
||||||
|
import type { ActionData, PageData } from './$types';
|
||||||
|
|
||||||
|
let { data, form }: { data: PageData; form: ActionData } = $props();
|
||||||
|
let name = $state('');
|
||||||
|
let slug = $state('');
|
||||||
|
|
||||||
|
function handleNameInput() {
|
||||||
|
if (!slug) {
|
||||||
|
slug = name
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^\w\u4e00-\u9fa5\s-]/g, '')
|
||||||
|
.replace(/\s+/g, '-');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="admin-page-header">
|
||||||
|
<h2>{$page.data.aliases?.series || '系列'}管理</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{#if form?.error}
|
||||||
|
<div class="error-alert">{form.error}</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<div class="series-grid-layout">
|
||||||
|
<section class="create-series-card">
|
||||||
|
<h3>新{$page.data.aliases?.series || '系列'}</h3>
|
||||||
|
<form method="POST" action="?/create">
|
||||||
|
<label for="name">名称 *</label>
|
||||||
|
<input
|
||||||
|
id="name"
|
||||||
|
name="name"
|
||||||
|
type="text"
|
||||||
|
bind:value={name}
|
||||||
|
oninput={handleNameInput}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label for="slug">URL 别名 *</label>
|
||||||
|
<input id="slug" name="slug" type="text" bind:value={slug} required />
|
||||||
|
|
||||||
|
<label for="description">描述</label>
|
||||||
|
<textarea
|
||||||
|
id="description"
|
||||||
|
name="description"
|
||||||
|
rows="5"
|
||||||
|
placeholder="介绍这个系列的主题和阅读顺序"></textarea>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">创建</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="series-list">
|
||||||
|
{#if data.series.length === 0}
|
||||||
|
<div class="empty-state">暂无系列。</div>
|
||||||
|
{:else}
|
||||||
|
{#each data.series as item (item.id)}
|
||||||
|
<article class="series-editor">
|
||||||
|
<form method="POST" action="?/update">
|
||||||
|
<input type="hidden" name="id" value={item.id} />
|
||||||
|
<div class="editor-heading">
|
||||||
|
<strong>{item.name}</strong>
|
||||||
|
<span class="badge">{item.post_count} 篇文章</span>
|
||||||
|
</div>
|
||||||
|
<div class="field-row">
|
||||||
|
<div>
|
||||||
|
<label for="name-{item.id}">名称</label>
|
||||||
|
<input id="name-{item.id}" name="name" type="text" value={item.name} required />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="slug-{item.id}">URL 别名</label>
|
||||||
|
<input id="slug-{item.id}" name="slug" type="text" value={item.slug} required />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<label for="description-{item.id}">描述</label>
|
||||||
|
<textarea
|
||||||
|
id="description-{item.id}"
|
||||||
|
name="description"
|
||||||
|
rows="3"
|
||||||
|
value={item.description || ''}></textarea>
|
||||||
|
<button type="submit" class="action-btn">保存修改</button>
|
||||||
|
<a
|
||||||
|
href={resolve('/series/[slug]', { slug: item.slug })}
|
||||||
|
target="_blank"
|
||||||
|
class="action-btn">查看</a
|
||||||
|
>
|
||||||
|
</form>
|
||||||
|
<form
|
||||||
|
method="POST"
|
||||||
|
action="?/delete"
|
||||||
|
class="delete-form"
|
||||||
|
onsubmit={(event) => {
|
||||||
|
if (!confirm(`确定删除系列“${item.name}”吗?相关文章会保留,但不再属于任何系列。`))
|
||||||
|
event.preventDefault();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<input type="hidden" name="id" value={item.id} />
|
||||||
|
<button type="submit" class="action-btn delete">删除</button>
|
||||||
|
</form>
|
||||||
|
</article>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.admin-page-header {
|
||||||
|
margin-bottom: 25px;
|
||||||
|
border-bottom: 2px solid var(--text-color);
|
||||||
|
padding-bottom: 12px;
|
||||||
|
}
|
||||||
|
.admin-page-header h2 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 2rem;
|
||||||
|
}
|
||||||
|
.error-alert {
|
||||||
|
padding: 12px 16px;
|
||||||
|
border-left: 4px solid #d9534f;
|
||||||
|
color: #d9534f;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
.series-grid-layout {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 300px 1fr;
|
||||||
|
gap: 30px;
|
||||||
|
align-items: start;
|
||||||
|
}
|
||||||
|
.create-series-card h3 {
|
||||||
|
margin: 0 0 20px;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
margin: 18px 0 8px;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
|
}
|
||||||
|
input,
|
||||||
|
textarea {
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 10px 0;
|
||||||
|
border: 0;
|
||||||
|
border-bottom: 2px solid rgba(0, 0, 0, 0.12);
|
||||||
|
background: transparent;
|
||||||
|
color: var(--text-color);
|
||||||
|
font: inherit;
|
||||||
|
}
|
||||||
|
textarea {
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid rgba(0, 0, 0, 0.15);
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
input:focus,
|
||||||
|
textarea:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: var(--accent-color, #419b38);
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 20px;
|
||||||
|
padding: 10px 20px;
|
||||||
|
border: 1px solid var(--text-color);
|
||||||
|
background: var(--text-color);
|
||||||
|
color: var(--bg-color);
|
||||||
|
font: inherit;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.btn:hover {
|
||||||
|
background: var(--accent-color, #419b38);
|
||||||
|
border-color: var(--accent-color, #419b38);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.series-list {
|
||||||
|
display: grid;
|
||||||
|
gap: 18px;
|
||||||
|
}
|
||||||
|
.series-editor {
|
||||||
|
position: relative;
|
||||||
|
border: 2px solid var(--text-color);
|
||||||
|
padding: 18px;
|
||||||
|
}
|
||||||
|
.editor-heading {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 16px;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.field-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 1fr;
|
||||||
|
gap: 18px;
|
||||||
|
}
|
||||||
|
.action-btn {
|
||||||
|
display: inline-block;
|
||||||
|
margin: 16px 14px 0 0;
|
||||||
|
padding: 0;
|
||||||
|
border: 0;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--text-color);
|
||||||
|
font: inherit;
|
||||||
|
cursor: pointer;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.action-btn:hover {
|
||||||
|
color: var(--accent-color, #419b38);
|
||||||
|
text-decoration: underline;
|
||||||
|
text-underline-offset: 4px;
|
||||||
|
}
|
||||||
|
.delete-form {
|
||||||
|
position: absolute;
|
||||||
|
right: 4px;
|
||||||
|
bottom: 18px;
|
||||||
|
}
|
||||||
|
.action-btn.delete:hover {
|
||||||
|
color: #d9534f;
|
||||||
|
}
|
||||||
|
.empty-state {
|
||||||
|
border: 2px solid var(--text-color);
|
||||||
|
padding: 50px 20px;
|
||||||
|
text-align: center;
|
||||||
|
color: var(--text-muted);
|
||||||
|
}
|
||||||
|
@media (max-width: 800px) {
|
||||||
|
.series-grid-layout,
|
||||||
|
.field-row {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
.delete-form {
|
||||||
|
position: static;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user