Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/views/media/file/index.vue
T

383 lines
9.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts" setup>
import { ref, computed } from 'vue';
import { Page } from '@vben/common-ui';
import {
Button,
Upload,
message,
Tag,
Space,
Popconfirm,
Tooltip
} from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import type { VxeGridProps } from '#/adapter/vxe-table';
import FileEditModal from './components/FileEditModal.vue';
// 创建图标组件
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,
deleteFileApi,
getFilePageApi,
} from '#/api/media/file';
import type { FileVO, FileDTO } from '#/api/media/file/types';
import type { PageQueryDTO } from '#/types/common';
defineOptions({ name: 'FileRepository' });
// 响应式数据
const loading = ref(false);
const fileList = ref<FileVO.Info[]>([]);
const total = ref(0);
const editModalVisible = ref(false);
const currentFileData = ref<FileVO.Info | null>(null);
const uploadLoading = ref(false);
// 搜索表单
const searchForm = ref({
filename: '',
fileFormat: undefined as string | undefined,
});
// 文件格式选项
const fileFormatOptions = [
{ label: 'PDF', value: 'pdf' },
{ label: 'CAD', value: 'cad' },
{ label: 'Excel', value: 'xlsx' },
{ label: 'Word', value: 'doc' },
{ label: 'PPT', value: 'ppt' },
{ label: '图片', value: 'jpg' },
{ label: '视频', value: 'mp4' },
{ label: '音频', value: 'mp3' },
{ label: '压缩包', value: 'zip' },
{ label: '其他', value: 'other' },
];
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
title: '文件名',
minWidth: 300,
slots: { default: 'filename' },
},
{
title: '文件格式',
minWidth: 120,
slots: { default: 'fileFormat' },
},
{
title: '标签',
minWidth: 200,
slots: { default: 'tags' },
},
{
title: '描述',
minWidth: 200,
slots: { default: 'remark' },
},
{
field: 'uploadTime',
title: '上传时间',
minWidth: 180,
},
{
title: '操作',
minWidth: 200,
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<FileDTO.Filters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
filename: searchForm.value.filename || undefined,
fileFormat: searchForm.value.fileFormat || undefined,
},
};
const response = await getFilePageApi(params);
fileList.value = response.records;
total.value = response.total;
return {
items: response.records,
total: response.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: fileFormatOptions,
allowClear: true,
},
fieldName: 'fileFormat',
label: '文件格式',
},
],
showDefaultActions: true,
collapsed: true,
collapsedRows: 1,
}));
// 刷新表格数据
function refreshTable() {
if (gridApi) {
gridApi.reload();
}
}
const handleUpload = async (file: File) => {
uploadLoading.value = true;
try {
await uploadFileApi(file);
message.success('文件上传成功');
refreshTable();
} catch (error) {
message.error('文件上传失败');
} finally {
uploadLoading.value = false;
}
};
const handleBatchUpload = async (files: File[]) => {
uploadLoading.value = true;
try {
await batchUploadFilesApi(files);
message.success(`成功上传 ${files.length} 个文件`);
refreshTable();
} catch (error) {
message.error('批量上传失败');
} finally {
uploadLoading.value = false;
}
};
const handleEdit = (record: FileVO.Info) => {
currentFileData.value = record;
editModalVisible.value = true;
};
const handlePreview = (record: FileVO.Info) => {
window.open(record.url, '_blank');
};
const handleDownload = (record: FileVO.Info) => {
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('删除成功');
refreshTable();
} catch (error) {
message.error('删除失败');
}
};
const handleEditSuccess = () => {
refreshTable();
};
// 上传配置
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);
}
},
};
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '文件管理',
});
</script>
<template>
<Page title="文件仓库" auto-content-height>
<template #description>
<div class="text-foreground/80 mt-2">
管理文件资源,支持文件上传、预览、下载和编辑
</div>
</template>
<!-- VbenVxeGrid 表格 -->
<Grid :loading="loading">
<!-- 工具栏工具 -->
<template #toolbar-tools>
<Space>
<Upload v-bind="uploadProps">
<Button type="primary" :loading="uploadLoading">
<UploadIcon />
上传文件
</Button>
</Upload>
</Space>
</template>
<!-- 文件名列 -->
<template #filename="{ row }: { row: any }">
<div style="display: flex; align-items: center; gap: 8px;">
<span style="font-weight: 500;">{{ row.filename }}</span>
<Tag color="blue" size="small">{{ row.fileFormat || '未知' }}</Tag>
</div>
</template>
<!-- 文件格式列 -->
<template #fileFormat="{ row }: { row: any }">
<Tag color="blue">{{ row.fileFormat || '未知' }}</Tag>
</template>
<!-- 标签列 -->
<template #tags="{ row }: { row: any }">
<div v-if="!row.tags" style="color: #999;">无标签</div>
<div v-else style="display: flex; flex-wrap: wrap; gap: 4px;">
<Tag
v-for="tag in row.tags.split(',').filter((t: string) => t.trim())"
:key="tag"
color="green"
size="small"
>
{{ tag.trim() }}
</Tag>
</div>
</template>
<!-- 描述列 -->
<template #remark="{ row }: { row: any }">
<Tooltip v-if="row.remark" :title="row.remark">
<span style="display: block; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
{{ row.remark }}
</span>
</Tooltip>
<span v-else style="color: #999;">无描述</span>
</template>
<!-- 操作列 -->
<template #action="{ row }: { row: any }">
<Space>
<Tooltip title="预览">
<Button type="text" size="small" @click="handlePreview(row)">
<EyeIcon class="w-4 h-4" />
</Button>
</Tooltip>
<Tooltip title="下载">
<Button type="text" size="small" @click="handleDownload(row)">
<DownloadIcon class="w-4 h-4" />
</Button>
</Tooltip>
<Tooltip title="编辑">
<Button type="text" size="small" @click="handleEdit(row)">
<EditIcon class="w-4 h-4" />
</Button>
</Tooltip>
<Popconfirm title="确定要删除这个文件吗?" @confirm="handleDelete(row.id)">
<Tooltip title="删除">
<Button type="text" size="small" danger>
<DeleteIcon class="w-4 h-4" />
</Button>
</Tooltip>
</Popconfirm>
</Space>
</template>
</Grid>
<!-- 编辑弹窗 -->
<FileEditModal
v-model:open="editModalVisible"
:file-data="currentFileData"
@success="handleEditSuccess"
@cancel="editModalVisible = false"
/>
</Page>
</template>
<style scoped>
.ant-upload-list {
display: none;
}
</style>