685c7b51d1
- 账号体系:修改密码(/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 供测试
212 lines
8.2 KiB
Vue
212 lines
8.2 KiB
Vue
<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:envelope-simple-open" 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="flex items-center gap-2 mb-4">
|
||
<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>
|
||
</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="loadContacts" 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="!contacts.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
|
||
<Icon name="ph:envelope-simple-open" class="w-10 h-10" />
|
||
<p class="text-sm">暂无联系消息</p>
|
||
</div>
|
||
|
||
<!-- 消息卡片列表 -->
|
||
<div v-else class="space-y-4">
|
||
<div
|
||
v-for="c in contacts"
|
||
:key="c.id"
|
||
class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 md:p-6 hover:shadow-md transition-shadow"
|
||
>
|
||
<div class="flex items-start justify-between gap-4">
|
||
<div class="flex items-center gap-3 min-w-0">
|
||
<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">
|
||
<span
|
||
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-semibold"
|
||
:class="Number(c.delivered) === 1 ? 'bg-green-100 text-green-700' : 'bg-amber-100 text-amber-600'"
|
||
>
|
||
<Icon :name="Number(c.delivered) === 1 ? 'ph:check' : 'ph:clock'" class="w-3.5 h-3.5" />
|
||
{{ Number(c.delivered) === 1 ? '已送达' : '未送达' }}
|
||
</span>
|
||
<span
|
||
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs"
|
||
:class="Number(c.read) === 1 ? 'bg-gray-100 text-gray-500' : 'bg-black text-white font-bold'"
|
||
>
|
||
<Icon :name="Number(c.read) === 1 ? 'ph:envelope-open' : 'ph:envelope-simple'" class="w-3.5 h-3.5" />
|
||
{{ Number(c.read) === 1 ? '已读' : '未读' }}
|
||
</span>
|
||
<button
|
||
@click="toggleRead(c)"
|
||
:disabled="togglingId === c.id"
|
||
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full border text-xs font-semibold transition-colors disabled:opacity-50"
|
||
:class="Number(c.read) === 1 ? 'border-gray-200 text-gray-500 hover:bg-gray-100' : 'border-black text-black hover:bg-black hover:text-white'"
|
||
>
|
||
<Icon v-if="togglingId === c.id" name="ph:circle-notch" class="w-3.5 h-3.5 animate-spin" />
|
||
<Icon v-else :name="Number(c.read) === 1 ? 'ph:envelope-simple-open' : 'ph:check-circle'" class="w-3.5 h-3.5" />
|
||
{{ Number(c.read) === 1 ? '标为未读' : '标为已读' }}
|
||
</button>
|
||
<button
|
||
@click="removeContact(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>
|
||
</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>
|
||
</section>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
useSeoMeta({ title: '联系消息' })
|
||
|
||
interface AdminContact {
|
||
id: number
|
||
name: string
|
||
email: string
|
||
subject: string
|
||
message: string
|
||
delivered: number
|
||
read: number
|
||
createdAt: string
|
||
}
|
||
|
||
const contacts = ref<AdminContact[]>([])
|
||
const loading = ref(true)
|
||
const loadError = ref('')
|
||
|
||
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 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 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
|
||
}
|
||
}
|
||
|
||
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
|
||
showFlash(next === 1 ? '已标记为已读' : '已标记为未读')
|
||
} catch (err) {
|
||
console.error('toggle contact read error:', err)
|
||
showFlash((err as any)?.data?.message || '操作失败,请重试', 'err')
|
||
} finally {
|
||
togglingId.value = null
|
||
}
|
||
}
|
||
|
||
const removeContact = async (c: AdminContact) => {
|
||
if (!window.confirm(`确定删除「${c.name}」的这条消息吗?此操作不可恢复。`)) return
|
||
try {
|
||
await $fetch(`/api/admin/contacts/${c.id}`, { method: 'DELETE' })
|
||
contacts.value = contacts.value.filter((x) => x.id !== c.id)
|
||
showFlash('消息已删除')
|
||
} catch (err) {
|
||
console.error('delete contact 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(loadContacts)
|
||
</script>
|