✨ 新增回收站与操作记录页面

This commit is contained in:
orion committed 2026-08-07 10:27:34 +08:00
1 parent d47d0edbce
commit 2bf610ac22
14 files changed
+418 -1

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
<template>
<div class="p-4 sm:p-6">
<header class="mb-5 flex flex-col gap-3 sm:mb-6 sm:flex-row sm:items-end sm:justify-between">
<div>
<p class="text-xs font-medium tracking-[0.16em]" :class="mutedTextClass">ADMIN AUDIT</p>
<h2 class="mt-1 text-2xl font-bold" :class="primaryTextClass">操作记录</h2>
<p class="mt-1 text-sm" :class="mutedTextClass">查看后台管理、回收站和配置变更的可追溯记录。</p>
</div>
<BaseButton variant="secondary" :loading="isLoading" @click="loadActivities">
<template #icon><RefreshCwIcon class="mr-2 h-4 w-4" /></template>
刷新记录
</BaseButton>
</header>
<section class="theme-2026-card theme-2026-card-hover mb-5 rounded-[24px] border p-3 shadow-md sm:mb-6 sm:rounded-[30px] sm:p-4" :class="panelClass">
<div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-[minmax(0,1fr)_220px_220px_auto]">
<label class="min-w-0">
<span class="sr-only">搜索操作、对象或文件名</span>
<span class="relative block">
<SearchIcon class="absolute left-3 top-3 h-4 w-4" :class="mutedTextClass" />
<input v-model="filters.keyword" class="min-h-11 w-full rounded-xl border py-2.5 pl-10 pr-3 text-sm outline-none focus:ring-2 focus:ring-[#8fb2bf]" :class="fieldClass" placeholder="搜索操作、对象或文件名" @keyup.enter="applyFilters" />
</span>
</label>
<label>
<span class="mb-1 block text-xs font-medium" :class="mutedTextClass">操作类型</span>
<select v-model="filters.action" class="min-h-11 w-full rounded-xl border px-3 text-sm outline-none focus:ring-2 focus:ring-[#8fb2bf]" :class="fieldClass" @change="applyFilters">
<option value="">全部操作</option>
<option v-for="option in actionOptions" :key="option.value" :value="option.value">{{ actionLabel(option.label) }} · {{ option.count }}</option>
</select>
</label>
<label>
<span class="mb-1 block text-xs font-medium" :class="mutedTextClass">对象类型</span>
<select v-model="filters.targetType" class="min-h-11 w-full rounded-xl border px-3 text-sm outline-none focus:ring-2 focus:ring-[#8fb2bf]" :class="fieldClass" @change="applyFilters">
<option value="">全部对象</option>
<option v-for="option in targetTypeOptions" :key="option.value" :value="option.value">{{ targetTypeLabel(option.label) }} · {{ option.count }}</option>
</select>
</label>
<div class="flex items-end gap-2">
<BaseButton class="flex-1" :loading="isLoading" @click="applyFilters">筛选</BaseButton>
<button type="button" class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl border transition-colors" :class="actionClass" aria-label="重置筛选" title="重置筛选" @click="resetFilters">
<RotateCcwIcon class="h-4 w-4" />
</button>
</div>
</div>
</section>
<section class="theme-2026-card overflow-hidden rounded-[24px] border shadow-md sm:rounded-[30px]" :class="panelClass">
<div class="flex flex-wrap items-center justify-between gap-3 border-b px-4 py-3 sm:px-6 sm:py-4" :class="dividerClass">
<div>
<h3 class="text-base font-semibold" :class="primaryTextClass">审计事件</h3>
<p class="mt-0.5 text-xs" :class="mutedTextClass">筛选结果 {{ total }} 条 · 已保存 {{ storedTotal }} 条</p>
</div>
<span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-medium" :class="isDarkMode ? 'bg-[#263941] text-[#b8d6de]' : 'bg-[#e5f0f1] text-[#365a66]'">
<ShieldCheckIcon class="mr-1.5 h-3.5 w-3.5" /> 审计留痕
</span>
</div>
<div v-if="isLoading" class="flex min-h-64 items-center justify-center text-sm" :class="mutedTextClass"><RefreshCwIcon class="mr-2 h-4 w-4 animate-spin" />正在加载操作记录…</div>
<div v-else-if="errorText" class="px-4 py-12 text-center sm:px-6"><p class="text-sm font-medium text-red-600">{{ errorText }}</p><BaseButton class="mt-3" variant="secondary" @click="loadActivities">重新加载</BaseButton></div>
<div v-else-if="activities.length === 0" class="px-4 py-14 text-center sm:px-6"><ClipboardListIcon class="mx-auto h-9 w-9" :class="mutedTextClass" /><p class="mt-3 text-sm" :class="mutedTextClass">没有匹配的操作记录</p></div>
<div v-else class="divide-y" :class="dividerClass">
<article v-for="activity in activities" :key="activity.id" class="group grid gap-3 px-4 py-4 transition-colors sm:grid-cols-[auto_minmax(0,1fr)_auto] sm:items-start sm:px-6" :class="isDarkMode ? 'hover:bg-[#253941]/60' : 'hover:bg-[#f2f8f8]'">
<div class="flex h-10 w-10 shrink-0 items-center justify-center rounded-xl" :class="activityToneClass(activity.action)">
<component :is="activityIcon(activity.action)" class="h-5 w-5" aria-hidden="true" />
</div>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<p class="break-words text-sm font-semibold" :class="primaryTextClass">{{ actionLabel(activity.action) }}</p>
<span class="rounded-full border px-2 py-0.5 text-[11px] font-medium" :class="badgeClass">{{ targetTypeLabel(activity.targetType) }}</span>
</div>
<p class="mt-1 break-all text-sm" :class="mutedTextClass">{{ activity.targetName || '系统对象' }}<span v-if="activity.targetId !== null && activity.targetId !== undefined" class="ml-1.5 text-xs">#{{ activity.targetId }}</span></p>
<div v-if="metaSummary(activity.meta)" class="mt-2 inline-flex max-w-full rounded-lg px-2 py-1 text-xs" :class="isDarkMode ? 'bg-[#17262c] text-[#abc0c6]' : 'bg-[#edf4f5] text-[#557078]'">{{ metaSummary(activity.meta) }}</div>
</div>
<time class="text-xs tabular-nums sm:pt-1 sm:text-right" :class="mutedTextClass">{{ formatTimestamp(activity.createdAt) }}</time>
</article>
</div>
<DataPagination v-if="total > 0" :current-page="filters.page" :page-size="filters.size" :total="total" @page-change="changePage" @page-size-change="changePageSize" />
</section>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import type { Component } from 'vue'
import { ActivityIcon, ClipboardListIcon, CogIcon, FilePenLineIcon, RefreshCwIcon, RotateCcwIcon, SearchIcon, ShieldCheckIcon, Trash2Icon, UploadIcon } from 'lucide-vue-next'
import BaseButton from '@/components/common/BaseButton.vue'
import DataPagination from '@/components/common/DataPagination.vue'
import { useAdminActivities, useInjectedDarkMode } from '@/composables'
import type { AdminActivityEvent, AdminActivityOption } from '@/types'
import { formatTimestamp } from '@/utils/common'
const isDarkMode = useInjectedDarkMode()
const { fetchAdminActivities } = useAdminActivities()
const activities = ref<AdminActivityEvent[]>([])
const actionOptions = ref<AdminActivityOption[]>([])
const targetTypeOptions = ref<AdminActivityOption[]>([])
const total = ref(0)
const storedTotal = ref(0)
const isLoading = ref(false)
const errorText = ref('')
const filters = ref({ page: 1, size: 20, keyword: '', action: '', targetType: '' })
const primaryTextClass = computed(() => isDarkMode.value ? 'text-white' : 'text-[#1f3c45]')
const mutedTextClass = computed(() => isDarkMode.value ? 'text-[#a9bdc2]' : 'text-[#617980]')
const panelClass = computed(() => isDarkMode.value ? 'border-[#40545c] bg-[#202f36]/90' : 'border-[#d7e3e5] bg-[#f8fbfb]')
const fieldClass = computed(() => isDarkMode.value ? 'border-[#50666f] bg-[#253941] text-white placeholder:text-[#9fb2b7]' : 'border-[#c8d8dc] bg-white text-[#263f48] placeholder:text-[#84969a]')
const dividerClass = computed(() => isDarkMode.value ? 'divide-[#40545c] border-[#40545c]' : 'divide-[#d7e3e5] border-[#d7e3e5]')
const actionClass = computed(() => isDarkMode.value ? 'border-[#50666f] bg-[#263941] text-[#d8e3e5] hover:bg-[#2d4751]' : 'border-[#c8d8dc] bg-[#edf4f5] text-[#415d66] hover:bg-[#e2ecee]')
const badgeClass = computed(() => isDarkMode.value ? 'border-[#50666f] text-[#b8d6de]' : 'border-[#c8d8dc] text-[#4e6c75]')
const actionLabel = (action: string) => ({
'file.delete': '移入回收站', 'files.batch_delete': '批量移入回收站', 'trash.restore': '从回收站恢复', 'trash.purge': '永久清理文件本体', 'file.update': '更新文件信息', 'files.batch_update': '批量更新文件', 'file.metadata_update': '更新文件备注', 'file.policy_action': '调整文件策略', 'files.batch_policy_action': '批量调整策略', 'config.update': '更新系统设置', 'local_file.delete': '删除本地文件', 'local_file.share': '分享本地文件'
}[action] || action)
const targetTypeLabel = (targetType: string) => ({ file: '文件', config: '系统配置', local_file: '本地文件', view_preset: '视图预设', system: '系统' }[targetType] || targetType || '系统')
const activityIcon = (action: string): Component => action.includes('trash') || action.includes('delete') ? Trash2Icon : action.includes('config') ? CogIcon : action.includes('update') || action.includes('policy') ? FilePenLineIcon : action.includes('share') ? UploadIcon : ActivityIcon
const activityToneClass = (action: string) => {
if (action === 'trash.purge') return isDarkMode.value ? 'bg-red-950/50 text-red-300' : 'bg-red-50 text-red-600'
if (action.includes('trash') || action.includes('delete')) return isDarkMode.value ? 'bg-amber-950/45 text-amber-300' : 'bg-amber-50 text-amber-700'
if (action.includes('restore') || action.includes('share')) return isDarkMode.value ? 'bg-emerald-950/45 text-emerald-300' : 'bg-emerald-50 text-emerald-700'
return isDarkMode.value ? 'bg-[#263f48] text-[#b8d6de]' : 'bg-[#e5f0f1] text-[#397084]'
}
const metaSummary = (meta: Record<string, unknown>) => Object.entries(meta || {}).slice(0, 3).map(([key, value]) => `${key}: ${Array.isArray(value) ? value.length + ' 项' : String(value)}`).join(' · ')
const loadActivities = async () => {
isLoading.value = true
errorText.value = ''
try {
const detail = await fetchAdminActivities(filters.value)
activities.value = detail?.activities || detail?.items || []
total.value = Number(detail?.total || 0)
storedTotal.value = Number(detail?.storedTotal ?? detail?.stored_total ?? total.value)
actionOptions.value = detail?.actionOptions || detail?.action_options || []
targetTypeOptions.value = detail?.targetTypeOptions || detail?.target_type_options || []
} catch (error: unknown) {
errorText.value = (error as { response?: { data?: { detail?: string } } })?.response?.data?.detail || '操作记录加载失败,请稍后重试。'
} finally { isLoading.value = false }
}
const applyFilters = () => { filters.value.page = 1; void loadActivities() }
const resetFilters = () => { filters.value = { page: 1, size: filters.value.size, keyword: '', action: '', targetType: '' }; void loadActivities() }
const changePage = (page: number) => { filters.value.page = page; void loadActivities() }
const changePageSize = (size: number) => { filters.value.size = size; filters.value.page = 1; void loadActivities() }
void loadActivities()
</script>
+96
View File
@@ -0,0 +1,96 @@
<template>
<div class="p-4 sm:p-6">
<header class="mb-5 flex flex-col gap-3 sm:mb-6 sm:flex-row sm:items-end sm:justify-between">
<div>
<p class="text-xs font-medium tracking-[0.16em]" :class="mutedTextClass">RECYCLE BIN</p>
<h2 class="mt-1 text-2xl font-bold" :class="primaryTextClass">回收站</h2>
<p class="mt-1 text-sm" :class="mutedTextClass">删除文件会先保留在这里;恢复后将生成新的 8 位取件码。</p>
</div>
<BaseButton variant="secondary" :loading="isLoading" @click="loadTrash">
<template #icon><RefreshCwIcon class="mr-2 h-4 w-4" /></template>
刷新回收站
</BaseButton>
</header>
<section class="mb-5 grid gap-3 sm:mb-6 sm:grid-cols-3">
<div class="theme-2026-card rounded-[20px] border p-3.5 shadow-sm sm:rounded-[24px] sm:p-4" :class="panelClass">
<p class="text-xs" :class="mutedTextClass">回收站记录</p><strong class="mt-1 block text-2xl" :class="primaryTextClass">{{ total }}</strong>
</div>
<div class="theme-2026-card rounded-[20px] border p-3.5 shadow-sm sm:rounded-[24px] sm:p-4" :class="panelClass">
<p class="text-xs" :class="mutedTextClass">可恢复</p><strong class="mt-1 block text-2xl" :class="primaryTextClass">{{ recoverableCount }}</strong>
</div>
<div class="theme-2026-card rounded-[20px] border p-3.5 shadow-sm sm:rounded-[24px] sm:p-4" :class="panelClass">
<p class="text-xs" :class="mutedTextClass">仅审计记录</p><strong class="mt-1 block text-2xl" :class="primaryTextClass">{{ purgedCount }}</strong>
</div>
</section>
<section class="theme-2026-card overflow-hidden rounded-[24px] border shadow-md sm:rounded-[30px]" :class="panelClass">
<div class="flex flex-wrap items-center justify-between gap-3 border-b px-4 py-3 sm:px-6 sm:py-4" :class="dividerClass">
<div><h3 class="text-base font-semibold" :class="primaryTextClass">已删除文件</h3><p class="mt-0.5 text-xs" :class="mutedTextClass">物理清理只释放文件本体,文件记录和取件历史会持续保留。</p></div>
<span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-medium" :class="isDarkMode ? 'bg-[#263941] text-[#b8d6de]' : 'bg-[#e5f0f1] text-[#365a66]'"><ShieldCheckIcon class="mr-1.5 h-3.5 w-3.5" />审计保留</span>
</div>
<div v-if="isLoading" class="flex min-h-64 items-center justify-center text-sm" :class="mutedTextClass"><RefreshCwIcon class="mr-2 h-4 w-4 animate-spin" />正在加载回收站…</div>
<div v-else-if="errorText" class="px-4 py-12 text-center sm:px-6"><p class="text-sm font-medium text-red-600">{{ errorText }}</p><BaseButton class="mt-3" variant="secondary" @click="loadTrash">重新加载</BaseButton></div>
<div v-else-if="items.length === 0" class="px-4 py-14 text-center sm:px-6"><Trash2Icon class="mx-auto h-9 w-9" :class="mutedTextClass" /><p class="mt-3 text-sm" :class="mutedTextClass">回收站目前为空</p></div>
<div v-else class="divide-y" :class="dividerClass">
<article v-for="item in items" :key="item.id" class="grid gap-3 px-4 py-4 transition-colors sm:grid-cols-[auto_minmax(0,1fr)_auto] sm:items-center sm:px-6" :class="isDarkMode ? 'hover:bg-[#253941]/60' : 'hover:bg-[#f2f8f8]'">
<div class="flex h-10 w-10 shrink-0 items-center justify-center rounded-xl" :class="item.isTrashPurged ? (isDarkMode ? 'bg-[#30383c] text-[#aab7ba]' : 'bg-[#edf0f1] text-[#61747a]') : (isDarkMode ? 'bg-amber-950/45 text-amber-300' : 'bg-amber-50 text-amber-700')"><FileX2Icon class="h-5 w-5" /></div>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2"><p class="break-all text-sm font-semibold" :class="primaryTextClass">{{ fileName(item) }}</p><span class="rounded-full border px-2 py-0.5 text-[11px] font-medium" :class="badgeClass">{{ stateLabel(item) }}</span></div>
<div class="mt-1 flex flex-wrap gap-x-3 gap-y-1 text-xs" :class="mutedTextClass"><span>旧取件码 {{ item.code }}</span><span>{{ formatFileSize(Number(item.size || 0), 1) }}</span><span>移入 {{ formatTimestamp(item.trashedAt || '') }}</span></div>
<p class="mt-1 text-xs" :class="mutedTextClass">{{ expiryLabel(item) }}</p>
</div>
<div class="flex flex-wrap items-center gap-2 sm:justify-end">
<BaseButton v-if="!item.isTrashPurged" size="sm" variant="success" :loading="busyId === item.id && busyAction === 'restore'" @click="restore(item)"><template #icon><RotateCcwIcon class="mr-2 h-4 w-4" /></template>恢复</BaseButton>
<BaseButton v-if="!item.isTrashPurged" size="sm" variant="outline" :loading="busyId === item.id && busyAction === 'retention'" @click="makePermanent(item)"><template #icon><InfinityIcon class="mr-2 h-4 w-4" /></template>永久保留</BaseButton>
<BaseButton v-if="!item.isTrashPurged" size="sm" variant="danger" :loading="busyId === item.id && busyAction === 'purge'" @click="purge(item)"><template #icon><EraserIcon class="mr-2 h-4 w-4" /></template>物理清理</BaseButton>
</div>
</article>
</div>
<DataPagination v-if="total > 0" :current-page="page" :page-size="size" :total="total" @page-change="changePage" @page-size-change="changePageSize" />
</section>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { EraserIcon, FileX2Icon, InfinityIcon, RefreshCwIcon, RotateCcwIcon, ShieldCheckIcon, Trash2Icon } from 'lucide-vue-next'
import BaseButton from '@/components/common/BaseButton.vue'
import DataPagination from '@/components/common/DataPagination.vue'
import { useAdminTrash, useInjectedDarkMode } from '@/composables'
import type { TrashFileItem } from '@/types'
import { formatFileSize, formatTimestamp } from '@/utils/common'
const isDarkMode = useInjectedDarkMode()
const { fetchTrash, purgeTrashItem, restoreTrashItem, updateTrashRetention } = useAdminTrash()
const items = ref<TrashFileItem[]>([])
const page = ref(1)
const size = ref(20)
const total = ref(0)
const isLoading = ref(false)
const errorText = ref('')
const busyId = ref<number | null>(null)
const busyAction = ref('')
const primaryTextClass = computed(() => isDarkMode.value ? 'text-white' : 'text-[#1f3c45]')
const mutedTextClass = computed(() => isDarkMode.value ? 'text-[#a9bdc2]' : 'text-[#617980]')
const panelClass = computed(() => isDarkMode.value ? 'border-[#40545c] bg-[#202f36]/90' : 'border-[#d7e3e5] bg-[#f8fbfb]')
const dividerClass = computed(() => isDarkMode.value ? 'divide-[#40545c] border-[#40545c]' : 'divide-[#d7e3e5] border-[#d7e3e5]')
const badgeClass = computed(() => isDarkMode.value ? 'border-[#50666f] text-[#b8d6de]' : 'border-[#c8d8dc] text-[#4e6c75]')
const recoverableCount = computed(() => items.value.filter(item => !item.isTrashPurged).length)
const purgedCount = computed(() => items.value.filter(item => item.isTrashPurged).length)
const fileName = (item: TrashFileItem) => item.name || `${item.prefix || ''}${item.suffix || ''}` || '未命名文件'
const stateLabel = (item: TrashFileItem) => item.isTrashPurged ? '仅审计记录' : item.isTrashPermanent ? '永久保留' : '可恢复'
const expiryLabel = (item: TrashFileItem) => item.isTrashPurged ? `文件本体已于 ${formatTimestamp(item.trashPurgedAt || '')} 清理` : item.isTrashPermanent ? '不会自动清理文件本体' : `预计清理:${formatTimestamp(item.trashExpiresAt || '')}`
const loadTrash = async () => { isLoading.value = true; errorText.value = ''; try { const detail = await fetchTrash({ page: page.value, size: size.value }); items.value = detail?.data || []; total.value = Number(detail?.total || 0) } catch (error: unknown) { errorText.value = (error as { response?: { data?: { detail?: string } } })?.response?.data?.detail || '回收站加载失败,请稍后重试。' } finally { isLoading.value = false } }
const run = async (item: TrashFileItem, action: string, operation: () => Promise<unknown>) => { busyId.value = item.id; busyAction.value = action; try { await operation(); await loadTrash() } catch (error: unknown) { errorText.value = (error as { response?: { data?: { detail?: string } } })?.response?.data?.detail || '操作未完成,请稍后重试。' } finally { busyId.value = null; busyAction.value = '' } }
const restore = (item: TrashFileItem) => run(item, 'restore', () => restoreTrashItem(item.id))
const makePermanent = (item: TrashFileItem) => run(item, 'retention', () => updateTrashRetention(item.id, 0))
const purge = (item: TrashFileItem) => { if (!window.confirm(`确定物理清理“${fileName(item)}”吗?文件本体不可恢复,但审计记录会永久保留。`)) return; void run(item, 'purge', () => purgeTrashItem(item.id)) }
const changePage = (nextPage: number) => { page.value = nextPage; void loadTrash() }
const changePageSize = (nextSize: number) => { size.value = nextSize; page.value = 1; void loadTrash() }
void loadTrash()
</script>