mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 19:49:28 +08:00
完成媒体管理模块
This commit is contained in:
@@ -0,0 +1,525 @@
|
||||
<script lang="ts" setup>
|
||||
import { h, ref, computed } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
Input,
|
||||
Modal,
|
||||
Select,
|
||||
Table,
|
||||
Upload,
|
||||
message,
|
||||
Tag,
|
||||
Space,
|
||||
Popconfirm,
|
||||
Tooltip,
|
||||
Image,
|
||||
Row,
|
||||
Col
|
||||
} from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import ImageCard from './components/ImageCard.vue';
|
||||
|
||||
// 创建图标组件
|
||||
const EyeIcon = createIconifyIcon('lucide:eye');
|
||||
const DownloadIcon = createIconifyIcon('lucide:download');
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
const UploadIcon = createIconifyIcon('lucide:upload');
|
||||
const GridIcon = createIconifyIcon('lucide:grid-3x3');
|
||||
const ListIcon = createIconifyIcon('lucide:list');
|
||||
|
||||
import {
|
||||
uploadImageApi,
|
||||
batchUploadImagesApi,
|
||||
updateImageApi,
|
||||
deleteImageApi,
|
||||
getImagePageApi,
|
||||
type MediaImageVO,
|
||||
type MediaImageDTO
|
||||
} from '#/api/media';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
defineOptions({ name: 'ImageRepository' });
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const imageList = ref<MediaImageVO.MediaImageInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(20);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentImageId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const uploadLoading = ref(false);
|
||||
const viewMode = ref<'grid' | 'list'>('grid'); // 视图模式:网格或列表
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
filename: '',
|
||||
type: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<MediaImageDTO.UpdateImageDTO>({
|
||||
id: 0,
|
||||
type: '',
|
||||
tags: '',
|
||||
remark: '',
|
||||
});
|
||||
|
||||
// 图片分类选项
|
||||
const imageTypeOptions = [
|
||||
{ label: '设计图', value: '设计图' },
|
||||
{ label: '实物图', value: '实物图' },
|
||||
{ label: '细节图', value: '细节图' },
|
||||
{ label: '工艺图', value: '工艺图' },
|
||||
{ label: '其他', value: '其他' },
|
||||
];
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '预览',
|
||||
dataIndex: 'url',
|
||||
key: 'preview',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
return h(Image, {
|
||||
src: record.url,
|
||||
alt: record.filename,
|
||||
width: 60,
|
||||
height: 60,
|
||||
style: { objectFit: 'cover', borderRadius: '4px' },
|
||||
preview: true,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '文件名',
|
||||
dataIndex: 'filename',
|
||||
key: 'filename',
|
||||
width: 250,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; align-items: center; gap: 8px;' }, [
|
||||
h('span', { style: 'font-weight: 500;' }, record.filename),
|
||||
h(Tag, { color: 'purple', size: 'small' }, () => record.type || '未分类'),
|
||||
]);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '分类',
|
||||
dataIndex: 'type',
|
||||
key: 'type',
|
||||
width: 120,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
return h(Tag, { color: 'purple' }, () => record.type || '未分类');
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '标签',
|
||||
dataIndex: 'tags',
|
||||
key: 'tags',
|
||||
width: 200,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
if (!record.tags) return h('span', { style: 'color: #999;' }, '无标签');
|
||||
const tags = record.tags.split(',').filter(tag => tag.trim());
|
||||
return h('div', { style: 'display: flex; flex-wrap: wrap; gap: 4px;' },
|
||||
tags.map(tag => h(Tag, { color: 'green', size: 'small' }, () => tag.trim()))
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '说明',
|
||||
dataIndex: 'remark',
|
||||
key: 'remark',
|
||||
width: 200,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
if (!record.remark) return h('span', { style: 'color: #999;' }, '无说明');
|
||||
return h(Tooltip, { title: record.remark }, {
|
||||
default: () => h('span', {
|
||||
style: 'display: block; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'
|
||||
}, record.remark)
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '上传时间',
|
||||
dataIndex: 'uploadTime',
|
||||
key: 'uploadTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 200,
|
||||
fixed: 'right' as const,
|
||||
customRender: ({ record }: { record: MediaImageVO.MediaImageInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; gap: 8px;' }, [
|
||||
h(Tooltip, { title: '预览' }, {
|
||||
default: () => h(Button, {
|
||||
type: 'text',
|
||||
size: 'small',
|
||||
onClick: () => handlePreview(record),
|
||||
}, {
|
||||
default: () => h(EyeIcon, { class: 'w-4 h-4' })
|
||||
})
|
||||
}),
|
||||
h(Tooltip, { title: '下载' }, {
|
||||
default: () => h(Button, {
|
||||
type: 'text',
|
||||
size: 'small',
|
||||
onClick: () => handleDownload(record),
|
||||
}, {
|
||||
default: () => h(DownloadIcon, { class: 'w-4 h-4' })
|
||||
})
|
||||
}),
|
||||
h(Tooltip, { title: '编辑' }, {
|
||||
default: () => h(Button, {
|
||||
type: 'text',
|
||||
size: 'small',
|
||||
onClick: () => handleEdit(record),
|
||||
}, {
|
||||
default: () => h(EditIcon, { class: 'w-4 h-4' })
|
||||
})
|
||||
}),
|
||||
h(Tooltip, { title: '删除' }, {
|
||||
default: () => h(Popconfirm, {
|
||||
title: '确定要删除这张图片吗?',
|
||||
onConfirm: () => handleDelete(record.id),
|
||||
}, {
|
||||
default: () => h(Button, {
|
||||
type: 'text',
|
||||
size: 'small',
|
||||
danger: true,
|
||||
}, {
|
||||
default: () => h(TrashIcon, { class: 'w-4 h-4' })
|
||||
})
|
||||
})
|
||||
}),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 计算属性 - 由于使用后端分页,不再需要前端过滤
|
||||
const filteredImageList = computed(() => imageList.value);
|
||||
|
||||
// 方法
|
||||
const loadImageList = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const params: PageQueryDTO<MediaImageDTO.MediaImageFilterDTO> = {
|
||||
page: current.value,
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
filename: searchForm.value.filename || undefined,
|
||||
type: searchForm.value.type || 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 = () => {
|
||||
searchForm.value = {
|
||||
filename: '',
|
||||
type: undefined,
|
||||
};
|
||||
handleSearch();
|
||||
};
|
||||
|
||||
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 = (record: MediaImageVO.MediaImageInfoVO) => {
|
||||
isEdit.value = true;
|
||||
modalTitle.value = '编辑图片信息';
|
||||
currentImageId.value = record.id;
|
||||
formData.value = {
|
||||
id: record.id,
|
||||
type: record.type,
|
||||
tags: record.tags,
|
||||
remark: record.remark,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
};
|
||||
|
||||
const handlePreview = (record: MediaImageVO.MediaImageInfoVO) => {
|
||||
window.open(record.url, '_blank');
|
||||
};
|
||||
|
||||
const handleDownload = (record: MediaImageVO.MediaImageInfoVO) => {
|
||||
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('删除成功');
|
||||
loadImageList();
|
||||
} catch (error) {
|
||||
message.error('删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleModalOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
await updateImageApi(formData.value);
|
||||
message.success('更新成功');
|
||||
modalVisible.value = false;
|
||||
loadImageList();
|
||||
} catch (error) {
|
||||
message.error('更新失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleModalCancel = () => {
|
||||
modalVisible.value = false;
|
||||
formData.value = {
|
||||
id: 0,
|
||||
type: '',
|
||||
tags: '',
|
||||
remark: '',
|
||||
};
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 初始化
|
||||
loadImageList();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page>
|
||||
<template #title>图片仓库</template>
|
||||
|
||||
<!-- 搜索区域 -->
|
||||
<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.type"
|
||||
placeholder="请选择图片分类"
|
||||
style="width: 150px"
|
||||
allow-clear
|
||||
>
|
||||
<Select.Option
|
||||
v-for="option in imageTypeOptions"
|
||||
: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="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="flex items-center gap-4">
|
||||
<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>
|
||||
<span class="text-gray-500">
|
||||
共 {{ total }} 张图片
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 网格视图 -->
|
||||
<div v-if="viewMode === 'grid'" class="bg-white rounded-lg shadow-sm p-4">
|
||||
<Row :gutter="[16, 16]">
|
||||
<Col
|
||||
v-for="image in filteredImageList"
|
||||
:key="image.id"
|
||||
:xs="12"
|
||||
:sm="8"
|
||||
:md="6"
|
||||
:lg="4"
|
||||
:xl="3"
|
||||
>
|
||||
<ImageCard
|
||||
:image="image"
|
||||
@download="handleDownload"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</div>
|
||||
|
||||
<!-- 列表视图 -->
|
||||
<div v-else class="bg-white rounded-lg shadow-sm">
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="filteredImageList"
|
||||
:loading="loading"
|
||||
:pagination="{
|
||||
current: current,
|
||||
pageSize: pageSize,
|
||||
total: total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条,共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
:scroll="{ x: 1200 }"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 编辑弹窗 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
@ok="handleModalOk"
|
||||
@cancel="handleModalCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="图片分类" name="type">
|
||||
<Select v-model:value="formData.type" placeholder="请选择图片分类">
|
||||
<Select.Option
|
||||
v-for="option in imageTypeOptions"
|
||||
:key="option.value"
|
||||
:value="option.value"
|
||||
>
|
||||
{{ option.label }}
|
||||
</Select.Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<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>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.ant-upload-list {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user