数据结构优化

This commit is contained in:
李琦
2026-01-19 21:19:35 +08:00
parent 7e918cedd4
commit 356dcf4fa5
14 changed files with 557 additions and 45 deletions

View File

@@ -1,5 +1,6 @@
<template>
<div id="inquiry-modal" class="fixed inset-0 z-[100] bg-[#050505]/95 backdrop-blur-xl hidden items-center justify-center p-4 transition-opacity duration-300">
<Teleport to="body">
<div id="inquiry-modal" class="fixed inset-0 z-[100] bg-[#050505]/95 backdrop-blur-xl hidden items-center justify-center p-4 transition-opacity duration-300">
<div class="relative w-full max-w-4xl bg-[#080808] border border-white/10 overflow-hidden flex flex-col md:flex-row shadow-2xl">
<div class="hidden md:flex w-1/3 bg-[#0c0c0c] border-r border-white/5 p-12 flex-col justify-between relative overflow-hidden">
<div class="absolute top-0 left-0 w-full h-full bg-noise opacity-10"></div>
@@ -163,6 +164,7 @@
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">

View File

@@ -1,9 +1,10 @@
<template>
<div
v-if="isOpen"
class="fixed inset-0 z-[100] flex items-center justify-center p-4 md:p-10 transition-opacity duration-300 pointer-events-auto"
:class="{ 'opacity-0': isAnimating, 'opacity-100': !isAnimating }"
>
<Teleport to="body">
<div
v-if="isOpen"
class="fixed inset-0 z-[100] flex items-center justify-center p-4 md:p-10 transition-opacity duration-300 pointer-events-auto"
:class="{ 'opacity-0': isAnimating, 'opacity-100': !isAnimating }"
>
<div class="absolute inset-0 bg-black/90 backdrop-blur-sm" @click="close"></div>
<div
class="relative z-[101] bg-[#0a0a0c] border border-white/10 w-full max-w-6xl h-[80vh] rounded-2xl flex flex-col overflow-hidden shadow-2xl transform transition-transform duration-300"
@@ -70,6 +71,7 @@
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">

View File

@@ -1,5 +1,6 @@
<template>
<div v-if="show" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50" @click.self="handleClose">
<Teleport to="body">
<div v-if="show" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50" @click.self="handleClose">
<div class="admin-card max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto">
<div class="flex items-center justify-between mb-6">
<h2 class="text-xl font-serif italic text-white">附件详情</h2>
@@ -90,6 +91,7 @@
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">

View File

@@ -0,0 +1,297 @@
<template>
<Teleport to="body">
<div v-if="show" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50" @click.self="handleClose">
<div class="admin-card max-w-4xl w-full mx-4 max-h-[90vh] flex flex-col">
<!-- 标题栏 -->
<div class="flex items-center justify-between mb-4 pb-4 border-b border-white/10">
<h2 class="text-xl font-serif italic text-white">从素材库选择</h2>
<button @click="handleClose" class="text-white/40 hover:text-white transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
</div>
<!-- 筛选栏 -->
<div class="mb-4 space-y-3">
<div class="flex gap-3">
<!-- 搜索框 -->
<div class="flex-1">
<input
v-model="searchKeyword"
@input="handleSearch"
type="text"
placeholder="搜索文件名..."
class="admin-input w-full"
/>
</div>
<!-- 分类筛选 -->
<div class="w-48">
<CustomSelect
v-model="filterCategoryId"
:options="categoryOptions"
placeholder="全部分类"
@update:modelValue="loadAttachments"
/>
</div>
</div>
<!-- 已选数量提示多选模式 -->
<div v-if="multiple" class="text-sm text-art-muted">
已选择 {{ selectedUrls.length }} / {{ maxCount || '' }} 张图片
</div>
</div>
<!-- 图片网格 -->
<div class="flex-1 overflow-y-auto mb-4">
<div v-if="loading" class="flex justify-center items-center h-64">
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-art-accent"></div>
</div>
<div v-else-if="attachments.length === 0" class="text-center py-20 text-art-muted">
<p>暂无图片</p>
</div>
<div v-else class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-4">
<div
v-for="attachment in attachments"
:key="attachment.id"
@click="toggleSelect(attachment)"
class="relative group cursor-pointer"
:class="[
isSelected(attachment.fileUrl)
? 'ring-2 ring-art-accent'
: 'hover:ring-2 hover:ring-white/20'
]"
>
<div class="aspect-square rounded-lg overflow-hidden bg-white/5 border border-white/10 transition-all"
:class="isSelected(attachment.fileUrl) ? 'border-art-accent bg-art-accent/10' : ''">
<img
:src="attachment.fileUrl"
:alt="attachment.originalName"
class="w-full h-full object-cover"
/>
<!-- 选中标记 -->
<div v-if="isSelected(attachment.fileUrl)" class="absolute top-2 right-2 w-6 h-6 bg-art-accent rounded-full flex items-center justify-center">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="text-black">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
</div>
<!-- 文件名提示 -->
<div class="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center p-2">
<p class="text-xs text-white text-center truncate w-full">{{ attachment.originalName }}</p>
</div>
</div>
</div>
</div>
</div>
<!-- 分页 -->
<div v-if="totalPages > 1" class="flex items-center justify-between mb-4 pt-4 border-t border-white/10">
<div class="text-sm text-art-muted">
{{ total }} 张图片 {{ currentPage }} / {{ totalPages }}
</div>
<div class="flex gap-2">
<button
@click="goToPage(currentPage - 1)"
:disabled="currentPage === 1"
class="admin-btn-secondary text-sm px-3 py-1 disabled:opacity-50 disabled:cursor-not-allowed"
>
上一页
</button>
<button
@click="goToPage(currentPage + 1)"
:disabled="currentPage === totalPages"
class="admin-btn-secondary text-sm px-3 py-1 disabled:opacity-50 disabled:cursor-not-allowed"
>
下一页
</button>
</div>
</div>
<!-- 底部操作栏 -->
<div class="flex items-center justify-between pt-4 border-t border-white/10">
<div v-if="multiple" class="text-sm text-art-muted">
已选择 {{ selectedUrls.length }} 张图片
</div>
<div v-else></div>
<div class="flex gap-3">
<button @click="handleClose" class="admin-btn-secondary">
取消
</button>
<button
@click="handleConfirm"
:disabled="selectedUrls.length === 0"
class="admin-btn-primary disabled:opacity-50 disabled:cursor-not-allowed"
>
确认选择 ({{ selectedUrls.length }})
</button>
</div>
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { useToast } from '../../composables/useToast'
import { getAdminAttachments, getAttachmentCategories, type Attachment, type AttachmentCategory } from '../../services/api'
import CustomSelect from '../CustomSelect.vue'
const props = withDefaults(defineProps<{
show: boolean
multiple?: boolean
maxCount?: number
selectedUrls?: string[]
}>(), {
multiple: false,
selectedUrls: () => []
})
const emit = defineEmits<{
'update:show': [value: boolean]
'confirm': [urls: string[]]
}>()
const toast = useToast()
const attachments = ref<Attachment[]>([])
const categories = ref<AttachmentCategory[]>([])
const loading = ref(false)
const searchKeyword = ref('')
const filterCategoryId = ref(0)
const currentPage = ref(1)
const pageSize = ref(24)
const total = ref(0)
const selectedUrls = ref<string[]>([...props.selectedUrls])
const totalPages = computed(() => Math.ceil(total.value / pageSize.value))
const categoryOptions = computed(() => {
return [
{ value: 0, label: '全部分类' },
...categories.value.map(cat => ({ value: cat.id, label: cat.name }))
]
})
// 搜索防抖
let searchTimer: ReturnType<typeof setTimeout> | null = null
const handleSearch = () => {
if (searchTimer) clearTimeout(searchTimer)
searchTimer = setTimeout(() => {
currentPage.value = 1
loadAttachments()
}, 300)
}
const loadAttachments = async () => {
loading.value = true
try {
const result = await getAdminAttachments({
page: currentPage.value,
pageSize: pageSize.value,
categoryId: filterCategoryId.value > 0 ? filterCategoryId.value : undefined,
fileType: 'image',
keyword: searchKeyword.value || undefined
})
attachments.value = result.list
total.value = result.total
} catch (err: any) {
toast.showToast(err.message || '加载附件失败', 'error')
} finally {
loading.value = false
}
}
const loadCategories = async () => {
try {
categories.value = await getAttachmentCategories()
} catch (err) {
console.error('Failed to load categories:', err)
}
}
const goToPage = (page: number) => {
if (page < 1 || page > totalPages.value) return
currentPage.value = page
loadAttachments()
}
const isSelected = (url: string): boolean => {
return selectedUrls.value.includes(url)
}
const toggleSelect = (attachment: Attachment) => {
const url = attachment.fileUrl
const index = selectedUrls.value.indexOf(url)
if (props.multiple) {
// 多选模式
if (index > -1) {
// 取消选择
selectedUrls.value.splice(index, 1)
} else {
// 检查是否超过最大数量
if (props.maxCount && selectedUrls.value.length >= props.maxCount) {
toast.showToast(`最多只能选择 ${props.maxCount} 张图片`, 'error')
return
}
selectedUrls.value.push(url)
}
} else {
// 单选模式
if (index > -1) {
// 取消选择
selectedUrls.value = []
} else {
// 选择
selectedUrls.value = [url]
}
}
}
const handleClose = () => {
emit('update:show', false)
}
const handleConfirm = () => {
if (selectedUrls.value.length === 0) {
toast.showToast('请至少选择一张图片', 'error')
return
}
emit('confirm', [...selectedUrls.value])
emit('update:show', false)
}
// 监听show变化重置状态
watch(() => props.show, (newVal) => {
if (newVal) {
// 打开模态框时,重置搜索和筛选,但保持已选状态
searchKeyword.value = ''
filterCategoryId.value = 0
currentPage.value = 1
selectedUrls.value = [...props.selectedUrls]
loadAttachments()
}
})
// 监听selectedUrls prop变化
watch(() => props.selectedUrls, (newVal) => {
if (props.show) {
selectedUrls.value = [...newVal]
}
}, { deep: true })
onMounted(() => {
loadCategories()
if (props.show) {
loadAttachments()
}
})
</script>

View File

@@ -37,6 +37,18 @@
</div>
</div>
<!-- 从素材库选择按钮 -->
<div v-if="!imageUrl" class="mt-3 text-center">
<button
@click="openLibraryModal"
type="button"
class="text-sm text-art-accent hover:text-art-accent/80 transition-colors"
:disabled="disabled"
>
或从素材库选择
</button>
</div>
<div v-else class="image-preview relative group">
<img :src="imageUrl" alt="Preview" class="w-full h-auto rounded-lg max-h-64 object-contain bg-white/5" />
<div class="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity rounded-lg flex items-center justify-center gap-2">
@@ -132,6 +144,18 @@
</p>
</div>
</div>
<!-- 从素材库选择按钮 -->
<div v-if="imageUrls.length < (maxCount || Infinity)" class="mt-3 text-center">
<button
@click="openLibraryModal"
type="button"
class="text-sm text-art-accent hover:text-art-accent/80 transition-colors"
:disabled="disabled"
>
或从素材库选择
</button>
</div>
<!-- 上传进度 -->
<div v-if="uploadingCount > 0" class="mt-4 text-center">
@@ -147,6 +171,16 @@
</div>
</div>
</template>
<!-- 素材库选择模态框 -->
<AttachmentLibraryModal
:show="showLibraryModal"
:multiple="multiple"
:max-count="maxCount"
:selected-urls="multiple ? imageUrls : (imageUrl ? [imageUrl] : [])"
@update:show="showLibraryModal = $event"
@confirm="handleLibrarySelect"
/>
</div>
</template>
@@ -154,6 +188,7 @@
import { ref, watch, computed } from 'vue'
import { useToast } from '../../composables/useToast'
import { API_BASE } from '../../services/api'
import AttachmentLibraryModal from './AttachmentLibraryModal.vue'
const props = withDefaults(defineProps<{
modelValue?: string | string[]
@@ -177,6 +212,7 @@ const uploading = ref(false)
const uploadingCount = ref(0)
const error = ref('')
const isDragging = ref(false)
const showLibraryModal = ref(false)
const toast = useToast()
@@ -365,6 +401,36 @@ const removeImage = (index?: number) => {
}
}
}
const openLibraryModal = () => {
if (props.disabled) return
showLibraryModal.value = true
}
const handleLibrarySelect = (urls: string[]) => {
if (props.multiple) {
// 批量模式添加选中的图片考虑maxCount限制
const currentCount = imageUrls.value.length
const remainingCount = props.maxCount ? props.maxCount - currentCount : Infinity
const urlsToAdd = urls.slice(0, remainingCount)
if (urlsToAdd.length > 0) {
const newUrls = [...imageUrls.value, ...urlsToAdd]
imageUrls.value = newUrls
toast.showToast(`已添加 ${urlsToAdd.length} 张图片`, 'success')
}
if (urls.length > urlsToAdd.length) {
toast.showToast(`最多只能添加 ${props.maxCount} 张图片,已添加 ${urlsToAdd.length}`, 'error')
}
} else {
// 单个模式:直接设置选中的第一张图片
if (urls.length > 0) {
imageUrl.value = urls[0]
toast.showToast('图片选择成功', 'success')
}
}
}
</script>
<style scoped>

View File

@@ -1,5 +1,6 @@
<template>
<div v-if="isOpen" class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm" @click.self="close">
<Teleport to="body">
<div v-if="isOpen" class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm" @click.self="close">
<div class="bg-[#1a1a1a] border border-white/10 rounded-lg shadow-xl w-full max-w-2xl max-h-[90vh] overflow-hidden animate-reveal flex flex-col">
<div class="p-4 border-b border-white/10 flex justify-between items-center shrink-0">
<h3 class="text-lg font-serif italic text-white">管理文章关联</h3>
@@ -131,6 +132,7 @@
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">

View File

@@ -119,7 +119,7 @@ const latestWork = ref<Work>({
desc: '',
techStack: [],
gallery: [],
links: { live: '#' },
links: {},
next: ''
})

View File

@@ -67,15 +67,48 @@
</div>
</div>
</div>
<div class="mt-12">
<div class="mt-12 space-y-4">
<!-- Live Link -->
<a
v-if="work.links.live"
id="work-link-live"
:href="work.links.live"
target="_blank"
class="group flex items-center justify-between w-full py-6 border-t border-white/10 hover:bg-white/5 transition-colors"
class="group flex items-center justify-between w-full py-4 border-t border-white/10 hover:bg-white/5 transition-colors"
>
<span class="font-serif text-2xl italic text-white group-hover:text-art-accent transition-colors">访问线上项目</span>
<i data-lucide="arrow-up-right" class="w-6 h-6 text-white group-hover:rotate-45 transition-transform"></i>
<span class="font-serif text-xl italic text-white group-hover:text-art-accent transition-colors flex items-center gap-3">
<i data-lucide="globe" class="w-5 h-5"></i>
访问线上项目
</span>
<i data-lucide="arrow-up-right" class="w-5 h-5 text-white group-hover:rotate-45 transition-transform"></i>
</a>
<!-- Demo Link -->
<a
v-if="work.links.demo"
id="work-link-demo"
:href="work.links.demo"
target="_blank"
class="group flex items-center justify-between w-full py-4 border-t border-white/10 hover:bg-white/5 transition-colors"
>
<span class="font-serif text-xl italic text-white group-hover:text-art-accent transition-colors flex items-center gap-3">
<i data-lucide="play-circle" class="w-5 h-5"></i>
查看演示
</span>
<i data-lucide="arrow-up-right" class="w-5 h-5 text-white group-hover:rotate-45 transition-transform"></i>
</a>
<!-- GitHub Link -->
<a
v-if="work.links.github"
id="work-link-github"
:href="work.links.github"
target="_blank"
class="group flex items-center justify-between w-full py-4 border-t border-white/10 hover:bg-white/5 transition-colors"
>
<span class="font-serif text-xl italic text-white group-hover:text-art-accent transition-colors flex items-center gap-3">
<i data-lucide="github" class="w-5 h-5"></i>
查看源码
</span>
<i data-lucide="arrow-up-right" class="w-5 h-5 text-white group-hover:rotate-45 transition-transform"></i>
</a>
</div>
</div>
@@ -133,7 +166,7 @@ const work = ref<Work>({
desc: '<p>加载中...</p>',
techStack: [],
gallery: [],
links: { live: '#' },
links: {},
next: ''
})

View File

@@ -68,6 +68,39 @@
{{ stack }}
</span>
</div>
<!-- 链接按钮 -->
<div v-if="work.links && (work.links.live || work.links.github || work.links.demo)" class="flex flex-wrap gap-3" :class="[index % 2 === 1 ? 'md:justify-end' : '']">
<a
v-if="work.links.live"
:href="work.links.live"
target="_blank"
@click.stop
class="inline-flex items-center gap-2 px-4 py-2 border border-white/10 rounded-full text-xs text-white/70 hover:border-art-accent hover:text-art-accent transition-all"
>
<i data-lucide="globe" class="w-3 h-3"></i>
线上项目
</a>
<a
v-if="work.links.demo"
:href="work.links.demo"
target="_blank"
@click.stop
class="inline-flex items-center gap-2 px-4 py-2 border border-white/10 rounded-full text-xs text-white/70 hover:border-art-accent hover:text-art-accent transition-all"
>
<i data-lucide="play-circle" class="w-3 h-3"></i>
演示
</a>
<a
v-if="work.links.github"
:href="work.links.github"
target="_blank"
@click.stop
class="inline-flex items-center gap-2 px-4 py-2 border border-white/10 rounded-full text-xs text-white/70 hover:border-art-accent hover:text-art-accent transition-all"
>
<i data-lucide="github" class="w-3 h-3"></i>
源码
</a>
</div>
<!-- 查看详情按钮 -->
<div class="pt-4" :class="[index % 2 === 1 ? 'flex md:justify-end' : '']">
<span class="inline-flex items-center gap-2 text-sm font-medium text-white border-b border-transparent hover:border-art-accent hover:text-art-accent transition-all">

View File

@@ -50,7 +50,7 @@ export interface Work {
desc: string
techStack: { category: string; items: string[] }[]
gallery: string[]
links: { live: string }
links: { live?: string; github?: string; demo?: string }
next: string
}
@@ -405,7 +405,7 @@ export const fetchWorks = async (): Promise<Work[]> => {
desc: work.desc || '',
techStack: Array.isArray(work.techStack) ? work.techStack : [],
gallery: Array.isArray(work.gallery) ? work.gallery : [],
links: work.links || { live: '' },
links: work.links || {},
next: work.next || ''
})) as Work[]
} catch (error) {
@@ -443,7 +443,7 @@ export const fetchWork = async (id: string): Promise<Work> => {
desc: '',
techStack: [],
gallery: [],
links: { live: '#' },
links: {},
next: ''
}
}
@@ -1657,6 +1657,42 @@ export interface AttachmentCategory {
}
// 附件管理API
export const getAdminAttachments = async (params?: {
page?: number
pageSize?: number
categoryId?: number
fileType?: string
keyword?: string
}): Promise<{ list: Attachment[], total: number, page: number, size: number }> => {
try {
const queryParams = new URLSearchParams()
if (params?.page) queryParams.append('page', params.page.toString())
if (params?.pageSize) queryParams.append('pageSize', params.pageSize.toString())
if (params?.categoryId) queryParams.append('categoryId', params.categoryId.toString())
if (params?.fileType) queryParams.append('fileType', params.fileType)
if (params?.keyword) queryParams.append('keyword', params.keyword)
const url = `${API_BASE}/admin/attachments${queryParams.toString() ? '?' + queryParams.toString() : ''}`
const response = await fetch(url, {
headers: getAuthHeaders()
})
if (!response.ok) {
const errorData = await response.json()
throw new Error(errorData.message || '获取附件列表失败')
}
const data = await response.json()
return {
list: data.result?.list || [],
total: data.result?.total || 0,
page: data.result?.page || 1,
size: data.result?.size || 20
}
} catch (error) {
console.error('Get admin attachments error:', error)
throw error
}
}
export const updateAttachment = async (id: number, data: { categoryId?: number | null }): Promise<Attachment> => {
try {
const response = await fetch(`${API_BASE}/admin/attachments/${id}`, {