Files
filecodebox/src/views/manage/TrashView.vue
T

128 lines
11 KiB
Vue
Raw 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.
<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>
<div class="flex flex-wrap items-center gap-2">
<BaseButton variant="secondary" @click="openRetentionSettings">
<template #icon><Settings2Icon class="mr-2 h-4 w-4" /></template>
配置默认清理时间
</BaseButton>
<BaseButton variant="secondary" :loading="isLoading" :disabled="busyId !== null" @click="loadTrash">
<template #icon><RefreshCwIcon class="mr-2 h-4 w-4" /></template>
刷新回收站
</BaseButton>
</div>
</header>
<section class="mb-5 grid grid-cols-3 gap-2 sm:mb-6 sm:gap-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-[11px] leading-4 sm:text-xs" :class="mutedTextClass">全部记录</p><strong class="mt-1 block text-xl sm: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-[11px] leading-4 sm:text-xs" :class="mutedTextClass">本页可恢复</p><strong class="mt-1 block text-xl sm: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-[11px] leading-4 sm:text-xs" :class="mutedTextClass">本页仅审计</p><strong class="mt-1 block text-xl sm: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 && items.length === 0" 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-if="errorText && items.length > 0" class="border-b px-4 py-3 text-sm text-red-600 sm:px-6" :class="dividerClass">{{ errorText }}</div>
<div v-if="!errorText && 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-if="!errorText && items.length > 0" class="divide-y" :class="dividerClass">
<article
v-for="item in items"
:key="item.id"
class="fcb-interactive-row group grid gap-3 px-4 py-4 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>
<BaseModal :show="showPurgeModal" title="确认物理清理" @close="closePurgeModal">
<p class="text-sm" :class="mutedTextClass">确定清理“{{ pendingPurge ? fileName(pendingPurge) : '' }}”吗?文件本体不可恢复,但审计记录会永久保留。</p>
<template #footer><BaseButton variant="secondary" @click="closePurgeModal">取消</BaseButton><BaseButton variant="danger" :loading="busyAction === 'purge'" @click="confirmPurge">确认物理清理</BaseButton></template>
</BaseModal>
<BaseModal :show="Boolean(restoredCode)" title="文件已恢复" @close="restoredCode = ''">
<p class="text-sm" :class="mutedTextClass">恢复成功,新的取件码:</p>
<div class="mt-3 flex items-center justify-between gap-3 rounded-xl border px-4 py-3" :class="panelClass"><strong class="text-xl tracking-[0.2em]" :class="primaryTextClass">{{ restoredCode }}</strong><BaseButton size="sm" variant="outline" @click="copyCode">复制</BaseButton></div>
</BaseModal>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { EraserIcon, FileX2Icon, InfinityIcon, RefreshCwIcon, RotateCcwIcon, Settings2Icon, ShieldCheckIcon, Trash2Icon } from 'lucide-vue-next'
import BaseButton from '@/components/common/BaseButton.vue'
import BaseModal from '@/components/common/BaseModal.vue'
import DataPagination from '@/components/common/DataPagination.vue'
import { useAdminTrash, useInjectedDarkMode } from '@/composables'
import type { TrashFileItem } from '@/types'
import { formatFileSize, formatTimestamp } from '@/utils/common'
import { copyRetrieveCode } from '@/utils/clipboard'
const isDarkMode = useInjectedDarkMode()
const router = useRouter()
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 showPurgeModal = ref(false)
const pendingPurge = ref<TrashFileItem | null>(null)
const restoredCode = 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 openRetentionSettings = () => { void router.push({ path: '/admin/settings', hash: '#settings-upload' }) }
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>) => { if (busyId.value !== null) return; busyId.value = item.id; busyAction.value = action; errorText.value = ''; try { const response = await operation(); if (action === 'restore') restoredCode.value = String((response as { detail?: { code?: string } })?.detail?.code || ''); 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) => void run(item, 'restore', () => restoreTrashItem(item.id))
const makePermanent = (item: TrashFileItem) => run(item, 'retention', () => updateTrashRetention(item.id, 0))
const purge = (item: TrashFileItem) => { if (busyId.value === null) { pendingPurge.value = item; showPurgeModal.value = true } }
const closePurgeModal = () => { if (busyId.value === null) { showPurgeModal.value = false; pendingPurge.value = null } }
const confirmPurge = () => { if (!pendingPurge.value) return; const item = pendingPurge.value; closePurgeModal(); void run(item, 'purge', () => purgeTrashItem(item.id)) }
const copyCode = () => { if (restoredCode.value) void copyRetrieveCode(restoredCode.value) }
const changePage = (nextPage: number) => { page.value = nextPage; void loadTrash() }
const changePageSize = (nextSize: number) => { size.value = nextSize; page.value = 1; void loadTrash() }
void loadTrash()
</script>