mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:49:19 +08:00
完成媒体管理模块
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
import type { MediaFileDTO, MediaFileVO, MediaImageDTO, MediaImageVO } from './types';
|
||||
import type { PageQueryDTO, PageResultVO } from '#/types/common';
|
||||
|
||||
/**
|
||||
* 文件管理相关API
|
||||
*/
|
||||
|
||||
// 文件上传
|
||||
export async function uploadFileApi(file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return requestClient.post<MediaFileVO.MediaFileInfoVO>('/media/file/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 批量文件上传
|
||||
export async function batchUploadFilesApi(files: File[]) {
|
||||
const formData = new FormData();
|
||||
files.forEach(file => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
return requestClient.post<MediaFileVO.MediaFileInfoVO[]>('/media/file/batch-upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 更新文件信息
|
||||
export async function updateFileApi(data: MediaFileDTO.UpdateFileDTO) {
|
||||
return requestClient.put<MediaFileVO.MediaFileInfoVO>('/media/file/update', data);
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
export async function deleteFileApi(id: number) {
|
||||
return requestClient.delete(`/media/file/delete/${id}`);
|
||||
}
|
||||
|
||||
// 根据哈希查询文件
|
||||
export async function getFileByHashApi(fileHash: string) {
|
||||
return requestClient.get<MediaFileVO.MediaFileInfoVO>(`/media/file/hash/${fileHash}`);
|
||||
}
|
||||
|
||||
// 批量根据哈希查询文件
|
||||
export async function getFilesByHashesApi(fileHashes: string[]) {
|
||||
return requestClient.post<MediaFileVO.MediaFileInfoVO[]>('/media/file/batch-hash', fileHashes);
|
||||
}
|
||||
|
||||
// 分页获取文件列表
|
||||
export async function getFilePageApi(params: PageQueryDTO<MediaFileDTO.MediaFileFilterDTO>) {
|
||||
return requestClient.post<PageResultVO<MediaFileVO.MediaFileInfoVO>>('/media/file/page', params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片管理相关API
|
||||
*/
|
||||
|
||||
// 图片上传
|
||||
export async function uploadImageApi(file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return requestClient.post<MediaImageVO.MediaImageInfoVO>('/media/image/upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 批量图片上传
|
||||
export async function batchUploadImagesApi(files: File[]) {
|
||||
const formData = new FormData();
|
||||
files.forEach(file => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
return requestClient.post<MediaImageVO.MediaImageInfoVO[]>('/media/image/batch-upload', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 更新图片信息
|
||||
export async function updateImageApi(data: MediaImageDTO.UpdateImageDTO) {
|
||||
return requestClient.put<MediaImageVO.MediaImageInfoVO>('/media/image/update', data);
|
||||
}
|
||||
|
||||
// 删除图片
|
||||
export async function deleteImageApi(id: number) {
|
||||
return requestClient.delete(`/media/image/delete/${id}`);
|
||||
}
|
||||
|
||||
// 根据哈希查询图片
|
||||
export async function getImageByHashApi(fileHash: string) {
|
||||
return requestClient.get<MediaImageVO.MediaImageInfoVO>(`/media/image/hash/${fileHash}`);
|
||||
}
|
||||
|
||||
// 批量根据哈希查询图片
|
||||
export async function getImagesByHashesApi(fileHashes: string[]) {
|
||||
return requestClient.post<MediaImageVO.MediaImageInfoVO[]>('/media/image/batch-hash', fileHashes);
|
||||
}
|
||||
|
||||
// 分页获取图片列表
|
||||
export async function getImagePageApi(params: PageQueryDTO<MediaImageDTO.MediaImageFilterDTO>) {
|
||||
return requestClient.post<PageResultVO<MediaImageVO.MediaImageInfoVO>>('/media/image/page', params);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './apis';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* 媒体文件相关类型定义
|
||||
*/
|
||||
|
||||
export namespace MediaFileVO {
|
||||
export interface MediaFileInfoVO {
|
||||
id: number;
|
||||
companyId: number;
|
||||
url: string;
|
||||
filename: string;
|
||||
fileType: string;
|
||||
fileHash: string;
|
||||
tags: string;
|
||||
remark: string;
|
||||
uploadBy: number;
|
||||
uploadTime: string;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace MediaImageVO {
|
||||
export interface MediaImageInfoVO {
|
||||
id: number;
|
||||
companyId: number;
|
||||
url: string;
|
||||
filename: string;
|
||||
type: string;
|
||||
fileHash: string;
|
||||
tags: string;
|
||||
remark: string;
|
||||
uploadBy: number;
|
||||
uploadTime: string;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace MediaFileDTO {
|
||||
export interface UpdateFileDTO {
|
||||
id: number;
|
||||
fileType?: string;
|
||||
tags?: string;
|
||||
remark?: string;
|
||||
}
|
||||
|
||||
export interface MediaFileFilterDTO {
|
||||
filename?: string;
|
||||
fileType?: string;
|
||||
tags?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace MediaImageDTO {
|
||||
export interface UpdateImageDTO {
|
||||
id: number;
|
||||
type?: string;
|
||||
tags?: string;
|
||||
remark?: string;
|
||||
}
|
||||
|
||||
export interface MediaImageFilterDTO {
|
||||
filename?: string;
|
||||
type?: string;
|
||||
tags?: string;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
meta: {
|
||||
icon: 'lucide:folder-open',
|
||||
order: 300,
|
||||
title: '媒体管理',
|
||||
},
|
||||
name: 'Media',
|
||||
path: '/media',
|
||||
children: [
|
||||
{
|
||||
name: 'FileRepository',
|
||||
path: '/media/file-repository',
|
||||
component: () => import('#/views/media/file-repository/index.vue'),
|
||||
meta: {
|
||||
icon: 'lucide:folder',
|
||||
title: '文件仓库',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'ImageRepository',
|
||||
path: '/media/image-repository',
|
||||
component: () => import('#/views/media/image-repository/index.vue'),
|
||||
meta: {
|
||||
icon: 'lucide:image',
|
||||
title: '图片仓库',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
||||
@@ -0,0 +1,459 @@
|
||||
<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
|
||||
} from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
// 创建图标组件
|
||||
const UploadIcon = createIconifyIcon('lucide:upload');
|
||||
const DeleteIcon = createIconifyIcon('lucide:trash-2');
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const DownloadIcon = createIconifyIcon('lucide:download');
|
||||
const EyeIcon = createIconifyIcon('lucide:eye');
|
||||
|
||||
import {
|
||||
uploadFileApi,
|
||||
batchUploadFilesApi,
|
||||
updateFileApi,
|
||||
deleteFileApi,
|
||||
getFilePageApi,
|
||||
type MediaFileVO,
|
||||
type MediaFileDTO
|
||||
} from '#/api/media';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
defineOptions({ name: 'FileRepository' });
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const fileList = ref<MediaFileVO.MediaFileInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(20);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentFileId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const uploadLoading = ref(false);
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
filename: '',
|
||||
fileType: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<MediaFileDTO.UpdateFileDTO>({
|
||||
id: 0,
|
||||
fileType: '',
|
||||
tags: '',
|
||||
remark: '',
|
||||
});
|
||||
|
||||
// 文件类型选项
|
||||
const fileTypeOptions = [
|
||||
{ label: 'PDF', value: 'PDF' },
|
||||
{ label: 'CAD', value: 'CAD' },
|
||||
{ label: 'Excel', value: 'Excel' },
|
||||
{ label: 'Word', value: 'Word' },
|
||||
{ label: 'PPT', value: 'PPT' },
|
||||
{ label: '图片', value: '图片' },
|
||||
{ label: '视频', value: '视频' },
|
||||
{ label: '音频', value: '音频' },
|
||||
{ label: '压缩包', value: '压缩包' },
|
||||
{ label: '其他', value: '其他' },
|
||||
];
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '文件名',
|
||||
dataIndex: 'filename',
|
||||
key: 'filename',
|
||||
width: 300,
|
||||
customRender: ({ record }: { record: MediaFileVO.MediaFileInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; align-items: center; gap: 8px;' }, [
|
||||
h('span', { style: 'font-weight: 500;' }, record.filename),
|
||||
h(Tag, { color: 'blue', size: 'small' }, () => record.fileType || '未知'),
|
||||
]);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '文件类型',
|
||||
dataIndex: 'fileType',
|
||||
key: 'fileType',
|
||||
width: 120,
|
||||
customRender: ({ record }: { record: MediaFileVO.MediaFileInfoVO }) => {
|
||||
return h(Tag, { color: 'blue' }, () => record.fileType || '未知');
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '标签',
|
||||
dataIndex: 'tags',
|
||||
key: 'tags',
|
||||
width: 200,
|
||||
customRender: ({ record }: { record: MediaFileVO.MediaFileInfoVO }) => {
|
||||
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: MediaFileVO.MediaFileInfoVO }) => {
|
||||
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: MediaFileVO.MediaFileInfoVO }) => {
|
||||
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(DeleteIcon, { class: 'w-4 h-4' })
|
||||
})
|
||||
})
|
||||
}),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 计算属性 - 由于使用后端分页,不再需要前端过滤
|
||||
const filteredFileList = computed(() => fileList.value);
|
||||
|
||||
// 方法
|
||||
const loadFileList = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const params: PageQueryDTO<MediaFileDTO.MediaFileFilterDTO> = {
|
||||
page: current.value,
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
filename: searchForm.value.filename || undefined,
|
||||
fileType: searchForm.value.fileType || undefined,
|
||||
}
|
||||
};
|
||||
|
||||
const response = await getFilePageApi(params);
|
||||
fileList.value = response.records;
|
||||
total.value = response.total;
|
||||
} catch (error) {
|
||||
message.error('加载文件列表失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleSearch = () => {
|
||||
current.value = 1;
|
||||
loadFileList();
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
searchForm.value = {
|
||||
filename: '',
|
||||
fileType: undefined,
|
||||
};
|
||||
handleSearch();
|
||||
};
|
||||
|
||||
const handleUpload = async (file: File) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await uploadFileApi(file);
|
||||
message.success('文件上传成功');
|
||||
loadFileList();
|
||||
} catch (error) {
|
||||
message.error('文件上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleBatchUpload = async (files: File[]) => {
|
||||
uploadLoading.value = true;
|
||||
try {
|
||||
await batchUploadFilesApi(files);
|
||||
message.success(`成功上传 ${files.length} 个文件`);
|
||||
loadFileList();
|
||||
} catch (error) {
|
||||
message.error('批量上传失败');
|
||||
} finally {
|
||||
uploadLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleEdit = (record: MediaFileVO.MediaFileInfoVO) => {
|
||||
isEdit.value = true;
|
||||
modalTitle.value = '编辑文件信息';
|
||||
currentFileId.value = record.id;
|
||||
formData.value = {
|
||||
id: record.id,
|
||||
fileType: record.fileType,
|
||||
tags: record.tags,
|
||||
remark: record.remark,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
};
|
||||
|
||||
const handlePreview = (record: MediaFileVO.MediaFileInfoVO) => {
|
||||
window.open(record.url, '_blank');
|
||||
};
|
||||
|
||||
const handleDownload = (record: MediaFileVO.MediaFileInfoVO) => {
|
||||
const link = document.createElement('a');
|
||||
link.href = record.url;
|
||||
link.download = record.filename;
|
||||
link.click();
|
||||
};
|
||||
|
||||
const handleDelete = async (id: number) => {
|
||||
try {
|
||||
await deleteFileApi(id);
|
||||
message.success('删除成功');
|
||||
loadFileList();
|
||||
} catch (error) {
|
||||
message.error('删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleModalOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
await updateFileApi(formData.value);
|
||||
message.success('更新成功');
|
||||
modalVisible.value = false;
|
||||
loadFileList();
|
||||
} catch (error) {
|
||||
message.error('更新失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleModalCancel = () => {
|
||||
modalVisible.value = false;
|
||||
formData.value = {
|
||||
id: 0,
|
||||
fileType: '',
|
||||
tags: '',
|
||||
remark: '',
|
||||
};
|
||||
};
|
||||
|
||||
// 上传配置
|
||||
const uploadProps = {
|
||||
multiple: true,
|
||||
showUploadList: false,
|
||||
beforeUpload: (file: any) => {
|
||||
const isValidSize = file.size / 1024 / 1024 < 100; // 100MB
|
||||
if (!isValidSize) {
|
||||
message.error('文件大小不能超过 100MB');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
customRequest: (options: any) => {
|
||||
const { file } = options;
|
||||
if (Array.isArray(file)) {
|
||||
handleBatchUpload(file);
|
||||
} else {
|
||||
handleUpload(file);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 初始化
|
||||
loadFileList();
|
||||
</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.fileType"
|
||||
placeholder="请选择文件类型"
|
||||
style="width: 150px"
|
||||
allow-clear
|
||||
>
|
||||
<Select.Option
|
||||
v-for="option in fileTypeOptions"
|
||||
: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">支持批量上传,单个文件最大100MB</span>
|
||||
</div>
|
||||
<div class="text-gray-500">
|
||||
共 {{ total }} 个文件
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 文件列表 -->
|
||||
<div class="bg-white rounded-lg shadow-sm">
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="filteredFileList"
|
||||
: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="fileType">
|
||||
<Select v-model:value="formData.fileType" placeholder="请选择文件类型">
|
||||
<Select.Option
|
||||
v-for="option in fileTypeOptions"
|
||||
: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>
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
<script lang="ts" setup>
|
||||
import { Card, Image, Tag, Button, Space, Popconfirm } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import type { MediaImageVO } from '#/api/media';
|
||||
|
||||
// 创建图标组件
|
||||
const DownloadIcon = createIconifyIcon('lucide:download');
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
|
||||
interface Props {
|
||||
image: MediaImageVO.MediaImageInfoVO;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'download', image: MediaImageVO.MediaImageInfoVO): void;
|
||||
(e: 'edit', image: MediaImageVO.MediaImageInfoVO): void;
|
||||
(e: 'delete', id: number): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const handleDownload = (image: MediaImageVO.MediaImageInfoVO) => {
|
||||
emit('download', image);
|
||||
};
|
||||
|
||||
const handleEdit = (image: MediaImageVO.MediaImageInfoVO) => {
|
||||
emit('edit', image);
|
||||
};
|
||||
|
||||
const handleDelete = (id: number) => {
|
||||
emit('delete', id);
|
||||
};
|
||||
|
||||
const getTagColor = (type: string | undefined) => {
|
||||
switch (type) {
|
||||
case '设计图':
|
||||
return 'blue';
|
||||
case '实物图':
|
||||
return 'green';
|
||||
case '细节图':
|
||||
return 'orange';
|
||||
case '工艺图':
|
||||
return 'purple';
|
||||
default:
|
||||
return '#006BE6';
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Card
|
||||
hoverable
|
||||
:body-style="{ padding: '8px' }"
|
||||
class="image-card"
|
||||
>
|
||||
<div class="relative">
|
||||
<Image
|
||||
:src="image.url"
|
||||
:alt="image.filename"
|
||||
class="w-full h-32 object-cover rounded"
|
||||
:preview="true"
|
||||
/>
|
||||
<div class="absolute top-2 right-2">
|
||||
<Tag :color="getTagColor(image.type)">
|
||||
{{ image.type || '未分类' }}
|
||||
</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,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>
|
||||
@@ -2,17 +2,26 @@ import { defineConfig } from '@vben/vite-config';
|
||||
|
||||
export default defineConfig(async () => {
|
||||
return {
|
||||
application: {},
|
||||
application: {
|
||||
// 设置API URL
|
||||
apiURL: '/api',
|
||||
},
|
||||
vite: {
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': {
|
||||
changeOrigin: true,
|
||||
rewrite: (path) => path.replace(/^\/api/, ''),
|
||||
// mock代理目标地址
|
||||
// 后端服务地址
|
||||
target: 'http://localhost:8080',
|
||||
ws: true,
|
||||
},
|
||||
// 静态资源代理 - 用于访问上传的图片和文件
|
||||
'/uploads': {
|
||||
changeOrigin: true,
|
||||
target: 'http://localhost:8080',
|
||||
ws: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user