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 供测试
228 lines
8.3 KiB
Vue
228 lines
8.3 KiB
Vue
<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>
|