322 lines
12 KiB
Vue
322 lines
12 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台 Dashboard:统计卡片 + 最近联系/评论 + 快捷入口 + 存储用量。
|
||
* 数据在 onMounted 客户端拉取(/api/admin/stats、/contacts、/comments、/uploads)。
|
||
*/
|
||
import { computed, onMounted, ref } from 'vue'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
useSeoMeta({ title: 'Dashboard' })
|
||
|
||
interface Stats {
|
||
articles: number
|
||
works: number
|
||
publishedArticles: number
|
||
publishedWorks: number
|
||
users: number
|
||
comments: number
|
||
contacts: number
|
||
subscribers: number
|
||
favorites: number
|
||
}
|
||
|
||
interface AdminContact {
|
||
id: number
|
||
name: string
|
||
email: string
|
||
subject: string
|
||
message: string
|
||
delivered: number
|
||
read: number
|
||
createdAt: string
|
||
}
|
||
|
||
interface AdminComment {
|
||
id: number
|
||
newsSlug: string
|
||
content: string
|
||
username: string
|
||
createdAt: string
|
||
}
|
||
|
||
interface UploadFile {
|
||
name: string
|
||
size: number
|
||
modified: string
|
||
url: string
|
||
}
|
||
|
||
const stats = ref<Stats | null>(null)
|
||
const contacts = ref<AdminContact[]>([])
|
||
const comments = ref<AdminComment[]>([])
|
||
|
||
const loading = ref(true)
|
||
const error = ref('')
|
||
|
||
const files = ref<UploadFile[]>([])
|
||
const storageType = ref('')
|
||
const storageLoading = ref(true)
|
||
const storageError = ref('')
|
||
|
||
const loadDashboard = async () => {
|
||
loading.value = true
|
||
error.value = ''
|
||
try {
|
||
const [s, c, cm] = await Promise.all([
|
||
$fetch<Stats>('/api/admin/stats'),
|
||
$fetch<{ contacts: AdminContact[] }>('/api/admin/contacts'),
|
||
$fetch<{ comments: AdminComment[] }>('/api/admin/comments?page=1&pageSize=5'),
|
||
])
|
||
stats.value = s
|
||
contacts.value = (c.contacts || []).slice(0, 5)
|
||
comments.value = (cm.comments || []).slice(0, 5)
|
||
} catch (err: any) {
|
||
error.value = err?.data?.message || '数据加载失败,请重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const loadStorage = async () => {
|
||
storageLoading.value = true
|
||
storageError.value = ''
|
||
try {
|
||
const data = await $fetch<{ files: UploadFile[]; storage: string }>('/api/admin/uploads')
|
||
files.value = data.files || []
|
||
storageType.value = data.storage || ''
|
||
} catch {
|
||
storageError.value = '存储信息加载失败'
|
||
} finally {
|
||
storageLoading.value = false
|
||
}
|
||
}
|
||
|
||
const refreshing = ref(false)
|
||
const refreshAll = async () => {
|
||
refreshing.value = true
|
||
try {
|
||
await Promise.all([loadDashboard(), loadStorage()])
|
||
} finally {
|
||
refreshing.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
loadDashboard()
|
||
loadStorage()
|
||
})
|
||
|
||
const cards = computed(() => {
|
||
const s = stats.value
|
||
return [
|
||
{
|
||
label: '动态文章',
|
||
value: s?.articles ?? 0,
|
||
sub: `已发布 ${s?.publishedArticles ?? 0}`,
|
||
icon: 'ph:newspaper',
|
||
to: '/admin/news',
|
||
},
|
||
{
|
||
label: '作品',
|
||
value: s?.works ?? 0,
|
||
sub: `已发布 ${s?.publishedWorks ?? 0}`,
|
||
icon: 'ph:image-square',
|
||
to: '/admin/work',
|
||
},
|
||
{ label: '注册用户', value: s?.users ?? 0, sub: '全部用户', icon: 'ph:users-three', to: '/admin/users' },
|
||
{ label: '评论', value: s?.comments ?? 0, sub: '全部评论', icon: 'ph:chat-circle-text', to: '/admin/comments' },
|
||
{ label: '联系留言', value: s?.contacts ?? 0, sub: '表单留言', icon: 'ph:envelope-simple', to: '/admin/contacts' },
|
||
{ label: '订阅邮箱', value: s?.subscribers ?? 0, sub: 'Newsletter', icon: 'ph:paper-plane-tilt', to: '/admin/subscribers' },
|
||
{ label: '作品收藏', value: s?.favorites ?? 0, sub: '收藏数', icon: 'ph:bookmark-simple', to: '/admin/work' },
|
||
]
|
||
})
|
||
|
||
const storageTotalBytes = computed(() => files.value.reduce((sum, f) => sum + (Number(f.size) || 0), 0))
|
||
|
||
const formatSize = (bytes: number) => {
|
||
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(2)} MB`
|
||
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`
|
||
return `${bytes} B`
|
||
}
|
||
|
||
const formatDate = (raw?: string) => {
|
||
if (!raw) return ''
|
||
// 兼容 "YYYY-MM-DD HH:MM:SS"(UTC)与 ISO 两种格式
|
||
const iso = String(raw).includes('T') ? String(raw) : String(raw).replace(' ', 'T') + 'Z'
|
||
const d = new Date(iso)
|
||
if (Number.isNaN(d.getTime())) return raw
|
||
return new Intl.DateTimeFormat('zh-CN', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }).format(d)
|
||
}
|
||
|
||
const initial = (name?: string) => (name || '?').charAt(0).toUpperCase()
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<AdminPageHeader title="Dashboard" subtitle="站点内容与数据概览">
|
||
<template #actions>
|
||
<AdminButton variant="secondary" :loading="refreshing" @click="refreshAll">
|
||
<Icon name="ph:arrows-clockwise" class="w-4 h-4" /> 刷新数据
|
||
</AdminButton>
|
||
</template>
|
||
</AdminPageHeader>
|
||
|
||
<!-- 统计卡片 -->
|
||
<div
|
||
v-if="loading"
|
||
class="mt-8 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5"
|
||
>
|
||
<div
|
||
v-for="i in 8"
|
||
:key="i"
|
||
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-6 h-32 animate-pulse"
|
||
></div>
|
||
</div>
|
||
|
||
<div v-else-if="error" class="mt-8">
|
||
<AdminCard>
|
||
<AdminEmptyState icon="ph:warning-circle" title="数据加载失败" :description="error">
|
||
<template #action>
|
||
<AdminButton variant="secondary" @click="loadDashboard">重新加载</AdminButton>
|
||
</template>
|
||
</AdminEmptyState>
|
||
</AdminCard>
|
||
</div>
|
||
|
||
<div
|
||
v-else
|
||
class="mt-8 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5"
|
||
>
|
||
<NuxtLink
|
||
v-for="card in cards"
|
||
:key="card.label"
|
||
:to="card.to"
|
||
class="group block h-full"
|
||
>
|
||
<AdminCard class="h-full transition-all duration-300 hover:-translate-y-0.5 hover:shadow-md">
|
||
<div class="flex items-start justify-between gap-3">
|
||
<div>
|
||
<p class="text-3xl font-bold text-black leading-none">{{ card.value }}</p>
|
||
<p class="mt-2 text-sm font-semibold text-gray-800">{{ card.label }}</p>
|
||
<p class="mt-1 text-xs text-gray-400">{{ card.sub }}</p>
|
||
</div>
|
||
<div
|
||
class="w-10 h-10 rounded-xl bg-gray-100 text-gray-600 flex items-center justify-center group-hover:bg-black group-hover:text-white transition-colors duration-300"
|
||
>
|
||
<Icon :name="card.icon" class="w-5 h-5" />
|
||
</div>
|
||
</div>
|
||
</AdminCard>
|
||
</NuxtLink>
|
||
</div>
|
||
|
||
<!-- 最近动态 + 侧栏 -->
|
||
<div class="mt-8 grid grid-cols-1 xl:grid-cols-3 gap-5">
|
||
<div class="xl:col-span-2 grid grid-cols-1 sm:grid-cols-2 gap-5">
|
||
<!-- 最近联系 -->
|
||
<AdminCard title="最近联系" subtitle="最新 5 条留言">
|
||
<template #actions>
|
||
<NuxtLink to="/admin/contacts" class="text-xs font-semibold text-gray-500 hover:text-black transition-colors">
|
||
查看全部
|
||
</NuxtLink>
|
||
</template>
|
||
<AdminSkeleton v-if="loading" :rows="4" />
|
||
<AdminEmptyState v-else-if="!contacts.length" icon="ph:envelope-simple-open" title="暂无留言" />
|
||
<ul v-else class="divide-y divide-gray-100 -my-1">
|
||
<li v-for="c in contacts" :key="c.id" class="flex items-center gap-3 py-2.5">
|
||
<div
|
||
class="w-8 h-8 rounded-full bg-black text-white flex items-center justify-center text-xs font-bold flex-shrink-0"
|
||
>
|
||
{{ initial(c.name) }}
|
||
</div>
|
||
<div class="min-w-0 flex-1">
|
||
<div class="flex items-center gap-2">
|
||
<p class="text-sm font-semibold text-gray-800 truncate">{{ c.name }}</p>
|
||
<span
|
||
v-if="Number(c.read) !== 1"
|
||
class="w-1.5 h-1.5 rounded-full bg-red-500 flex-shrink-0"
|
||
title="未读"
|
||
></span>
|
||
</div>
|
||
<p class="text-xs text-gray-500 truncate">{{ c.subject }}</p>
|
||
</div>
|
||
<span class="text-[11px] text-gray-400 flex-shrink-0">{{ formatDate(c.createdAt) }}</span>
|
||
</li>
|
||
</ul>
|
||
</AdminCard>
|
||
|
||
<!-- 最近评论 -->
|
||
<AdminCard title="最近评论" subtitle="最新 5 条评论">
|
||
<template #actions>
|
||
<NuxtLink to="/admin/comments" class="text-xs font-semibold text-gray-500 hover:text-black transition-colors">
|
||
查看全部
|
||
</NuxtLink>
|
||
</template>
|
||
<AdminSkeleton v-if="loading" :rows="4" />
|
||
<AdminEmptyState v-else-if="!comments.length" icon="ph:chat-circle-text" title="暂无评论" />
|
||
<ul v-else class="divide-y divide-gray-100 -my-1">
|
||
<li v-for="cm in comments" :key="cm.id" class="py-2.5">
|
||
<div class="flex items-center gap-2">
|
||
<p class="text-sm font-semibold text-gray-800 truncate">{{ cm.username }}</p>
|
||
<span class="text-[11px] text-gray-400 flex-shrink-0">{{ formatDate(cm.createdAt) }}</span>
|
||
</div>
|
||
<p class="mt-0.5 text-xs text-gray-500 truncate">{{ cm.content }}</p>
|
||
</li>
|
||
</ul>
|
||
</AdminCard>
|
||
</div>
|
||
|
||
<div class="space-y-5">
|
||
<!-- 快捷入口 -->
|
||
<AdminCard title="快捷入口" subtitle="常用操作">
|
||
<div class="grid grid-cols-1 gap-2">
|
||
<NuxtLink
|
||
to="/admin/news"
|
||
class="inline-flex items-center justify-center gap-2 rounded-xl bg-black text-white text-sm font-semibold px-4 py-2.5 hover:bg-gray-800 transition-colors shadow-sm"
|
||
>
|
||
<Icon name="ph:plus" class="w-4 h-4" /> 新建动态
|
||
</NuxtLink>
|
||
<NuxtLink
|
||
to="/admin/uploads"
|
||
class="inline-flex items-center justify-center gap-2 rounded-xl bg-gray-100 text-gray-800 text-sm font-semibold px-4 py-2.5 hover:bg-gray-200 transition-colors"
|
||
>
|
||
<Icon name="ph:cloud-arrow-up" class="w-4 h-4" /> 上传图片
|
||
</NuxtLink>
|
||
<NuxtLink
|
||
to="/admin/settings"
|
||
class="inline-flex items-center justify-center gap-2 rounded-xl bg-gray-100 text-gray-800 text-sm font-semibold px-4 py-2.5 hover:bg-gray-200 transition-colors"
|
||
>
|
||
<Icon name="ph:gear-six" class="w-4 h-4" /> 站点设置
|
||
</NuxtLink>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 存储用量 -->
|
||
<AdminCard title="存储用量" subtitle="上传文件占用">
|
||
<AdminSkeleton v-if="storageLoading" :rows="3" />
|
||
<div v-else-if="storageError" class="py-4 text-center">
|
||
<p class="text-sm text-gray-500 mb-3">{{ storageError }}</p>
|
||
<AdminButton variant="secondary" size="sm" @click="loadStorage">重试</AdminButton>
|
||
</div>
|
||
<div v-else class="flex items-center gap-4">
|
||
<div class="flex-1">
|
||
<p class="text-2xl font-bold text-black leading-none">{{ files.length }}</p>
|
||
<p class="mt-1 text-xs text-gray-500">图片文件</p>
|
||
</div>
|
||
<div class="flex-1">
|
||
<p class="text-2xl font-bold text-black leading-none">{{ formatSize(storageTotalBytes) }}</p>
|
||
<p class="mt-1 text-xs text-gray-500">总大小</p>
|
||
</div>
|
||
</div>
|
||
<div v-if="storageType" class="mt-4 pt-4 border-t border-gray-100 flex items-center gap-2">
|
||
<AdminBadge :tone="storageType === 'oss' ? 'blue' : 'gray'">
|
||
{{ storageType === 'oss' ? '对象存储 OSS' : '本地存储' }}
|
||
</AdminBadge>
|
||
<span class="text-xs text-gray-400">约 {{ (storageTotalBytes / (1024 * 1024)).toFixed(1) }} MB</span>
|
||
</div>
|
||
</AdminCard>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|