Files
arcticnewone/app/pages/admin/contacts.vue
T
virtheart 5a7ad8de11 feat: 全面升级——后台优化 / 动态配置 / 个人中心 / OSS 存储
- OSS 文件存储(RustFS / S3 v4 兼容,零依赖手写 SigV4):
  上传/列表/删除走桶 arcticnewone,/uploads 代理服务 + 本地回退;
  server/utils/sigv4.ts 纯函数 + AWS 测试向量验证
- 业务动态配置:settings 表 + /api/settings(公开) + /api/admin/settings(PATCH),
  useSiteSettings 组合式,footer/contact/tools/app.vue/侧边栏/工具页全部接入,
  后台 /admin/settings 修改前台即时生效(站点/联系/社交/工具箱/页脚/SEO)
- 后台全面优化:Admin* 设计系统(按钮/输入/表单/卡片/徽标/弹窗/确认/空态/骨架/
  分页/页头/Toast)14 组件;Dashboard 增强;各列表页搜索/筛选/批量删除/分页;
  布局与侧边栏分组;统一 toast/确认反馈
- 个人中心全新重设计:顶部资料卡 + 统计、精致化改密/删号(二次确认)、
  收藏封面网格、评论卡片、移动端适配
- deploy.sh 支持 OSS 凭据注入(NUXT_OSS_*);.env.example 更新
- 测试 17 例全过(含 SigV4 AWS 向量、settings 默认结构)
2026-08-06 18:40:17 +08:00

350 lines
12 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.
<script setup lang="ts">
/**
* 后台联系留言:列表 + 已读/未读筛选 + 已读切换 + 多选批量删除 + 前端分页。
* - GET /api/admin/contacts;PATCH /api/admin/contacts/[id] body { read: 0|1 };DELETE /api/admin/contacts/[id]
*/
import { computed, onMounted, ref, watch } from 'vue'
import { useToastStore } from '~/stores/toast'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: '联系留言' })
interface AdminContact {
id: number
name: string
email: string
subject: string
message: string
delivered: number
read: number
createdAt: string
}
const toast = useToastStore()
/* ---------- 列表 ---------- */
const contacts = ref<AdminContact[]>([])
const loading = ref(true)
const loadError = ref('')
const loadContacts = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ contacts: AdminContact[] }>('/api/admin/contacts')
contacts.value = data.contacts
} catch (err) {
console.error('load contacts error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
onMounted(loadContacts)
const deliveredCount = computed(() => contacts.value.filter((c) => Number(c.delivered) === 1).length)
const unreadCount = computed(() => contacts.value.filter((c) => Number(c.read) !== 1).length)
/* ---------- 筛选 / 搜索 / 分页 ---------- */
const readFilter = ref<'all' | 'read' | 'unread'>('all')
const keyword = ref('')
const filteredContacts = computed(() => {
const kw = keyword.value.trim().toLowerCase()
return contacts.value.filter((c) => {
if (readFilter.value === 'read' && Number(c.read) !== 1) return false
if (readFilter.value === 'unread' && Number(c.read) === 1) return false
if (!kw) return true
return (
c.name.toLowerCase().includes(kw) ||
c.email.toLowerCase().includes(kw) ||
c.subject.toLowerCase().includes(kw) ||
c.message.toLowerCase().includes(kw)
)
})
})
const pageSize = 10
const page = ref(1)
const totalPages = computed(() => Math.max(1, Math.ceil(filteredContacts.value.length / pageSize)))
const pagedContacts = computed(() => {
const p = Math.min(page.value, totalPages.value)
const start = (p - 1) * pageSize
return filteredContacts.value.slice(start, start + pageSize)
})
watch([readFilter, keyword], () => {
page.value = 1
})
const changePage = (p: number) => {
page.value = Math.min(Math.max(1, p), totalPages.value)
}
/* ---------- 已读切换 ---------- */
const togglingId = ref<number | null>(null)
const toggleRead = async (c: AdminContact) => {
const next = Number(c.read) === 1 ? 0 : 1
togglingId.value = c.id
try {
await $fetch(`/api/admin/contacts/${c.id}`, { method: 'PATCH', body: { read: next } })
c.read = next
toast.success(next === 1 ? '已标记为已读' : '已标记为未读')
} catch (err: any) {
toast.error(err?.data?.message || '操作失败,请重试')
} finally {
togglingId.value = null
}
}
/* ---------- 多选 / 批量删除 ---------- */
const selected = ref<Set<number>>(new Set())
const allChecked = computed(
() => pagedContacts.value.length > 0 && pagedContacts.value.every((c) => selected.value.has(c.id)),
)
const toggleAll = () => {
if (allChecked.value) {
for (const c of pagedContacts.value) selected.value.delete(c.id)
selected.value = new Set(selected.value)
} else {
const next = new Set(selected.value)
for (const c of pagedContacts.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/contacts/${id}`, { method: 'DELETE' })
} catch (err: any) {
console.error('batch delete contact 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 loadContacts()
}
/* ---------- 单条删除 ---------- */
const confirmDelete = ref<AdminContact | null>(null)
const deleting = ref(false)
const askDelete = (c: AdminContact) => {
confirmDelete.value = c
}
const doDelete = async () => {
const c = confirmDelete.value
if (!c) return
deleting.value = true
try {
await $fetch(`/api/admin/contacts/${c.id}`, { method: 'DELETE' })
contacts.value = contacts.value.filter((x) => x.id !== c.id)
selected.value.delete(c.id)
toast.success('留言已删除')
confirmDelete.value = null
} catch (err: any) {
toast.error(err?.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="loadContacts">
<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">共 {{ contacts.length }} 条消息</span>
<span class="text-sm text-gray-400">· 已送达 {{ deliveredCount }} 条</span>
<span class="text-sm text-gray-400">· 未读 {{ unreadCount }} 条</span>
<span v-if="selected.size" class="text-sm text-gray-400">· 已选 {{ selected.size }} 项</span>
</div>
<div class="mb-5 flex flex-wrap items-center 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="搜索姓名 / 邮箱 / 主题 / 内容…"
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>
<AdminSelect
v-model="readFilter"
:options="[
{ value: 'all', label: '全部状态' },
{ value: 'unread', label: '未读' },
{ value: 'read', label: '已读' },
]"
class="w-full sm:w-36"
/>
<AdminButton variant="danger" size="sm" :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="loadContacts">重新加载</AdminButton>
</div>
</AdminCard>
<!-- 空态 -->
<AdminEmptyState
v-else-if="!contacts.length"
icon="ph:envelope-simple-open"
title="暂无联系消息"
description="访客通过联系表单提交的留言会出现在这里。"
/>
<AdminEmptyState
v-else-if="!filteredContacts.length"
icon="ph:magnifying-glass"
title="没有匹配的消息"
description="换个关键词或筛选条件试试。"
/>
<!-- 消息卡片列表 -->
<div v-else class="space-y-4">
<div
v-for="c in pagedContacts"
:key="c.id"
class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 md:p-6 transition-shadow"
:class="[selected.has(c.id) ? 'ring-2 ring-black/5 border-black/10' : 'hover:shadow-md']"
>
<div class="flex items-start justify-between gap-4">
<div class="flex items-center gap-3 min-w-0">
<input
type="checkbox"
:checked="selected.has(c.id)"
class="w-4 h-4 rounded border-gray-300 accent-black cursor-pointer flex-shrink-0 mt-1"
@change="toggleOne(c.id)"
/>
<div class="w-10 h-10 rounded-full bg-black text-white flex items-center justify-center font-bold flex-shrink-0">
{{ (c.name || '?').charAt(0) }}
</div>
<div class="min-w-0">
<div class="flex items-center gap-2 flex-wrap">
<h3 class="font-bold text-black">{{ c.name }}</h3>
<a :href="`mailto:${c.email}`" class="text-sm text-gray-500 hover:text-black transition-colors truncate">
{{ c.email }}
</a>
</div>
<p class="text-xs text-gray-400 mt-1">{{ formatDate(c.createdAt) }}</p>
</div>
</div>
<div class="flex items-center gap-2 flex-shrink-0">
<AdminBadge :tone="Number(c.delivered) === 1 ? 'green' : 'amber'">
<Icon :name="Number(c.delivered) === 1 ? 'ph:check' : 'ph:clock'" class="w-3.5 h-3.5" />
{{ Number(c.delivered) === 1 ? '已送达' : '未送达' }}
</AdminBadge>
<AdminBadge :tone="Number(c.read) === 1 ? 'gray' : 'black'">
<Icon :name="Number(c.read) === 1 ? 'ph:envelope-open' : 'ph:envelope-simple'" class="w-3.5 h-3.5" />
{{ Number(c.read) === 1 ? '已读' : '未读' }}
</AdminBadge>
<AdminButton size="sm" variant="secondary" :loading="togglingId === c.id" @click="toggleRead(c)">
{{ Number(c.read) === 1 ? '标为未读' : '标为已读' }}
</AdminButton>
<AdminButton size="sm" variant="ghost" @click="askDelete(c)">
<Icon name="ph:trash" class="w-4 h-4" />
</AdminButton>
</div>
</div>
<div class="mt-4 border-t border-gray-100 pt-4">
<p class="text-sm font-semibold text-gray-800">{{ c.subject }}</p>
<p class="mt-1.5 text-sm text-gray-600 whitespace-pre-line break-words">{{ c.message }}</p>
</div>
</div>
<div class="mt-4">
<AdminPagination :total="filteredContacts.length" :page="page" :page-size="pageSize" @page="changePage" />
</div>
</div>
<!-- 批量删除确认 -->
<AdminConfirm
:open="batchConfirmOpen"
:loading="batchDeleting"
danger
:message="`确定删除已选的 ${selected.size} 条留言吗?此操作不可恢复。`"
@update:open="batchConfirmOpen = false"
@confirm="doBatchDelete"
/>
<!-- 单条删除确认 -->
<AdminConfirm
:open="!!confirmDelete"
:loading="deleting"
danger
:message="`确定删除「${confirmDelete?.name}」的这条消息吗?此操作不可恢复。`"
@update:open="confirmDelete = null"
@confirm="doDelete"
/>
</div>
</template>