Files
arcticnewone/app/pages/admin/comments.vue
T
virtheart 685c7b51d1 feat: 收官补齐全部未实现功能
- 账号体系:修改密码(/account + PATCH /api/auth/password)、自助删除账号
  (DELETE /api/auth/me)、订阅退订(token + /unsubscribe 页)
- 前台/SEO:RSS 订阅 /feed.xml、作品详情分享、canonical URL、
  文章 Article JSON-LD、品牌化错误页(i18n)
- 后台增强:图片管理 /admin/uploads、操作审计日志 /admin/audit(含清空)、
  联系消息已读标记、评论列表分页
- 工程:vitest 测试(密码哈希/内容校验器/种子完整性,12 例)+ pnpm test
- DB 迁移:contacts.read、subscribers.token、audit_logs(老库 ALTER 兼容)
- 密码逻辑抽为 server/utils/password.ts 供测试
2026-08-06 17:26:26 +08:00

248 lines
9.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8">
<div class="flex items-center gap-3">
<div class="w-11 h-11 rounded-2xl bg-black text-white flex items-center justify-center flex-shrink-0">
<Icon name="ph:chat-text" class="w-6 h-6" />
</div>
<div>
<h1 class="text-2xl font-bold text-black leading-tight">评论管理</h1>
<p class="text-sm text-gray-500 mt-1">查看全站文章评论,按文章或内容关键词检索。</p>
</div>
</div>
</header>
<!-- 操作反馈 -->
<div
v-if="flash"
class="mb-6 px-4 py-3 rounded-xl text-sm font-medium flex items-center gap-2"
:class="flashKind === 'ok' ? 'bg-green-50 text-green-700 border border-green-200' : 'bg-red-50 text-red-700 border border-red-200'"
>
<Icon :name="flashKind === 'ok' ? 'ph:check-circle' : 'ph:warning-circle'" class="w-4 h-4" />
{{ flash }}
</div>
<!-- 搜索框 -->
<div class="relative max-w-md mb-4">
<Icon name="ph:magnifying-glass" class="w-5 h-5 text-gray-400 absolute left-4 top-1/2 -translate-y-1/2" />
<input
v-model="keyword"
type="text"
placeholder="按文章或内容关键词筛选…"
class="w-full pl-12 pr-10 py-3 rounded-full border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 text-sm"
/>
<button
v-if="keyword"
@click="keyword = ''"
class="w-6 h-6 rounded-full flex items-center justify-center text-gray-400 hover:text-gray-600 absolute right-3 top-1/2 -translate-y-1/2"
title="清空筛选"
>
<Icon name="ph:x" class="w-4 h-4" />
</button>
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ total }} 条评论</span>
<template v-if="keyword">
<span class="text-sm text-gray-400">· 匹配 {{ filteredComments.length }} 条</span>
</template>
</div>
<!-- 加载中 -->
<div v-if="loading" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:circle-notch" class="w-8 h-8 animate-spin" />
<p class="text-sm">加载中…</p>
</div>
<!-- 加载失败 -->
<div v-else-if="loadError" class="py-24 text-center">
<div class="inline-flex flex-col items-center gap-3 text-gray-500">
<Icon name="ph:warning-circle" class="w-8 h-8 text-red-500" />
<p class="text-sm">{{ loadError }}</p>
<button @click="loadComments" class="px-5 py-2 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors">
重试
</button>
</div>
</div>
<!-- 空态 -->
<div v-else-if="!comments.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:chat-text" class="w-10 h-10" />
<p class="text-sm">暂无评论</p>
</div>
<div v-else-if="!filteredComments.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:magnifying-glass" class="w-10 h-10" />
<p class="text-sm">没有匹配的评论</p>
</div>
<!-- 评论表格 -->
<div v-else class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-gray-500 border-b border-gray-100 bg-gray-50">
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider">文章</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider">作者</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider">内容</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">评论时间</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider text-right">操作</th>
</tr>
</thead>
<tbody>
<tr
v-for="c in filteredComments"
:key="c.id"
class="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/70 transition-colors"
>
<td class="px-6 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-6 py-4 text-gray-600 whitespace-nowrap">{{ c.username }}</td>
<td class="px-6 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-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(c.createdAt) }}</td>
<td class="px-6 py-4">
<div class="flex items-center justify-end">
<button
@click="removeComment(c)"
class="w-8 h-8 rounded-full flex items-center justify-center text-gray-400 hover:bg-red-50 hover:text-red-600 transition-colors"
title="删除评论"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- 加载更多 -->
<div v-if="comments.length < total" class="p-4 border-t border-gray-100 flex justify-center">
<button
@click="loadMore"
:disabled="loadingMore"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 disabled:opacity-60 transition-colors"
>
<Icon v-if="loadingMore" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ loadingMore ? '加载中…' : '加载更多' }}
</button>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '评论管理' })
interface AdminComment {
id: number
newsSlug: string
content: string
createdAt: string
username: string
}
const pageSize = 20
const page = ref(0)
const total = ref(0)
const comments = ref<AdminComment[]>([])
const keyword = ref('')
const loading = ref(true)
const loadingMore = ref(false)
const loadError = 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),
)
})
const flash = ref('')
const flashKind = ref<'ok' | 'err'>('ok')
let flashTimer: ReturnType<typeof setTimeout> | null = null
const showFlash = (msg: string, kind: 'ok' | 'err' = 'ok') => {
flash.value = msg
flashKind.value = kind
if (flashTimer) clearTimeout(flashTimer)
flashTimer = setTimeout(() => {
flash.value = ''
}, 3000)
}
const loadComments = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ comments: AdminComment[]; total: number; page: number }>('/api/admin/comments', {
query: { page: 1, 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
}
}
const loadMore = async () => {
if (loadingMore.value || comments.value.length >= total.value) return
loadingMore.value = true
try {
const next = page.value + 1
const data = await $fetch<{ comments: AdminComment[]; total: number; page: number }>('/api/admin/comments', {
query: { page: next, pageSize },
})
comments.value.push(...data.comments)
page.value = data.page
} catch (err) {
console.error('load more comments error:', err)
showFlash('加载更多失败,请重试', 'err')
} finally {
loadingMore.value = false
}
}
const removeComment = async (c: AdminComment) => {
if (!window.confirm('确定删除这条评论吗?此操作不可恢复。')) return
try {
await $fetch(`/api/admin/comments/${c.id}`, { method: 'DELETE' })
comments.value = comments.value.filter((x) => x.id !== c.id)
total.value = Math.max(0, total.value - 1)
showFlash('评论已删除')
} catch (err) {
console.error('delete comment error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
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)
}
onMounted(loadComments)
</script>