f1d2a405f9
- 个人中心重设计:对齐站点黑白编辑风(极简资料区+细分隔线统计、下划线页签、 细边框卡片、克制危险区、收藏网格、评论列表),移除后台系组件与彩色渐变 - 后台响应式适配:表格页 md 以下卡片堆叠(md:hidden/hidden md:block 双模板)、 Dashboard 统计卡自适应、表单双语两列、AdminModal 移动端底部弹层、 触摸目标与间距统一,覆盖全部 9 个后台页 + 布局
483 lines
16 KiB
Vue
483 lines
16 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台动态管理:列表(搜索/分类/状态筛选 + 前端分页)+ 新建/编辑表单。
|
||
* - 列表来自 /api/admin/articles(含未发布)
|
||
* - 新建 POST /api/admin/articles;编辑 PATCH /api/admin/articles/[id];删除 DELETE /api/admin/articles/[id]
|
||
* - 正文为内容块编辑器(中英各一组)
|
||
*/
|
||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||
import { useToastStore } from '~/stores/toast'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
useHead({ title: '动态管理' })
|
||
|
||
interface Block {
|
||
type: string
|
||
text: string
|
||
}
|
||
|
||
interface Article {
|
||
id?: number
|
||
slug: string
|
||
category: 'insights' | 'projects' | 'announcements'
|
||
titleZh: string
|
||
titleEn: string
|
||
excerptZh: string
|
||
excerptEn: string
|
||
contentZh: Block[]
|
||
contentEn: Block[]
|
||
image: string | null
|
||
date: string | null
|
||
readTime: number
|
||
author: string
|
||
published: number
|
||
createdAt: string
|
||
updatedAt: string
|
||
}
|
||
|
||
const toast = useToastStore()
|
||
|
||
const CATEGORIES = [
|
||
{ value: 'insights', label: '行业洞察' },
|
||
{ value: 'projects', label: '项目动态' },
|
||
{ value: 'announcements', label: '公告通知' },
|
||
]
|
||
const catLabel = (c: string) => CATEGORIES.find((x) => x.value === c)?.label ?? c
|
||
|
||
/* ---------- 列表 ---------- */
|
||
const articles = ref<Article[]>([])
|
||
const loading = ref(true)
|
||
const error = ref('')
|
||
|
||
const loadArticles = async () => {
|
||
loading.value = true
|
||
error.value = ''
|
||
try {
|
||
const res = await $fetch<{ articles: Article[] }>('/api/admin/articles')
|
||
articles.value = res.articles
|
||
} catch (err: any) {
|
||
error.value = err?.data?.message || '加载失败'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(loadArticles)
|
||
|
||
/* ---------- 搜索 / 筛选 / 分页 ---------- */
|
||
const keyword = ref('')
|
||
const categoryFilter = ref<'all' | Article['category']>('all')
|
||
const statusFilter = ref<'all' | 'published' | 'draft'>('all')
|
||
|
||
const filteredArticles = computed(() => {
|
||
const kw = keyword.value.trim().toLowerCase()
|
||
return articles.value.filter((a) => {
|
||
if (categoryFilter.value !== 'all' && a.category !== categoryFilter.value) return false
|
||
if (statusFilter.value === 'published' && !a.published) return false
|
||
if (statusFilter.value === 'draft' && a.published) return false
|
||
if (!kw) return true
|
||
return (
|
||
a.titleZh.toLowerCase().includes(kw) ||
|
||
a.titleEn.toLowerCase().includes(kw) ||
|
||
a.slug.toLowerCase().includes(kw) ||
|
||
a.author.toLowerCase().includes(kw)
|
||
)
|
||
})
|
||
})
|
||
|
||
const pageSize = 10
|
||
const page = ref(1)
|
||
const totalPages = computed(() => Math.max(1, Math.ceil(filteredArticles.value.length / pageSize)))
|
||
|
||
const pagedArticles = computed(() => {
|
||
const p = Math.min(page.value, totalPages.value)
|
||
const start = (p - 1) * pageSize
|
||
return filteredArticles.value.slice(start, start + pageSize)
|
||
})
|
||
|
||
watch([keyword, categoryFilter, statusFilter], () => {
|
||
page.value = 1
|
||
})
|
||
|
||
const changePage = (p: number) => {
|
||
page.value = Math.min(Math.max(1, p), totalPages.value)
|
||
}
|
||
|
||
const headerSubtitle = computed(() => {
|
||
if (view.value === 'form') return editingId.value != null ? '编辑动态' : '新建动态'
|
||
return `共 ${articles.value.length} 篇动态`
|
||
})
|
||
|
||
/* ---------- 表单 ---------- */
|
||
const view = ref<'list' | 'form'>('list')
|
||
const editingId = ref<number | null>(null)
|
||
const saving = ref(false)
|
||
const formError = ref('')
|
||
|
||
const emptyForm = () => ({
|
||
slug: '',
|
||
category: 'insights' as Article['category'],
|
||
titleZh: '',
|
||
titleEn: '',
|
||
excerptZh: '',
|
||
excerptEn: '',
|
||
contentZh: [] as Block[],
|
||
contentEn: [] as Block[],
|
||
image: '',
|
||
date: '',
|
||
readTime: 5,
|
||
author: 'ArcticNewOne',
|
||
published: true,
|
||
})
|
||
const form = reactive(emptyForm())
|
||
|
||
const openCreate = () => {
|
||
Object.assign(form, emptyForm())
|
||
editingId.value = null
|
||
formError.value = ''
|
||
view.value = 'form'
|
||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||
}
|
||
|
||
const openEdit = (a: Article) => {
|
||
if (a.id == null) {
|
||
toast.error('该条目缺少 id 字段,暂时无法编辑(后端待补)')
|
||
return
|
||
}
|
||
editingId.value = a.id
|
||
Object.assign(form, {
|
||
slug: a.slug,
|
||
category: a.category,
|
||
titleZh: a.titleZh,
|
||
titleEn: a.titleEn,
|
||
excerptZh: a.excerptZh,
|
||
excerptEn: a.excerptEn,
|
||
contentZh: a.contentZh.map((b) => ({ ...b })),
|
||
contentEn: a.contentEn.map((b) => ({ ...b })),
|
||
image: a.image ?? '',
|
||
date: a.date ?? '',
|
||
readTime: a.readTime,
|
||
author: a.author,
|
||
published: !!a.published,
|
||
})
|
||
formError.value = ''
|
||
view.value = 'form'
|
||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||
}
|
||
|
||
const backToList = () => {
|
||
view.value = 'list'
|
||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||
}
|
||
|
||
const save = async () => {
|
||
formError.value = ''
|
||
if (!/^[a-z0-9-]{2,64}$/.test(form.slug)) {
|
||
formError.value = 'slug 需为 2-64 位小写字母/数字/中划线'
|
||
return
|
||
}
|
||
if (!form.titleZh.trim() || !form.titleEn.trim()) {
|
||
formError.value = '中英文标题均为必填'
|
||
return
|
||
}
|
||
saving.value = true
|
||
try {
|
||
const payload = {
|
||
slug: form.slug,
|
||
category: form.category,
|
||
titleZh: form.titleZh,
|
||
titleEn: form.titleEn,
|
||
excerptZh: form.excerptZh,
|
||
excerptEn: form.excerptEn,
|
||
contentZh: form.contentZh,
|
||
contentEn: form.contentEn,
|
||
image: form.image || null,
|
||
date: form.date || null,
|
||
readTime: Number(form.readTime) || 5,
|
||
author: form.author.trim() || 'ArcticNewOne',
|
||
published: form.published,
|
||
}
|
||
if (editingId.value != null) {
|
||
await $fetch(`/api/admin/articles/${editingId.value}`, { method: 'PATCH', body: payload })
|
||
toast.success('动态已更新')
|
||
} else {
|
||
await $fetch('/api/admin/articles', { method: 'POST', body: payload })
|
||
toast.success('动态已创建')
|
||
}
|
||
await loadArticles()
|
||
view.value = 'list'
|
||
} catch (err: any) {
|
||
formError.value = err?.data?.message || '保存失败'
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
/* ---------- 删除 ---------- */
|
||
const confirmDelete = ref<Article | null>(null)
|
||
const deleting = ref(false)
|
||
|
||
const askDelete = (a: Article) => {
|
||
if (a.id == null) {
|
||
toast.error('该条目缺少 id 字段,暂时无法删除(后端待补)')
|
||
return
|
||
}
|
||
confirmDelete.value = a
|
||
}
|
||
|
||
const doDelete = async () => {
|
||
const a = confirmDelete.value
|
||
if (!a || a.id == null) return
|
||
deleting.value = true
|
||
try {
|
||
await $fetch(`/api/admin/articles/${a.id}`, { method: 'DELETE' })
|
||
toast.success(`已删除「${a.titleZh}」`)
|
||
confirmDelete.value = null
|
||
await loadArticles()
|
||
} catch (err: any) {
|
||
toast.error(err?.data?.message || '删除失败')
|
||
} finally {
|
||
deleting.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<!-- 页头 -->
|
||
<AdminPageHeader title="动态管理" :subtitle="headerSubtitle">
|
||
<template #actions>
|
||
<AdminButton v-if="view === 'list'" size="sm" @click="openCreate">
|
||
<Icon name="ph:plus" class="w-4 h-4" /> 新建动态
|
||
</AdminButton>
|
||
<AdminButton v-else size="sm" variant="secondary" @click="backToList">
|
||
<Icon name="ph:arrow-left" class="w-4 h-4" /> 返回列表
|
||
</AdminButton>
|
||
</template>
|
||
</AdminPageHeader>
|
||
|
||
<!-- ==================== 列表视图 ==================== -->
|
||
<template v-if="view === 'list'">
|
||
<!-- 工具栏 -->
|
||
<div class="mb-5 flex flex-col sm:flex-row gap-3">
|
||
<div class="relative flex-1 sm:max-w-xs">
|
||
<Icon name="ph:magnifying-glass" class="w-4 h-4 text-gray-400 absolute left-3.5 top-1/2 -translate-y-1/2 pointer-events-none" />
|
||
<input
|
||
v-model="keyword"
|
||
type="text"
|
||
placeholder="搜索标题 / slug / 作者…"
|
||
class="w-full rounded-xl border border-gray-200 bg-white py-2.5 pl-10 pr-9 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10"
|
||
/>
|
||
<button
|
||
v-if="keyword"
|
||
@click="keyword = ''"
|
||
class="absolute right-2.5 top-1/2 -translate-y-1/2 w-6 h-6 rounded-full text-gray-400 hover:text-gray-600 hover:bg-gray-100 flex items-center justify-center"
|
||
aria-label="清空筛选"
|
||
>
|
||
<Icon name="ph:x" class="w-3.5 h-3.5" />
|
||
</button>
|
||
</div>
|
||
<AdminSelect
|
||
v-model="categoryFilter"
|
||
:options="[
|
||
{ value: 'all', label: '全部分类' },
|
||
...CATEGORIES,
|
||
]"
|
||
class="w-full sm:w-40"
|
||
/>
|
||
<AdminSelect
|
||
v-model="statusFilter"
|
||
:options="[
|
||
{ value: 'all', label: '全部状态' },
|
||
{ value: 'published', label: '已发布' },
|
||
{ value: 'draft', label: '草稿' },
|
||
]"
|
||
class="w-full sm:w-36"
|
||
/>
|
||
</div>
|
||
|
||
<!-- 加载态 -->
|
||
<AdminCard v-if="loading">
|
||
<AdminSkeleton :rows="6" />
|
||
</AdminCard>
|
||
|
||
<!-- 错误态 -->
|
||
<AdminCard v-else-if="error">
|
||
<div class="py-10 text-center">
|
||
<Icon name="ph:warning-circle" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
|
||
<p class="text-gray-500 mb-4">{{ error }}</p>
|
||
<AdminButton size="sm" @click="loadArticles">重新加载</AdminButton>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 空态 -->
|
||
<AdminEmptyState
|
||
v-else-if="!articles.length"
|
||
icon="ph:newspaper"
|
||
title="还没有动态"
|
||
description="点击右上角「新建动态」创建第一篇。"
|
||
/>
|
||
|
||
<AdminEmptyState
|
||
v-else-if="!filteredArticles.length"
|
||
icon="ph:magnifying-glass"
|
||
title="没有匹配的动态"
|
||
description="换个关键词或筛选条件试试。"
|
||
/>
|
||
|
||
<!-- 列表 -->
|
||
<template v-else>
|
||
<div class="rounded-2xl border border-gray-100 bg-white shadow-sm overflow-hidden">
|
||
<div class="overflow-x-auto">
|
||
<table class="w-full text-sm">
|
||
<thead>
|
||
<tr class="text-left text-gray-400 border-b border-gray-100 bg-gray-50">
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">标题</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">分类</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">发布日期</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">时长</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">状态</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider text-right">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody class="divide-y divide-gray-50">
|
||
<tr
|
||
v-for="a in pagedArticles"
|
||
:key="a.slug"
|
||
class="hover:bg-gray-50/60 transition-colors"
|
||
>
|
||
<td class="px-5 py-4">
|
||
<p class="font-semibold text-black">{{ a.titleZh }}</p>
|
||
<p class="text-xs text-gray-400 mt-0.5 font-mono">{{ a.slug }}</p>
|
||
</td>
|
||
<td class="px-5 py-4">
|
||
<AdminBadge tone="gray">{{ catLabel(a.category) }}</AdminBadge>
|
||
</td>
|
||
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ a.date || '-' }}</td>
|
||
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ a.readTime }} 分钟</td>
|
||
<td class="px-5 py-4">
|
||
<AdminBadge :tone="a.published ? 'green' : 'amber'">
|
||
<span
|
||
class="w-1.5 h-1.5 rounded-full inline-block"
|
||
:class="a.published ? 'bg-green-500' : 'bg-amber-500'"
|
||
></span>
|
||
{{ a.published ? '已发布' : '草稿' }}
|
||
</AdminBadge>
|
||
</td>
|
||
<td class="px-5 py-4">
|
||
<div class="flex items-center justify-end gap-2">
|
||
<AdminButton size="sm" variant="secondary" class="min-h-10" @click="openEdit(a)">
|
||
<Icon name="ph:pencil-simple" class="w-3.5 h-3.5" /> 编辑
|
||
</AdminButton>
|
||
<AdminButton size="sm" variant="danger" class="min-h-10" @click="askDelete(a)">
|
||
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
|
||
</AdminButton>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="mt-4">
|
||
<AdminPagination :total="filteredArticles.length" :page="page" :page-size="pageSize" @page="changePage" />
|
||
</div>
|
||
</template>
|
||
</template>
|
||
|
||
<!-- ==================== 表单视图 ==================== -->
|
||
<template v-else>
|
||
<div class="space-y-6 max-w-4xl">
|
||
<!-- 基本信息 -->
|
||
<AdminCard title="基本信息">
|
||
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
|
||
<div>
|
||
<AdminInput
|
||
v-model="form.slug"
|
||
label="slug"
|
||
required
|
||
placeholder="如 design-trends-2026"
|
||
:error="formError && !/^[a-z0-9-]{2,64}$/.test(form.slug) ? 'slug 需为 2-64 位小写字母/数字/中划线' : ''"
|
||
/>
|
||
<p class="mt-1 text-xs text-gray-400">2-64 位小写字母 / 数字 / 中划线,用于前台链接</p>
|
||
</div>
|
||
<AdminSelect
|
||
v-model="form.category"
|
||
label="分类"
|
||
:options="CATEGORIES"
|
||
/>
|
||
<AdminInput v-model="form.date" type="date" label="发布日期" />
|
||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-5">
|
||
<AdminInput
|
||
:model-value="String(form.readTime)"
|
||
type="number"
|
||
label="阅读时长(分钟)"
|
||
@update:model-value="form.readTime = Number($event) || 1"
|
||
/>
|
||
<AdminInput v-model="form.author" label="作者" />
|
||
</div>
|
||
<div class="md:col-span-2">
|
||
<p class="block text-sm font-semibold text-gray-800 mb-1.5">发布状态</p>
|
||
<div class="flex items-center gap-3">
|
||
<AdminToggle v-model="form.published" />
|
||
<span class="text-sm" :class="form.published ? 'text-green-700 font-semibold' : 'text-gray-500'">
|
||
{{ form.published ? '已发布(前台可见)' : '草稿(前台隐藏)' }}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 标题与摘要 -->
|
||
<AdminCard title="标题与摘要">
|
||
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
|
||
<div class="space-y-4">
|
||
<AdminInput v-model="form.titleZh" label="中文标题" required />
|
||
<AdminTextarea v-model="form.excerptZh" label="中文摘要" :rows="3" />
|
||
</div>
|
||
<div class="space-y-4">
|
||
<AdminInput v-model="form.titleEn" label="English Title" required />
|
||
<AdminTextarea v-model="form.excerptEn" label="English Excerpt" :rows="3" />
|
||
</div>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 正文内容 -->
|
||
<AdminCard title="正文内容(中英各一组)">
|
||
<div class="space-y-6">
|
||
<AdminBlockEditor v-model="form.contentZh" label="中文正文" />
|
||
<AdminBlockEditor v-model="form.contentEn" label="English Content" />
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 封面图 -->
|
||
<AdminCard title="封面图">
|
||
<AdminImageUpload v-model="form.image" label="封面图" />
|
||
</AdminCard>
|
||
|
||
<!-- 提交 -->
|
||
<div class="flex flex-col sm:flex-row sm:items-center sm:justify-end gap-3">
|
||
<p v-if="formError" class="text-sm text-red-600 sm:mr-auto">{{ formError }}</p>
|
||
<div class="flex items-center gap-3 w-full sm:w-auto sm:justify-end">
|
||
<AdminButton class="flex-1 sm:flex-none" variant="secondary" @click="backToList">取消</AdminButton>
|
||
<AdminButton class="flex-1 sm:flex-none" :loading="saving" @click="save">
|
||
{{ saving ? '保存中…' : editingId != null ? '保存修改' : '创建动态' }}
|
||
</AdminButton>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<!-- 删除确认 -->
|
||
<AdminConfirm
|
||
:open="!!confirmDelete"
|
||
:loading="deleting"
|
||
danger
|
||
:message="`确定要删除「${confirmDelete?.titleZh}」吗?该动态下的所有评论将一并删除,此操作不可撤销。`"
|
||
@update:open="confirmDelete = null"
|
||
@confirm="doDelete"
|
||
/>
|
||
</div>
|
||
</template>
|