feat(@vben/common-ui): 优化文档预览和下载功能
This commit is contained in:
parent
a08474d32c
commit
33d8241ce4
@ -7,8 +7,8 @@ import VueOfficeDocx from '@vue-office/docx';
|
|||||||
|
|
||||||
import '@vue-office/docx/lib/index.css';
|
import '@vue-office/docx/lib/index.css';
|
||||||
|
|
||||||
const url = ref('');
|
// const url = ref('');
|
||||||
const docx = ref<any>('/docx/027c6b7c-fea6-4964-839b-27857c4d3181.docx');
|
const docx = ref<any>(`/docx/027c6b7c-fea6-4964-839b-27857c4d3181.docx`);
|
||||||
const pptStyle = ref({
|
const pptStyle = ref({
|
||||||
height: 'calc(100vh - 100px)',
|
height: 'calc(100vh - 100px)',
|
||||||
width: '100%',
|
width: '100%',
|
||||||
@ -29,7 +29,11 @@ const [Drawer, drawerApi] = useVbenDrawer({
|
|||||||
},
|
},
|
||||||
onOpenChange(isOpen: boolean) {
|
onOpenChange(isOpen: boolean) {
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
url.value = drawerApi.getData<Record<string, any>>();
|
const data = drawerApi.getData<Record<string, any>>();
|
||||||
|
if (data) {
|
||||||
|
docx.value = `/docx/${data}`; // 更新 docx 的值
|
||||||
|
}
|
||||||
|
// url.value = drawerApi.getData<Record<string, any>>();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -67,9 +67,12 @@ const [PreviewDrawer, previewDrawerApi] = useVbenDrawer({
|
|||||||
connectedComponent: WordPreview,
|
connectedComponent: WordPreview,
|
||||||
// placement: 'left',
|
// placement: 'left',
|
||||||
});
|
});
|
||||||
|
function openPreviewDrawer(
|
||||||
function openPreviewDrawer(placement: DrawerPlacement = 'right') {
|
placement: DrawerPlacement = 'right',
|
||||||
previewDrawerApi.setState({ placement }).open();
|
filename?: string,
|
||||||
|
) {
|
||||||
|
const fileData = filename.value;
|
||||||
|
previewDrawerApi.setState({ placement }).setData(fileData).open();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startFetching = async () => {
|
const startFetching = async () => {
|
||||||
@ -111,7 +114,29 @@ const startFetching = async () => {
|
|||||||
content: value.value || '',
|
content: value.value || '',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
fetchResult.value = '/static/9e1c421e-991c-411f-8176-6350a97e70f3.docx';
|
const { answer } = res;
|
||||||
|
|
||||||
|
// 判断是否是以 .docx 结尾的 URL
|
||||||
|
function isDocxURL(str: string): boolean {
|
||||||
|
return str.endsWith('.docx');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用正则提取 /static/ 后面的文件名部分
|
||||||
|
function extractDocxFilename(url: string): null | string {
|
||||||
|
const match = url.match(/\/static\/([a-f0-9-]+\.docx)$/i);
|
||||||
|
if (match) {
|
||||||
|
return match[1]; // 返回类似:9e1c421e-991c-411f-8176-6350a97e70f3.docx
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = ref('');
|
||||||
|
|
||||||
|
if (answer && isDocxURL(answer)) {
|
||||||
|
filename.value = extractDocxFilename(answer);
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchResult.value = `/static/${filename.value}`;
|
||||||
resultItems.value.push({
|
resultItems.value.push({
|
||||||
key: resultItems.value.length + 1,
|
key: resultItems.value.length + 1,
|
||||||
role: 'ai',
|
role: 'ai',
|
||||||
@ -123,7 +148,7 @@ const startFetching = async () => {
|
|||||||
size: 'nomarl',
|
size: 'nomarl',
|
||||||
type: 'primary',
|
type: 'primary',
|
||||||
onClick: () => {
|
onClick: () => {
|
||||||
openPreviewDrawer('right');
|
openPreviewDrawer('right', filename);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'文档预览',
|
'文档预览',
|
||||||
@ -136,7 +161,15 @@ const startFetching = async () => {
|
|||||||
style: {
|
style: {
|
||||||
marginLeft: '10px',
|
marginLeft: '10px',
|
||||||
},
|
},
|
||||||
onClick: () => {},
|
onClick: () => {
|
||||||
|
// 创建隐藏的 <a> 标签用于触发下载
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = answer; // 设置下载链接
|
||||||
|
link.download = filename; // 设置下载文件名
|
||||||
|
document.body.append(link); // 将 <a> 标签添加到页面中
|
||||||
|
link.click(); // 触发点击事件开始下载
|
||||||
|
link.remove(); // 下载完成后移除 <a> 标签
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'文档下载',
|
'文档下载',
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user