1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
| import type { DmsPreviewResourceType } from '#/api/x/dms/preview';
|
| import { createDmsFilePreviewTicket } from '#/api/x/dms/preview';
| import { onlineUtils } from '#/utils/jnpf';
|
| const SUPPORTED_EXTENSIONS = new Set([
| 'csv',
| 'doc',
| 'docm',
| 'docx',
| 'dot',
| 'dotx',
| 'htm',
| 'html',
| 'odp',
| 'ods',
| 'odt',
| 'otp',
| 'ots',
| 'ott',
| 'pdf',
| 'pot',
| 'potx',
| 'pps',
| 'ppsx',
| 'ppt',
| 'pptm',
| 'pptx',
| 'rtf',
| 'txt',
| 'xls',
| 'xlsm',
| 'xlsx',
| 'xlt',
| 'xltx',
| ]);
| const openingResources = new Set<string>();
|
| export interface OpenDmsFilePreviewOptions {
| fileName: string;
| resourceId: string;
| resourceType: DmsPreviewResourceType;
| }
|
| export function supportsOnlyOfficePreview(fileName: string) {
| const normalized = fileName.trim().toLowerCase();
| const extension = normalized.includes('.') ? normalized.split('.').pop() || '' : '';
| return SUPPORTED_EXTENSIONS.has(extension);
| }
|
| export async function openDmsFilePreview(options: OpenDmsFilePreviewOptions) {
| if (!supportsOnlyOfficePreview(options.fileName)) {
| throw new Error('OnlyOffice 不支持该文件格式');
| }
| const openingKey = `${options.resourceType}:${options.resourceId}`;
| if (openingResources.has(openingKey)) return false;
| openingResources.add(openingKey);
| try {
| const response = await createDmsFilePreviewTicket({
| operationId: crypto.randomUUID(),
| resourceId: options.resourceId,
| resourceType: options.resourceType,
| });
| const ticket = response.data;
| if (!ticket?.accessTicket || !ticket.fileVersionId || !ticket.fileName) {
| throw new Error('后端返回的 DMS 预览票据不完整');
| }
| if (!supportsOnlyOfficePreview(ticket.fileName)) {
| throw new Error('OnlyOffice 不支持该文件格式');
| }
| onlineUtils.openOfficeDocument({
| dmsAccessTicket: ticket.accessTicket,
| file: {
| fileId: ticket.fileVersionId,
| fileSize: ticket.sizeBytes,
| name: ticket.fileName,
| },
| fileType: 'dms',
| mode: 'view',
| title: ticket.fileName,
| });
| return true;
| } finally {
| openingResources.delete(openingKey);
| }
| }
|
| export function dmsPreviewErrorMessage(error: unknown) {
| return error instanceof Error && error.message ? error.message : '文件预览失败';
| }
|
|