开发商务模块文件预览功能

This commit is contained in:
chabai 2025-08-08 10:26:36 +08:00
parent 19cdc0998f
commit e098cfd8fa
2 changed files with 152 additions and 18 deletions

View File

@ -156,10 +156,10 @@ export function deleteFileApi(fileId: string) {
}) })
} }
// 预览文件(后端没有提供预览接口,使用下载接口) // 预览文件
export function previewFileApi(fileId: string) { export function previewFileApi(fileId: string) {
return request({ return request({
url: '/businessData/file/download', url: '/businessData/file/preview',
method: 'get', method: 'get',
params: { params: {
fileId: fileId fileId: fileId

View File

@ -1333,31 +1333,157 @@ const resetUpload = () => {
// //
const handlePreview = async (file) => { const handlePreview = async (file) => {
try { try {
console.log('开始预览文件:', file);
Message.loading('正在加载预览...', 0); //
const blob = await previewFileApi(file.fileId); const blob = await previewFileApi(file.fileId);
Message.clear(); //
if (!blob) {
Message.error('无法获取文件数据');
return;
}
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const fileName = file.fileName || file.name;
const ext = getFileExtension(fileName).toLowerCase();
console.log('文件扩展名:', ext);
// //
const ext = getFileExtension(file.fileName || file.name).toLowerCase(); if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(ext)) {
if (['jpg', 'jpeg', 'png', 'gif', 'bmp'].includes(ext)) { // - 使
// showImagePreview(url, fileName);
const img = new Image();
img.src = url;
Modal.info({
title: '图片预览',
content: img,
width: '80%'
});
} else if (ext === 'pdf') { } else if (ext === 'pdf') {
// PDF // PDF
window.open(url, '_blank'); window.open(url, '_blank');
} else { Message.success('PDF文件已在新窗口打开');
// } else if (['txt', 'md', 'json', 'xml', 'csv'].includes(ext)) {
Message.info(`该文件类型不支持预览,将为您下载文件: ${file.fileName || file.name}`); //
showTextPreview(blob, fileName);
} else if (['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'].includes(ext)) {
// Office
Modal.confirm({
title: '文件预览',
content: `${fileName} 是Office文档格式您可以选择`,
okText: '下载查看',
cancelText: '取消',
onOk: () => {
handleDownload(file); handleDownload(file);
} }
});
} else {
//
Modal.confirm({
title: '文件预览',
content: `文件类型 ${ext.toUpperCase()} 暂不支持在线预览,是否下载查看?`,
okText: '下载',
cancelText: '取消',
onOk: () => {
handleDownload(file);
}
});
}
// URL
setTimeout(() => {
URL.revokeObjectURL(url);
}, 10000);
} catch (error) { } catch (error) {
Message.clear(); //
console.error('预览失败:', error); console.error('预览失败:', error);
Message.error('预览文件失败');
//
if (error.response?.status === 404) {
Message.error('文件不存在或已被删除');
} else if (error.response?.status === 403) {
Message.error('没有权限访问该文件');
} else if (error.response?.status === 500) {
Message.error('服务器内部错误,请稍后重试');
} else {
Message.error('预览文件失败,请检查网络连接');
}
}
};
//
const showImagePreview = (url, fileName) => {
const container = document.createElement('div');
container.style.cssText = `
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
box-sizing: border-box;
`;
const img = document.createElement('img');
img.src = url;
img.style.cssText = `
max-width: 100%;
max-height: 70vh;
object-fit: contain;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
`;
const title = document.createElement('div');
title.textContent = fileName;
title.style.cssText = `
margin-top: 16px;
font-size: 14px;
color: #666;
text-align: center;
word-break: break-all;
`;
container.appendChild(img);
container.appendChild(title);
Modal.info({
title: '图片预览',
content: container,
width: '80%',
maskClosable: true,
footer: null
});
};
//
const showTextPreview = async (blob, fileName) => {
try {
const text = await blob.text();
const container = document.createElement('div');
container.style.cssText = `
width: 100%;
height: 400px;
border: 1px solid #e5e5e5;
border-radius: 6px;
padding: 16px;
background-color: #fafafa;
font-family: 'Courier New', monospace;
font-size: 13px;
line-height: 1.6;
overflow: auto;
white-space: pre-wrap;
word-break: break-word;
`;
container.textContent = text;
Modal.info({
title: `文本预览 - ${fileName}`,
content: container,
width: '70%',
maskClosable: true,
footer: null
});
} catch (error) {
console.error('文本预览失败:', error);
Message.error('无法读取文本内容');
} }
}; };
@ -1771,6 +1897,8 @@ onMounted(() => {
min-height: 300px; min-height: 300px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
position: relative;
height: 100%;
} }
/* 表格容器 */ /* 表格容器 */
@ -1780,9 +1908,10 @@ onMounted(() => {
margin-top: 16px; margin-top: 16px;
border-radius: 8px; border-radius: 8px;
border: 1px solid var(--color-border); border: 1px solid var(--color-border);
overflow: auto; overflow-y: auto;
background-color: var(--color-bg-1); background-color: var(--color-bg-1);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
padding-bottom: 80px; /* 为分页留出空间 */
} }
/* 表头行样式 */ /* 表头行样式 */
@ -2553,7 +2682,12 @@ onMounted(() => {
/* 文件分页样式 */ /* 文件分页样式 */
.file-pagination { .file-pagination {
margin-top: 24px; position: absolute;
bottom: 0;
left: 0;
right: 0;
z-index: 10;
margin-top: 0;
padding: 16px 0; padding: 16px 0;
display: flex; display: flex;
justify-content: center; justify-content: center;