Files
arcticnewone/app/pages/admin/uploads.vue
T
virtheart 5a7ad8de11 feat: 全面升级——后台优化 / 动态配置 / 个人中心 / OSS 存储
- OSS 文件存储(RustFS / S3 v4 兼容,零依赖手写 SigV4):
  上传/列表/删除走桶 arcticnewone,/uploads 代理服务 + 本地回退;
  server/utils/sigv4.ts 纯函数 + AWS 测试向量验证
- 业务动态配置:settings 表 + /api/settings(公开) + /api/admin/settings(PATCH),
  useSiteSettings 组合式,footer/contact/tools/app.vue/侧边栏/工具页全部接入,
  后台 /admin/settings 修改前台即时生效(站点/联系/社交/工具箱/页脚/SEO)
- 后台全面优化:Admin* 设计系统(按钮/输入/表单/卡片/徽标/弹窗/确认/空态/骨架/
  分页/页头/Toast)14 组件;Dashboard 增强;各列表页搜索/筛选/批量删除/分页;
  布局与侧边栏分组;统一 toast/确认反馈
- 个人中心全新重设计:顶部资料卡 + 统计、精致化改密/删号(二次确认)、
  收藏封面网格、评论卡片、移动端适配
- deploy.sh 支持 OSS 凭据注入(NUXT_OSS_*);.env.example 更新
- 测试 17 例全过(含 SigV4 AWS 向量、settings 默认结构)
2026-08-06 18:40:17 +08:00

226 lines
7.5 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.
<script setup lang="ts">
/**
* 后台图片管理:网格 + 搜索 + 存储模式标识 + 删除确认 + 前端分页。
* - GET /api/admin/uploads → { files, storage };DELETE /api/admin/uploads?name=<key>
*/
import { computed, onMounted, ref, watch } from 'vue'
import { useToastStore } from '~/stores/toast'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: '图片管理' })
interface UploadFile {
name: string
size: number
modified: string
url: string
}
const toast = useToastStore()
/* ---------- 列表 ---------- */
const files = ref<UploadFile[]>([])
const storage = ref<'local' | 'oss' | ''>('')
const loading = ref(true)
const loadError = ref('')
const loadFiles = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ files: UploadFile[]; storage: 'local' | 'oss' }>('/api/admin/uploads')
files.value = data.files
storage.value = data.storage
} catch (err) {
console.error('load uploads error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
onMounted(loadFiles)
/* ---------- 搜索 / 分页 ---------- */
const keyword = ref('')
const filteredFiles = computed(() => {
const kw = keyword.value.trim().toLowerCase()
if (!kw) return files.value
return files.value.filter((f) => f.name.toLowerCase().includes(kw))
})
const pageSize = 12
const page = ref(1)
const totalPages = computed(() => Math.max(1, Math.ceil(filteredFiles.value.length / pageSize)))
const pagedFiles = computed(() => {
const p = Math.min(page.value, totalPages.value)
const start = (p - 1) * pageSize
return filteredFiles.value.slice(start, start + pageSize)
})
watch(keyword, () => {
page.value = 1
})
const changePage = (p: number) => {
page.value = Math.min(Math.max(1, p), totalPages.value)
}
const totalSize = computed(() => files.value.reduce((sum, f) => sum + f.size, 0))
/* ---------- 删除 ---------- */
const confirmDelete = ref<UploadFile | null>(null)
const deleting = ref(false)
const askDelete = (f: UploadFile) => {
confirmDelete.value = f
}
const doDelete = async () => {
const f = confirmDelete.value
if (!f) return
deleting.value = true
try {
await $fetch('/api/admin/uploads', { method: 'DELETE', query: { name: f.name } })
files.value = files.value.filter((x) => x.name !== f.name)
toast.success('已删除;如被内容引用请同步更新')
confirmDelete.value = null
} catch (err: any) {
toast.error(err?.data?.message || '删除失败,请重试')
} finally {
deleting.value = false
}
}
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)
}
</script>
<template>
<div>
<AdminPageHeader title="图片管理" subtitle="站点上传的图片文件,删除前请确认未被动态/作品引用。">
<template #actions>
<AdminButton variant="secondary" size="sm" @click="loadFiles">
<Icon name="ph:arrows-clockwise" 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">共 {{ files.length }} 张图片</span>
<span class="text-sm text-gray-400">· 共 {{ formatSize(totalSize) }}</span>
<AdminBadge :tone="storage === 'oss' ? 'blue' : 'gray'">
<Icon :name="storage === 'oss' ? 'ph:cloud' : 'ph:hard-drives'" class="w-3.5 h-3.5" />
{{ storage === 'oss' ? 'OSS 存储' : '本地存储' }}
</AdminBadge>
</div>
<div class="mb-5">
<div class="relative flex-1 sm: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>
</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="loadFiles">重新加载</AdminButton>
</div>
</AdminCard>
<!-- 空态 -->
<AdminEmptyState v-else-if="!files.length" icon="ph:image" title="暂无图片" description="通过动态/作品的图片上传功能产生的文件会出现在这里。" />
<AdminEmptyState
v-else-if="!filteredFiles.length"
icon="ph:magnifying-glass"
title="没有匹配的图片"
description="换个文件名关键词试试。"
/>
<!-- 图片网格 -->
<template v-else>
<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
<div
v-for="f in pagedFiles"
: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="askDelete(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>
<div class="mt-4">
<AdminPagination :total="filteredFiles.length" :page="page" :page-size="pageSize" @page="changePage" />
</div>
</template>
<!-- 删除确认 -->
<AdminConfirm
:open="!!confirmDelete"
:loading="deleting"
danger
:message="`确定删除图片「${confirmDelete?.name}」吗?此操作不可恢复。`"
@update:open="confirmDelete = null"
@confirm="doDelete"
/>
</div>
</template>