f1d2a405f9
- 个人中心重设计:对齐站点黑白编辑风(极简资料区+细分隔线统计、下划线页签、 细边框卡片、克制危险区、收藏网格、评论列表),移除后台系组件与彩色渐变 - 后台响应式适配:表格页 md 以下卡片堆叠(md:hidden/hidden md:block 双模板)、 Dashboard 统计卡自适应、表单双语两列、AdminModal 移动端底部弹层、 触摸目标与间距统一,覆盖全部 9 个后台页 + 布局
354 lines
12 KiB
Vue
354 lines
12 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台评论管理:后端分页 + 关键词检索(当前页过滤)+ 多选批量删除。
|
||
* - GET /api/admin/comments?page=&pageSize= → { comments, total, page, pageSize };DELETE /api/admin/comments/[id]
|
||
*/
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { useToastStore } from '~/stores/toast'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
useHead({ title: '评论管理' })
|
||
|
||
interface AdminComment {
|
||
id: number
|
||
newsSlug: string
|
||
content: string
|
||
createdAt: string
|
||
username: string
|
||
}
|
||
|
||
const toast = useToastStore()
|
||
|
||
/* ---------- 列表(后端分页) ---------- */
|
||
const pageSize = 15
|
||
const page = ref(1)
|
||
const total = ref(0)
|
||
const comments = ref<AdminComment[]>([])
|
||
const loading = ref(true)
|
||
const loadError = ref('')
|
||
|
||
const loadComments = async () => {
|
||
loading.value = true
|
||
loadError.value = ''
|
||
try {
|
||
const data = await $fetch<{ comments: AdminComment[]; total: number; page: number }>('/api/admin/comments', {
|
||
query: { page: page.value, pageSize },
|
||
})
|
||
comments.value = data.comments
|
||
total.value = data.total
|
||
page.value = data.page
|
||
} catch (err) {
|
||
console.error('load comments error:', err)
|
||
loadError.value = '加载失败,请重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(loadComments)
|
||
|
||
const changePage = (p: number) => {
|
||
if (p === page.value) return
|
||
page.value = p
|
||
loadComments()
|
||
}
|
||
|
||
/* ---------- 关键词(当前页过滤) ---------- */
|
||
const keyword = ref('')
|
||
const filteredComments = computed(() => {
|
||
const kw = keyword.value.trim().toLowerCase()
|
||
if (!kw) return comments.value
|
||
return comments.value.filter(
|
||
(c) => c.newsSlug.toLowerCase().includes(kw) || c.content.toLowerCase().includes(kw) || c.username.toLowerCase().includes(kw),
|
||
)
|
||
})
|
||
|
||
watch(keyword, () => {
|
||
if (page.value !== 1) {
|
||
page.value = 1
|
||
loadComments()
|
||
}
|
||
})
|
||
|
||
/* ---------- 多选 / 批量删除 ---------- */
|
||
const selected = ref<Set<number>>(new Set())
|
||
|
||
const allChecked = computed(
|
||
() => filteredComments.value.length > 0 && filteredComments.value.every((c) => selected.value.has(c.id)),
|
||
)
|
||
|
||
const toggleAll = () => {
|
||
if (allChecked.value) {
|
||
for (const c of filteredComments.value) selected.value.delete(c.id)
|
||
selected.value = new Set(selected.value)
|
||
} else {
|
||
const next = new Set(selected.value)
|
||
for (const c of filteredComments.value) next.add(c.id)
|
||
selected.value = next
|
||
}
|
||
}
|
||
|
||
const toggleOne = (id: number) => {
|
||
const next = new Set(selected.value)
|
||
if (next.has(id)) next.delete(id)
|
||
else next.add(id)
|
||
selected.value = next
|
||
}
|
||
|
||
const batchConfirmOpen = ref(false)
|
||
const batchDeleting = ref(false)
|
||
|
||
const doBatchDelete = async () => {
|
||
const ids = [...selected.value]
|
||
if (!ids.length) return
|
||
batchDeleting.value = true
|
||
let failed = 0
|
||
for (const id of ids) {
|
||
try {
|
||
await $fetch(`/api/admin/comments/${id}`, { method: 'DELETE' })
|
||
} catch (err: any) {
|
||
console.error('batch delete comment error:', err)
|
||
failed++
|
||
}
|
||
}
|
||
batchDeleting.value = false
|
||
batchConfirmOpen.value = false
|
||
selected.value = new Set()
|
||
if (failed === 0) {
|
||
toast.success(`已删除 ${ids.length} 条评论`)
|
||
} else {
|
||
toast.error(`删除 ${ids.length - failed} 条成功,${failed} 条失败`)
|
||
}
|
||
await loadComments()
|
||
}
|
||
|
||
/* ---------- 单条删除 ---------- */
|
||
const confirmDelete = ref<AdminComment | null>(null)
|
||
const deleting = ref(false)
|
||
|
||
const askDelete = (c: AdminComment) => {
|
||
confirmDelete.value = c
|
||
}
|
||
|
||
const doDelete = async () => {
|
||
const c = confirmDelete.value
|
||
if (!c) return
|
||
deleting.value = true
|
||
try {
|
||
await $fetch(`/api/admin/comments/${c.id}`, { method: 'DELETE' })
|
||
toast.success('评论已删除')
|
||
confirmDelete.value = null
|
||
selected.value.delete(c.id)
|
||
await loadComments()
|
||
} catch (err: any) {
|
||
toast.error((err as any)?.data?.message || '删除失败,请重试')
|
||
} finally {
|
||
deleting.value = false
|
||
}
|
||
}
|
||
|
||
const formatDate = (raw?: string) => {
|
||
if (!raw) return ''
|
||
const d = new Date(String(raw).replace(' ', 'T') + 'Z')
|
||
if (Number.isNaN(d.getTime())) return raw
|
||
return new Intl.DateTimeFormat('zh-CN', {
|
||
year: 'numeric',
|
||
month: 'short',
|
||
day: 'numeric',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
}).format(d)
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<AdminPageHeader title="评论管理" subtitle="查看全站文章评论,按文章或内容关键词检索。">
|
||
<template #actions>
|
||
<AdminButton variant="secondary" size="sm" @click="loadComments">
|
||
<Icon name="ph:arrows-clockwise" class="w-4 h-4" /> 刷新
|
||
</AdminButton>
|
||
</template>
|
||
</AdminPageHeader>
|
||
|
||
<!-- 统计 + 工具栏 -->
|
||
<div class="mb-4 flex flex-wrap items-center gap-x-4 gap-y-2">
|
||
<span class="text-sm text-gray-500">共 {{ total }} 条评论</span>
|
||
<span v-if="keyword" class="text-sm text-gray-400">· 当前页匹配 {{ filteredComments.length }} 条</span>
|
||
<span v-if="selected.size" class="text-sm text-gray-400">· 已选 {{ selected.size }} 项</span>
|
||
</div>
|
||
|
||
<div class="mb-5 flex flex-col gap-3 md:flex-row md:items-center">
|
||
<div class="relative flex-1 md: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="按文章或内容关键词筛选…"
|
||
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-5 h-5 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>
|
||
<AdminButton variant="danger" size="sm" class="w-full md:w-auto" :disabled="!selected.size" @click="batchConfirmOpen = true">
|
||
<Icon name="ph:trash" class="w-4 h-4" /> 批量删除
|
||
</AdminButton>
|
||
</div>
|
||
|
||
<!-- 加载态 -->
|
||
<AdminCard v-if="loading">
|
||
<AdminSkeleton :rows="6" />
|
||
</AdminCard>
|
||
|
||
<!-- 错误态 -->
|
||
<AdminCard v-else-if="loadError">
|
||
<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">{{ loadError }}</p>
|
||
<AdminButton size="sm" @click="loadComments">重新加载</AdminButton>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 空态 -->
|
||
<AdminEmptyState v-else-if="!comments.length" icon="ph:chat-circle-text" title="暂无评论" description="还没有人发表评论。" />
|
||
|
||
<AdminEmptyState
|
||
v-else-if="!filteredComments.length"
|
||
icon="ph:magnifying-glass"
|
||
title="当前页没有匹配的评论"
|
||
description="换个关键词,或在其他页试试。"
|
||
/>
|
||
|
||
<!-- 评论表格 -->
|
||
<template v-else>
|
||
<!-- 桌面表格(md+) -->
|
||
<div class="hidden md:block 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 w-10 sticky left-0 bg-gray-50 z-10 border-r border-gray-100">
|
||
<input
|
||
type="checkbox"
|
||
:checked="allChecked"
|
||
class="w-4 h-4 rounded border-gray-300 accent-black cursor-pointer"
|
||
@change="toggleAll"
|
||
/>
|
||
</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 whitespace-nowrap">评论时间</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="c in filteredComments"
|
||
:key="c.id"
|
||
class="hover:bg-gray-50/60 transition-colors"
|
||
:class="selected.has(c.id) ? 'bg-gray-50/70' : ''"
|
||
>
|
||
<td class="px-5 py-4 sticky left-0 bg-white z-10 border-r border-gray-100">
|
||
<input
|
||
type="checkbox"
|
||
:checked="selected.has(c.id)"
|
||
class="w-4 h-4 rounded border-gray-300 accent-black cursor-pointer"
|
||
@change="toggleOne(c.id)"
|
||
/>
|
||
</td>
|
||
<td class="px-5 py-4 whitespace-nowrap">
|
||
<NuxtLink
|
||
:to="`/news/${c.newsSlug}`"
|
||
class="inline-flex items-center gap-1.5 font-semibold text-black hover:text-gray-600 transition-colors"
|
||
>
|
||
<Icon name="ph:article" class="w-4 h-4 text-gray-400" />
|
||
#{{ c.newsSlug }}
|
||
</NuxtLink>
|
||
</td>
|
||
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ c.username }}</td>
|
||
<td class="px-5 py-4">
|
||
<p class="text-gray-700 max-w-[300px] md:max-w-md truncate" :title="c.content">{{ c.content }}</p>
|
||
</td>
|
||
<td class="px-5 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(c.createdAt) }}</td>
|
||
<td class="px-5 py-4">
|
||
<div class="flex items-center justify-end">
|
||
<AdminButton size="sm" variant="ghost" @click="askDelete(c)">
|
||
<Icon name="ph:trash" class="w-4 h-4" />
|
||
</AdminButton>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 移动端卡片(<md) -->
|
||
<div class="md:hidden space-y-3">
|
||
<div
|
||
v-for="c in filteredComments"
|
||
:key="c.id"
|
||
class="bg-white rounded-2xl border border-gray-100 shadow-sm p-4"
|
||
:class="selected.has(c.id) ? 'ring-2 ring-black/5 border-black/10' : ''"
|
||
>
|
||
<div class="flex items-start gap-3">
|
||
<input
|
||
type="checkbox"
|
||
:checked="selected.has(c.id)"
|
||
class="w-5 h-5 mt-0.5 rounded border-gray-300 accent-black cursor-pointer flex-shrink-0"
|
||
@change="toggleOne(c.id)"
|
||
/>
|
||
<div class="min-w-0 flex-1">
|
||
<div class="flex items-center justify-between gap-2">
|
||
<NuxtLink
|
||
:to="`/news/${c.newsSlug}`"
|
||
class="inline-flex items-center gap-1.5 font-semibold text-black min-w-0"
|
||
>
|
||
<Icon name="ph:article" class="w-4 h-4 text-gray-400 flex-shrink-0" />
|
||
<span class="truncate">#{{ c.newsSlug }}</span>
|
||
</NuxtLink>
|
||
<AdminButton size="sm" variant="ghost" class="min-h-10 shrink-0 -my-1" @click="askDelete(c)">
|
||
<Icon name="ph:trash" class="w-4 h-4" />
|
||
</AdminButton>
|
||
</div>
|
||
<p class="mt-1 text-xs text-gray-400">作者:{{ c.username }} · {{ formatDate(c.createdAt) }}</p>
|
||
<p class="mt-2 text-sm text-gray-700 break-words line-clamp-3">{{ c.content }}</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="mt-4">
|
||
<AdminPagination :total="total" :page="page" :page-size="pageSize" @page="changePage" />
|
||
</div>
|
||
</template>
|
||
|
||
<!-- 批量删除确认 -->
|
||
<AdminConfirm
|
||
:open="batchConfirmOpen"
|
||
:loading="batchDeleting"
|
||
danger
|
||
:message="`确定删除已选的 ${selected.size} 条评论吗?此操作不可恢复。`"
|
||
@update:open="batchConfirmOpen = false"
|
||
@confirm="doBatchDelete"
|
||
/>
|
||
|
||
<!-- 单条删除确认 -->
|
||
<AdminConfirm
|
||
:open="!!confirmDelete"
|
||
:loading="deleting"
|
||
danger
|
||
:message="'确定删除这条评论吗?此操作不可恢复。'"
|
||
@update:open="confirmDelete = null"
|
||
@confirm="doDelete"
|
||
/>
|
||
</div>
|
||
</template>
|