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 供测试
This commit is contained in:
2026-08-06 17:26:26 +08:00
parent d67849a311
commit 685c7b51d1
47 changed files with 1600 additions and 72 deletions
+227
View File
@@ -0,0 +1,227 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<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:list-checks" 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>
<button
@click="clearLogs"
:disabled="!logs.length || clearing"
class="inline-flex items-center gap-2 px-5 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 disabled:opacity-40 disabled:cursor-not-allowed transition-colors flex-shrink-0"
title="清空全部操作日志"
>
<Icon v-if="clearing" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
<Icon v-else name="ph:trash" class="w-4 h-4" />
清空日志
</button>
</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">共 {{ total }} 条日志</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="loadLogs" 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="!logs.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:list-checks" 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 whitespace-nowrap">时间</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 whitespace-nowrap">操作</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>
</tr>
</thead>
<tbody>
<tr
v-for="log in logs"
:key="log.id"
class="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/70 transition-colors"
>
<td class="px-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(log.createdAt) }}</td>
<td class="px-6 py-4 text-gray-700 whitespace-nowrap">{{ log.actor_name || 'system' }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span
class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-semibold"
:class="actionMeta(log.action).cls"
>
{{ actionMeta(log.action).label }}
</span>
</td>
<td class="px-6 py-4">
<code class="text-xs text-gray-600 bg-gray-50 border border-gray-100 rounded px-2 py-1">{{ log.target }}</code>
</td>
<td class="px-6 py-4 text-gray-600 max-w-[320px] md:max-w-md truncate" :title="log.detail || undefined">
{{ log.detail || '—' }}
</td>
</tr>
</tbody>
</table>
</div>
<!-- 加载更多 -->
<div v-if="logs.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 { onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '操作日志' })
interface AuditLog {
id: number
actor_name: string | null
action: string
target: string
detail: string | null
createdAt: string
}
const pageSize = 20
const page = ref(0)
const total = ref(0)
const logs = ref<AuditLog[]>([])
const loading = ref(true)
const loadingMore = ref(false)
const loadError = ref('')
const ACTION_META: Record<string, { label: string; cls: string }> = {
create: { label: '新增', cls: 'bg-green-100 text-green-700' },
update: { label: '更新', cls: 'bg-amber-100 text-amber-700' },
delete: { label: '删除', cls: 'bg-red-100 text-red-700' },
upload: { label: '上传', cls: 'bg-blue-100 text-blue-700' },
clear: { label: '清空', cls: 'bg-gray-200 text-gray-600' },
}
const actionMeta = (action: string) => ACTION_META[action] ?? { label: action, cls: 'bg-gray-100 text-gray-700' }
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 loadLogs = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ logs: AuditLog[]; total: number; page: number }>('/api/admin/audit', {
query: { page: 1, pageSize },
})
logs.value = data.logs
total.value = data.total
page.value = data.page
} catch (err) {
console.error('load audit error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const loadMore = async () => {
if (loadingMore.value || logs.value.length >= total.value) return
loadingMore.value = true
try {
const next = page.value + 1
const data = await $fetch<{ logs: AuditLog[]; total: number; page: number }>('/api/admin/audit', {
query: { page: next, pageSize },
})
logs.value.push(...data.logs)
page.value = data.page
} catch (err) {
console.error('load more audit error:', err)
showFlash('加载更多失败,请重试', 'err')
} finally {
loadingMore.value = false
}
}
const clearing = ref(false)
const clearLogs = async () => {
if (!window.confirm('确定清空全部操作日志吗?此操作不可恢复。')) return
clearing.value = true
try {
await $fetch('/api/admin/audit', { method: 'DELETE' })
showFlash('日志已清空')
await loadLogs()
} catch (err) {
console.error('clear audit error:', err)
showFlash((err as any)?.data?.message || '清空失败,请重试', 'err')
} finally {
clearing.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)
}
onMounted(loadLogs)
</script>
+41 -2
View File
@@ -41,7 +41,7 @@
</button>
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ comments.length }} 条评论</span>
<span class="text-sm text-gray-500">共 {{ total }} 条评论</span>
<template v-if="keyword">
<span class="text-sm text-gray-400">· 匹配 {{ filteredComments.length }} 条</span>
</template>
@@ -122,6 +122,18 @@
</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>
@@ -140,9 +152,13 @@ interface AdminComment {
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(() => {
@@ -169,8 +185,12 @@ const loadComments = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ comments: AdminComment[] }>('/api/admin/comments')
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 = '加载失败,请重试'
@@ -179,11 +199,30 @@ const loadComments = async () => {
}
}
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)
+36
View File
@@ -25,6 +25,7 @@
<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>
<!-- 加载中 -->
@@ -80,6 +81,23 @@
<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"
@@ -111,6 +129,7 @@ interface AdminContact {
subject: string
message: string
delivered: number
read: number
createdAt: string
}
@@ -119,6 +138,7 @@ 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')
@@ -146,6 +166,22 @@ const loadContacts = async () => {
}
}
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 {
+165
View File
@@ -0,0 +1,165 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<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:image" 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>
<button
@click="loadFiles"
class="inline-flex items-center gap-2 px-4 py-2 rounded-full border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors flex-shrink-0"
>
<Icon name="ph:arrows-clockwise" class="w-4 h-4" />
刷新
</button>
</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">共 {{ files.length }} 张图片</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="loadFiles" 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="!files.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:image" class="w-10 h-10" />
<p class="text-sm">暂无图片</p>
</div>
<!-- 图片网格 -->
<div v-else class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
<div
v-for="f in files"
:key="f.name"
class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden group"
>
<div class="relative aspect-square bg-gray-50">
<img :src="f.url" :alt="f.name" loading="lazy" class="w-full h-full object-cover" />
<button
@click="removeFile(f)"
class="absolute top-2 right-2 w-8 h-8 rounded-full bg-black/60 text-white flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-600"
title="删除图片"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
<div class="p-3">
<p class="text-xs font-semibold text-gray-800 truncate" :title="f.name">{{ f.name }}</p>
<div class="mt-1 flex items-center justify-between gap-2 text-[11px] text-gray-400">
<span class="flex-shrink-0">{{ formatSize(f.size) }}</span>
<span class="truncate">{{ formatDate(f.modified) }}</span>
</div>
</div>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '图片管理' })
interface UploadFile {
name: string
size: number
modified: string
url: string
}
const files = ref<UploadFile[]>([])
const loading = ref(true)
const loadError = ref('')
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 loadFiles = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ files: UploadFile[] }>('/api/admin/uploads')
files.value = data.files
} catch (err) {
console.error('load uploads error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const removeFile = async (f: UploadFile) => {
if (!window.confirm(`确定删除图片「${f.name}」吗?此操作不可恢复。`)) return
try {
await $fetch(`/api/admin/uploads/${encodeURIComponent(f.name)}`, { method: 'DELETE' })
files.value = files.value.filter((x) => x.name !== f.name)
showFlash('已删除;如被内容引用请同步更新')
} catch (err) {
console.error('delete upload error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
const formatSize = (bytes: number) => {
if (!bytes && bytes !== 0) return ''
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
return `${Math.max(1, Math.round(bytes / 1024))} KB`
}
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', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}).format(d)
}
onMounted(loadFiles)
</script>