mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 08:29:20 +08:00
refact: 重构目录架构
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<script lang="ts" setup>
|
||||
import { Card, Image, Tag, Button, Space, Popconfirm } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import type { ImageVO } from '#/api/media/image/types';
|
||||
|
||||
// 创建图标组件
|
||||
const DownloadIcon = createIconifyIcon('lucide:download');
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
|
||||
interface Props {
|
||||
image: ImageVO.Info;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'download', image: ImageVO.Info): void;
|
||||
(e: 'edit', image: ImageVO.Info): void;
|
||||
(e: 'delete', id: number): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const handleDownload = (image: ImageVO.Info) => {
|
||||
emit('download', image);
|
||||
};
|
||||
|
||||
const handleEdit = (image: ImageVO.Info) => {
|
||||
emit('edit', image);
|
||||
};
|
||||
|
||||
const handleDelete = (id: number) => {
|
||||
emit('delete', id);
|
||||
};
|
||||
|
||||
const getTagColor = (category: string | undefined) => {
|
||||
switch (category) {
|
||||
case 'image':
|
||||
return 'blue';
|
||||
case 'avatar':
|
||||
return 'green';
|
||||
case 'logo':
|
||||
return 'purple';
|
||||
default:
|
||||
return '#006BE6';
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Card
|
||||
hoverable
|
||||
:body-style="{ padding: '8px' }"
|
||||
class="image-card"
|
||||
>
|
||||
<div class="relative overflow-hidden">
|
||||
<Image
|
||||
:src="image.url"
|
||||
:alt="image.filename"
|
||||
class="w-full h-32 object-cover rounded"
|
||||
:preview="true"
|
||||
/>
|
||||
<div v-if="image.tags" class="absolute top-2 right-2 max-w-[calc(100%-16px)]">
|
||||
<Tag
|
||||
:color="getTagColor(image.category)"
|
||||
class="max-w-full truncate"
|
||||
:title="image.tags"
|
||||
>
|
||||
{{ image.tags }}
|
||||
</Tag>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
<div class="text-sm font-medium truncate" :title="image.filename">
|
||||
{{ image.filename || '未命名' }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-500 mt-1">
|
||||
{{ image.uploadTime }}
|
||||
</div>
|
||||
<div class="flex justify-between items-center mt-2">
|
||||
<Space size="small">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleDownload(image)"
|
||||
>
|
||||
<DownloadIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleEdit(image)"
|
||||
>
|
||||
<EditIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定要删除这张图片吗?"
|
||||
@confirm="handleDelete(image.id)"
|
||||
>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
danger
|
||||
>
|
||||
<TrashIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.image-card {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.image-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,106 @@
|
||||
<template>
|
||||
<Modal
|
||||
v-model:open="visible"
|
||||
title="编辑图片信息"
|
||||
@ok="handleOk"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="标签" name="tags">
|
||||
<Input
|
||||
v-model:value="formData.tags"
|
||||
placeholder="请输入标签,多个标签用逗号分隔"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="说明" name="remark">
|
||||
<Input.TextArea
|
||||
v-model:value="formData.remark"
|
||||
placeholder="请输入图片说明"
|
||||
:rows="3"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from 'vue'
|
||||
import { Modal, Form, Input, message } from 'ant-design-vue'
|
||||
import { updateImageApi } from '#/api/media/image'
|
||||
import type { ImageDTO, ImageVO } from '#/api/media/image/types'
|
||||
|
||||
defineOptions({ name: 'ImageEditModal' })
|
||||
|
||||
// Props
|
||||
interface Props {
|
||||
open: boolean
|
||||
imageData?: ImageVO.Info | null
|
||||
}
|
||||
|
||||
// Emits
|
||||
interface Emits {
|
||||
(e: 'update:open', value: boolean): void
|
||||
(e: 'success'): void
|
||||
(e: 'cancel'): void
|
||||
}
|
||||
|
||||
const props = defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
// 响应式数据
|
||||
const visible = ref(false)
|
||||
const formRef = ref()
|
||||
const formData = ref<ImageDTO.Update>({
|
||||
tags: '',
|
||||
remark: '',
|
||||
})
|
||||
|
||||
|
||||
// 监听props变化
|
||||
watch(() => props.open, (newVal) => {
|
||||
visible.value = newVal
|
||||
if (newVal && props.imageData) {
|
||||
// 填充表单数据
|
||||
formData.value = {
|
||||
tags: props.imageData.tags,
|
||||
remark: props.imageData.remark,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 监听visible变化,同步到父组件
|
||||
watch(visible, (newVal) => {
|
||||
emit('update:open', newVal)
|
||||
})
|
||||
|
||||
// 确认操作
|
||||
const handleOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
if (props.imageData?.id) {
|
||||
await updateImageApi(props.imageData.id, formData.value)
|
||||
message.success('更新成功')
|
||||
emit('success')
|
||||
visible.value = false
|
||||
}
|
||||
} catch (error) {
|
||||
message.error('更新失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 取消操作
|
||||
const handleCancel = () => {
|
||||
emit('cancel')
|
||||
visible.value = false
|
||||
// 重置表单数据
|
||||
formData.value = {
|
||||
tags: '',
|
||||
remark: '',
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,272 @@
|
||||
<template>
|
||||
<div class="image-grid-view h-full">
|
||||
<!-- 工具栏 -->
|
||||
<div class="mb-4 flex justify-between items-center">
|
||||
<div class="flex items-center gap-4">
|
||||
<Upload v-bind="uploadProps">
|
||||
<Button type="primary" :loading="uploadLoading">
|
||||
<UploadIcon class="w-4 h-4 mr-2" />
|
||||
上传图片
|
||||
</Button>
|
||||
</Upload>
|
||||
<span class="text-gray-500">支持批量上传,单个图片最大10MB</span>
|
||||
</div>
|
||||
<div class="text-gray-500">
|
||||
共 {{ total }} 张图片
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 搜索区域 -->
|
||||
<div class="mb-4 p-4 bg-white rounded-lg shadow-sm">
|
||||
<Form :model="searchForm" layout="inline">
|
||||
<Form.Item label="文件名">
|
||||
<Input
|
||||
v-model:value="searchForm.filename"
|
||||
placeholder="请输入文件名"
|
||||
style="width: 200px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="图片分类">
|
||||
<Select
|
||||
v-model:value="searchForm.category"
|
||||
placeholder="请选择图片分类"
|
||||
style="width: 150px"
|
||||
allow-clear
|
||||
>
|
||||
<Select.Option
|
||||
v-for="option in categoryOptions"
|
||||
:key="option.value"
|
||||
:value="option.value"
|
||||
>
|
||||
{{ option.label }}
|
||||
</Select.Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
<!-- 网格内容 -->
|
||||
<div class="bg-white rounded-lg shadow-sm p-4">
|
||||
<Row :gutter="[16, 16]" v-loading="loading">
|
||||
<Col
|
||||
v-for="image in imageList"
|
||||
:key="image.id"
|
||||
:xs="12"
|
||||
:sm="8"
|
||||
:md="6"
|
||||
:lg="4"
|
||||
:xl="3"
|
||||
>
|
||||
<ImageCard
|
||||
:image="image"
|
||||
@download="handleDownload"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
@preview="handlePreview"
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<!-- 空状态 -->
|
||||
<div v-if="!loading && imageList.length === 0" class="text-center py-12">
|
||||
<div class="text-gray-400 text-lg">暂无图片数据</div>
|
||||
<div class="text-gray-400 text-sm mt-2">请上传图片或调整搜索条件</div>
|
||||
</div>
|
||||
|
||||
<!-- 分页 -->
|
||||
<div v-if="total > 0" class="mt-4 flex justify-center">
|
||||
<Pagination
|
||||
v-model:current="current"
|
||||
v-model:page-size="pageSize"
|
||||
:total="total"
|
||||
:page-size-options="['12', '24', '48', '96']"
|
||||
show-size-changer
|
||||
show-quick-jumper
|
||||
:show-total="(total, range) => `第 ${range[0]}-${range[1]} 条,共 ${total} 条`"
|
||||
@change="handlePageChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from 'vue'
|
||||
import { Form, Input, Select, Button, Space, Row, Col, Pagination, Upload, message } from 'ant-design-vue'
|
||||
import { createIconifyIcon } from '@vben/icons'
|
||||
import { getImagePageApi, uploadImageApi, batchUploadImagesApi } from '#/api/media/image'
|
||||
import type { ImageVO, ImageDTO } from '#/api/media/image/types'
|
||||
import type { PageQueryDTO } from '#/types/common'
|
||||
import ImageCard from './ImageCard.vue'
|
||||
|
||||
// 创建图标组件
|
||||
const UploadIcon = createIconifyIcon('lucide:upload')
|
||||
|
||||
defineOptions({ name: 'ImageGridView' })
|
||||
|
||||
// Props
|
||||
interface Props {
|
||||
searchForm: {
|
||||
filename: string
|
||||
category?: string
|
||||
}
|
||||
categoryOptions: Array<{ label: string; value: string }>
|
||||
}
|
||||
|
||||
// Emits
|
||||
interface Emits {
|
||||
(e: 'edit', image: ImageVO.Info): void
|
||||
(e: 'delete', id: number): void
|
||||
(e: 'download', image: ImageVO.Info): void
|
||||
(e: 'preview', image: ImageVO.Info): void
|
||||
}
|
||||
|
||||
const props = defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false)
|
||||
const uploadLoading = ref(false)
|
||||
const imageList = ref<ImageVO.Info[]>([])
|
||||
const total = ref(0)
|
||||
const current = ref(1)
|
||||
const pageSize = ref(24) // 网格视图默认24个
|
||||
|
||||
// 监听搜索表单变化
|
||||
watch(() => props.searchForm, () => {
|
||||
current.value = 1
|
||||
loadImageList()
|
||||
}, { deep: true })
|
||||
|
||||
// 加载图片列表
|
||||
const loadImageList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const params: PageQueryDTO<ImageDTO.Filters> = {
|
||||
page: current.value,
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
filename: props.searchForm.filename || undefined,
|
||||
category: props.searchForm.category || undefined,
|
||||
}
|
||||
}
|
||||
|
||||
const response = await getImagePageApi(params)
|
||||
imageList.value = response.records
|
||||
total.value = response.total
|
||||
} catch (error) {
|
||||
message.error('加载图片列表失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索
|
||||
const handleSearch = () => {
|
||||
current.value = 1
|
||||
loadImageList()
|
||||
}
|
||||
|
||||
// 重置
|
||||
const handleReset = () => {
|
||||
props.searchForm.filename = ''
|
||||
props.searchForm.category = undefined
|
||||
handleSearch()
|
||||
}
|
||||
|
||||
// 分页变化
|
||||
const handlePageChange = (page: number, size: number) => {
|
||||
current.value = page
|
||||
pageSize.value = size
|
||||
loadImageList()
|
||||
}
|
||||
|
||||
// 上传配置
|
||||
const uploadProps = {
|
||||
multiple: true,
|
||||
showUploadList: false,
|
||||
accept: 'image/*',
|
||||
beforeUpload: (file: any) => {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
return false;
|
||||
}
|
||||
const isValidSize = file.size / 1024 / 1024 < 10; // 10MB
|
||||
if (!isValidSize) {
|
||||
message.error('图片大小不能超过 10MB');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
customRequest: (options: any) => {
|
||||
const { file } = options;
|
||||
if (Array.isArray(file)) {
|
||||
handleBatchUpload(file);
|
||||
} else {
|
||||
handleUpload(file);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 处理上传
|
||||
const handleUpload = async (file: File) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await uploadImageApi(file);
|
||||
message.success('图片上传成功');
|
||||
loadImageList();
|
||||
} catch (error) {
|
||||
message.error('图片上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 处理批量上传
|
||||
const handleBatchUpload = async (files: File[]) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await batchUploadImagesApi(files);
|
||||
message.success(`成功上传 ${files.length} 张图片`);
|
||||
loadImageList();
|
||||
} catch (error) {
|
||||
message.error('批量上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 事件处理
|
||||
const handleEdit = (image: ImageVO.Info) => {
|
||||
emit('edit', image)
|
||||
}
|
||||
|
||||
const handleDelete = (id: number) => {
|
||||
emit('delete', id)
|
||||
}
|
||||
|
||||
const handleDownload = (image: ImageVO.Info) => {
|
||||
emit('download', image)
|
||||
}
|
||||
|
||||
const handlePreview = (image: ImageVO.Info) => {
|
||||
emit('preview', image)
|
||||
}
|
||||
|
||||
// 初始化
|
||||
loadImageList()
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.image-grid-view {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,438 @@
|
||||
<script lang="ts" setup>
|
||||
import { ref, computed } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, message, Space, Tag, Upload, Image } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
|
||||
import {
|
||||
uploadImageApi,
|
||||
batchUploadImagesApi,
|
||||
deleteImageApi,
|
||||
getImagePageApi,
|
||||
} from '#/api/media/image';
|
||||
import type { ImageVO, ImageDTO } from '#/api/media/image/types';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import ImageEditModal from './components/ImageEditModal.vue';
|
||||
import ImageGridView from './components/ImageGridView.vue';
|
||||
|
||||
// 创建图标组件
|
||||
const UploadIcon = createIconifyIcon('lucide:upload');
|
||||
const GridIcon = createIconifyIcon('lucide:grid-3x3');
|
||||
const ListIcon = createIconifyIcon('lucide:list');
|
||||
|
||||
defineOptions({ name: 'ImageRepository' });
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const editModalVisible = ref(false);
|
||||
const currentImageData = ref<ImageVO.Info | null>(null);
|
||||
const uploadLoading = ref(false);
|
||||
const viewMode = ref<'grid' | 'list'>('grid'); // 默认使用网格视图
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
filename: '',
|
||||
category: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 图片分类选项
|
||||
const imageCategoryOptions = [
|
||||
{ label: '普通图片', value: 'image' },
|
||||
{ label: '头像', value: 'avatar' },
|
||||
{ label: 'Logo', value: 'logo' },
|
||||
];
|
||||
|
||||
// VbenVxeGrid 表格配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 'auto',
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
title: '预览',
|
||||
minWidth: 'auto',
|
||||
slots: { default: 'preview' },
|
||||
},
|
||||
{
|
||||
field: 'filename',
|
||||
title: '文件名',
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '分类',
|
||||
minWidth: 'auto',
|
||||
slots: { default: 'category' },
|
||||
},
|
||||
{
|
||||
title: '标签',
|
||||
minWidth: 'auto',
|
||||
slots: { default: 'tags' },
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '说明',
|
||||
minWidth: 'auto',
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'uploadTime',
|
||||
title: '上传时间',
|
||||
minWidth: 'auto',
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 'auto',
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
showOverflow: true,
|
||||
size: 'small',
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
refresh: true,
|
||||
zoom: true,
|
||||
},
|
||||
data: [],
|
||||
pagerConfig: {
|
||||
enabled: true,
|
||||
pageSizes: [10, 20, 50, 100],
|
||||
layouts: ['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total'] as ('PrevJump' | 'PrevPage' | 'Number' | 'NextPage' | 'NextJump' | 'Sizes' | 'FullJump' | 'Total')[],
|
||||
},
|
||||
// 配置远程加载
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }) => {
|
||||
try {
|
||||
loading.value = true;
|
||||
const params: PageQueryDTO<ImageDTO.Filters> = {
|
||||
page: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
filters: {
|
||||
filename: searchForm.value.filename || undefined,
|
||||
category: searchForm.value.category || undefined,
|
||||
},
|
||||
};
|
||||
const data = await getImagePageApi(params);
|
||||
return {
|
||||
items: data.records,
|
||||
total: data.total,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取图片列表失败:', error);
|
||||
message.error('获取图片列表失败');
|
||||
return {
|
||||
items: [],
|
||||
total: 0,
|
||||
};
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// 搜索表单配置
|
||||
const formOptions = computed(() => ({
|
||||
schema: [
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '请输入文件名',
|
||||
},
|
||||
fieldName: 'filename',
|
||||
label: '文件名',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
placeholder: '请选择图片分类',
|
||||
options: imageCategoryOptions,
|
||||
allowClear: true,
|
||||
},
|
||||
fieldName: 'category',
|
||||
label: '图片分类',
|
||||
},
|
||||
],
|
||||
showDefaultActions: true,
|
||||
collapsed: true,
|
||||
collapsedRows: 1,
|
||||
}));
|
||||
|
||||
// 刷新表格数据
|
||||
function refreshTable() {
|
||||
if (gridApi) {
|
||||
gridApi.reload();
|
||||
}
|
||||
}
|
||||
|
||||
// 创建 VbenVxeGrid 实例
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions,
|
||||
formOptions: formOptions.value,
|
||||
showSearchForm: true,
|
||||
tableTitle: '图片管理',
|
||||
});
|
||||
|
||||
// 处理上传
|
||||
const handleUpload = async (file: File) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await uploadImageApi(file);
|
||||
message.success('图片上传成功');
|
||||
refreshTable();
|
||||
} catch (error) {
|
||||
message.error('图片上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 处理批量上传
|
||||
const handleBatchUpload = async (files: File[]) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await batchUploadImagesApi(files);
|
||||
message.success(`成功上传 ${files.length} 张图片`);
|
||||
refreshTable();
|
||||
} catch (error) {
|
||||
message.error('批量上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 处理编辑
|
||||
const handleEdit = (record: ImageVO.Info) => {
|
||||
currentImageData.value = record;
|
||||
editModalVisible.value = true;
|
||||
};
|
||||
|
||||
// 处理预览
|
||||
const handlePreview = (record: ImageVO.Info) => {
|
||||
window.open(record.url, '_blank');
|
||||
};
|
||||
|
||||
// 处理下载
|
||||
const handleDownload = (record: ImageVO.Info) => {
|
||||
const link = document.createElement('a');
|
||||
link.href = record.url;
|
||||
link.download = record.filename || '未命名';
|
||||
link.click();
|
||||
};
|
||||
|
||||
// 处理删除
|
||||
const handleDelete = async (id: number) => {
|
||||
try {
|
||||
await deleteImageApi(id);
|
||||
message.success('删除成功');
|
||||
refreshTable();
|
||||
} catch (error) {
|
||||
message.error('删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 处理编辑成功
|
||||
const handleEditSuccess = () => {
|
||||
refreshTable();
|
||||
};
|
||||
|
||||
// 切换视图模式
|
||||
const toggleViewMode = () => {
|
||||
viewMode.value = viewMode.value === 'grid' ? 'list' : 'grid';
|
||||
};
|
||||
|
||||
// 上传配置
|
||||
const uploadProps = {
|
||||
multiple: true,
|
||||
showUploadList: false,
|
||||
accept: 'image/*',
|
||||
beforeUpload: (file: any) => {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
return false;
|
||||
}
|
||||
const isValidSize = file.size / 1024 / 1024 < 10; // 10MB
|
||||
if (!isValidSize) {
|
||||
message.error('图片大小不能超过 10MB');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
customRequest: (options: any) => {
|
||||
const { file } = options;
|
||||
if (Array.isArray(file)) {
|
||||
handleBatchUpload(file);
|
||||
} else {
|
||||
handleUpload(file);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 获取分类标签颜色
|
||||
const getCategoryColor = (category: string | undefined) => {
|
||||
switch (category) {
|
||||
case 'image':
|
||||
return 'blue';
|
||||
case 'avatar':
|
||||
return 'green';
|
||||
case 'logo':
|
||||
return 'purple';
|
||||
default:
|
||||
return 'default';
|
||||
}
|
||||
};
|
||||
|
||||
// 获取分类显示文本
|
||||
const getCategoryText = (category: string | undefined) => {
|
||||
switch (category) {
|
||||
case 'image':
|
||||
return '普通图片';
|
||||
case 'avatar':
|
||||
return '头像';
|
||||
case 'logo':
|
||||
return 'Logo';
|
||||
default:
|
||||
return '未分类';
|
||||
}
|
||||
};
|
||||
|
||||
// 获取标签列表
|
||||
const getTagList = (tags: string | undefined) => {
|
||||
if (!tags) return [];
|
||||
return tags.split(',').map(tag => tag.trim()).filter(tag => tag);
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page title="图片仓库" auto-content-height>
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
管理图片资源,支持上传、预览、编辑和分类管理
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #extra>
|
||||
<Space>
|
||||
<Button
|
||||
:type="viewMode === 'grid' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<GridIcon class="w-4 h-4 mr-2" />
|
||||
网格视图
|
||||
</Button>
|
||||
<Button
|
||||
:type="viewMode === 'list' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<ListIcon class="w-4 h-4 mr-2" />
|
||||
列表视图
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
<!-- 列表视图 -->
|
||||
<Grid
|
||||
v-if="viewMode === 'list'"
|
||||
:loading="loading"
|
||||
>
|
||||
<!-- 工具栏工具 -->
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Upload v-bind="uploadProps">
|
||||
<Button type="primary" :loading="uploadLoading">
|
||||
<UploadIcon class="w-4 h-4 mr-2" />
|
||||
上传图片
|
||||
</Button>
|
||||
</Upload>
|
||||
<span class="text-gray-500">支持批量上传,单个图片最大10MB</span>
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
<!-- 预览列 -->
|
||||
<template #preview="{ row }: { row: any }">
|
||||
<Image
|
||||
:src="row.url"
|
||||
:alt="row.filename"
|
||||
width="60"
|
||||
height="60"
|
||||
style="object-fit: cover; border-radius: 4px;"
|
||||
:preview="true"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<!-- 分类列 -->
|
||||
<template #category="{ row }: { row: any }">
|
||||
<Tag :color="getCategoryColor(row.category)">
|
||||
{{ getCategoryText(row.category) }}
|
||||
</Tag>
|
||||
</template>
|
||||
|
||||
<!-- 标签列 -->
|
||||
<template #tags="{ row }: { row: any }">
|
||||
<div v-if="row.tags" class="flex flex-wrap gap-1">
|
||||
<Tag
|
||||
v-for="tag in getTagList(row.tags)"
|
||||
:key="tag"
|
||||
size="small"
|
||||
color="blue"
|
||||
>
|
||||
{{ tag }}
|
||||
</Tag>
|
||||
</div>
|
||||
<span v-else class="text-gray-400">无标签</span>
|
||||
</template>
|
||||
|
||||
<!-- 操作列 -->
|
||||
<template #action="{ row }: { row: any }">
|
||||
<Space>
|
||||
<Button size="small" @click="handleDownload(row)">
|
||||
下载
|
||||
</Button>
|
||||
<Button size="small" @click="handleEdit(row)">
|
||||
编辑
|
||||
</Button>
|
||||
<Button size="small" danger @click="handleDelete(row.id)">
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
<!-- 网格视图 -->
|
||||
<ImageGridView
|
||||
v-else
|
||||
:search-form="searchForm"
|
||||
:category-options="imageCategoryOptions"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
@download="handleDownload"
|
||||
@preview="handlePreview"
|
||||
/>
|
||||
|
||||
<!-- 编辑弹窗 -->
|
||||
<ImageEditModal
|
||||
v-model:open="editModalVisible"
|
||||
:image-data="currentImageData"
|
||||
@success="handleEditSuccess"
|
||||
@cancel="editModalVisible = false"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.ant-upload-list {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user