Files
virtheart f1d2a405f9 feat: 个人中心重设计 + 后台全量响应式适配
- 个人中心重设计:对齐站点黑白编辑风(极简资料区+细分隔线统计、下划线页签、
  细边框卡片、克制危险区、收藏网格、评论列表),移除后台系组件与彩色渐变
- 后台响应式适配:表格页 md 以下卡片堆叠(md:hidden/hidden md:block 双模板)、
  Dashboard 统计卡自适应、表单双语两列、AdminModal 移动端底部弹层、
  触摸目标与间距统一,覆盖全部 9 个后台页 + 布局
2026-08-06 19:21:37 +08:00

261 lines
9.5 KiB
Vue
Raw Permalink 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/audit?page=&pageSize= → { logs, total, page, pageSize };DELETE /api/admin/audit
*/
import { computed, onMounted, ref, watch } from 'vue'
import { useToastStore } from '~/stores/toast'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: '操作日志' })
interface AuditLog {
id: number
actor_name: string | null
action: string
target: string
detail: string | null
createdAt: string
}
const toast = useToastStore()
const ACTION_META: Record<string, { label: string; tone: 'green' | 'amber' | 'red' | 'blue' | 'gray' }> = {
create: { label: '新增', tone: 'green' },
update: { label: '更新', tone: 'amber' },
delete: { label: '删除', tone: 'red' },
upload: { label: '上传', tone: 'blue' },
clear: { label: '清空', tone: 'gray' },
}
const actionMeta = (action: string) =>
ACTION_META[action] ?? { label: action, tone: 'gray' as const }
const ACTION_OPTIONS = [
{ value: 'all', label: '全部操作' },
{ value: 'create', label: '新增' },
{ value: 'update', label: '更新' },
{ value: 'delete', label: '删除' },
{ value: 'upload', label: '上传' },
{ value: 'clear', label: '清空' },
]
/* ---------- 列表(后端分页) ---------- */
const pageSize = 15
const page = ref(1)
const total = ref(0)
const logs = ref<AuditLog[]>([])
const loading = ref(true)
const loadError = ref('')
const loadLogs = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ logs: AuditLog[]; total: number; page: number }>('/api/admin/audit', {
query: { page: page.value, 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
}
}
onMounted(loadLogs)
const changePage = (p: number) => {
if (p === page.value) return
page.value = p
loadLogs()
}
/* ---------- 筛选 / 关键词(当前页过滤) ---------- */
const actionFilter = ref('all')
const keyword = ref('')
const filteredLogs = computed(() => {
const kw = keyword.value.trim().toLowerCase()
return logs.value.filter((log) => {
if (actionFilter.value !== 'all' && log.action !== actionFilter.value) return false
if (!kw) return true
return (
(log.actor_name || '').toLowerCase().includes(kw) ||
log.target.toLowerCase().includes(kw) ||
(log.detail || '').toLowerCase().includes(kw)
)
})
})
watch([actionFilter, keyword], () => {
if (page.value !== 1) {
page.value = 1
loadLogs()
}
})
/* ---------- 清空 ---------- */
const confirmClear = ref(false)
const clearing = ref(false)
const clearLogs = async () => {
clearing.value = true
try {
await $fetch('/api/admin/audit', { method: 'DELETE' })
toast.success('日志已清空')
confirmClear.value = false
await loadLogs()
} catch (err: any) {
toast.error(err?.data?.message || '清空失败,请重试')
} 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)
}
</script>
<template>
<div>
<AdminPageHeader title="操作日志" subtitle="后台管理员的关键操作审计记录。">
<template #actions>
<AdminButton variant="danger" size="sm" :disabled="!total || clearing" :loading="clearing" @click="confirmClear = true">
<Icon name="ph:trash" 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">共 {{ total }} 条日志</span>
<span v-if="keyword" class="text-sm text-gray-400">· 当前页匹配 {{ filteredLogs.length }} 条</span>
</div>
<div class="mb-5 flex flex-col gap-3 md:flex-row">
<div class="relative flex-1 md: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="actionFilter" :options="ACTION_OPTIONS" class="w-full md:w-40" />
</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="loadLogs">重新加载</AdminButton>
</div>
</AdminCard>
<!-- 空态 -->
<AdminEmptyState v-else-if="!logs.length" icon="ph:list-checks" title="暂无操作日志" description="后台管理操作会自动记录在这里。" />
<AdminEmptyState
v-else-if="!filteredLogs.length"
icon="ph:magnifying-glass"
title="当前页没有匹配的日志"
description="换个关键词或筛选条件,或在其他页试试。"
/>
<!-- 日志表格 -->
<template v-else>
<!-- 桌面表格(md+) -->
<div class="hidden md:block rounded-2xl border border-gray-100 bg-white shadow-sm overflow-hidden">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-gray-400 border-b border-gray-100 bg-gray-50">
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap sticky left-0 bg-gray-50 z-10 border-r border-gray-100">时间</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">操作人</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">操作</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">对象</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">详情</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-50">
<tr v-for="log in filteredLogs" :key="log.id" class="hover:bg-gray-50/60 transition-colors">
<td class="px-5 py-4 text-gray-500 text-xs whitespace-nowrap sticky left-0 bg-white z-10 border-r border-gray-100">{{ formatDate(log.createdAt) }}</td>
<td class="px-5 py-4 text-gray-700 whitespace-nowrap">{{ log.actor_name || 'system' }}</td>
<td class="px-5 py-4 whitespace-nowrap">
<AdminBadge :tone="actionMeta(log.action).tone">{{ actionMeta(log.action).label }}</AdminBadge>
</td>
<td class="px-5 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-5 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>
<!-- 移动端卡片(<md) -->
<div class="md:hidden space-y-3">
<div v-for="log in filteredLogs" :key="log.id" class="bg-white rounded-2xl border border-gray-100 shadow-sm p-4">
<div class="flex items-center justify-between gap-2">
<AdminBadge :tone="actionMeta(log.action).tone">{{ actionMeta(log.action).label }}</AdminBadge>
<span class="text-xs text-gray-400 flex-shrink-0">{{ formatDate(log.createdAt) }}</span>
</div>
<div class="mt-2 flex items-center gap-2 text-sm">
<span class="text-gray-400 flex-shrink-0">操作人</span>
<span class="text-gray-700 truncate">{{ log.actor_name || 'system' }}</span>
</div>
<div class="mt-1.5">
<code class="inline-block max-w-full text-xs text-gray-600 bg-gray-50 border border-gray-100 rounded px-2 py-1 break-all">{{ log.target }}</code>
</div>
<p class="mt-2 text-sm text-gray-600 break-words">{{ log.detail || '—' }}</p>
</div>
</div>
<div class="mt-4">
<AdminPagination :total="total" :page="page" :page-size="pageSize" @page="changePage" />
</div>
</template>
<!-- 清空确认 -->
<AdminConfirm
:open="confirmClear"
:loading="clearing"
danger
message="确定清空全部操作日志吗?此操作不可恢复。"
@update:open="confirmClear = false"
@confirm="clearLogs"
/>
</div>
</template>