刘光辉
3 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
91
92
93
94
95
96
97
98
import type { OnlyOfficeConfigQuery } from '#/api/x/onlyoffice';
 
export interface ElnRecordDocument {
  fileExtension?: string;
  fileId: string;
  fileSize?: number | string;
  name: string;
}
 
export interface ElnRecordOpenParams {
  document: ElnRecordDocument;
  mode: 'edit' | 'view';
  recordId: string;
  revisionId: string;
}
 
export const ELN_RECORD_FILL_STATE_MESSAGE = 'jnpf:eln-record-fill-state';
 
export interface ElnRecordFillStateMessage {
  bizDataId: string;
  documentId: string;
  pendingCount: number;
  type: typeof ELN_RECORD_FILL_STATE_MESSAGE;
  version: 1;
}
 
export class ElnRecordOpenParamError extends Error {}
 
export function parseElnRecordFillStateMessage(
  value: unknown,
  expected: Pick<ElnRecordOpenParams, 'document' | 'recordId'>,
): ElnRecordFillStateMessage | undefined {
  if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
  const message = value as Record<string, unknown>;
  if (
    message.type !== ELN_RECORD_FILL_STATE_MESSAGE ||
    message.version !== 1 ||
    message.documentId !== expected.document.fileId ||
    message.bizDataId !== expected.recordId ||
    !Number.isSafeInteger(message.pendingCount) ||
    Number(message.pendingCount) < 0
  ) {
    return undefined;
  }
  return message as unknown as ElnRecordFillStateMessage;
}
 
function requiredString(value: unknown, label: string) {
  if (typeof value !== 'string' || !value.trim()) {
    throw new ElnRecordOpenParamError(`缺少 ELN 记录${label}`);
  }
  return value.trim();
}
 
function asDocument(value: unknown): ElnRecordDocument {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    throw new ElnRecordOpenParamError('缺少 ELN 记录文档');
  }
  const document = value as Record<string, unknown>;
  return {
    fileExtension: typeof document.fileExtension === 'string' ? document.fileExtension : undefined,
    fileId: requiredString(document.fileId, '文档 fileId'),
    fileSize: typeof document.fileSize === 'number' || typeof document.fileSize === 'string' ? document.fileSize : undefined,
    name: requiredString(document.name, '文档名称'),
  };
}
 
export function resolveElnRecordOpenParams(getParam: (name: string) => unknown): ElnRecordOpenParams {
  return {
    document: asDocument(getParam('document')),
    mode: getParam('mode') === 'view' ? 'view' : 'edit',
    recordId: requiredString(getParam('recordId'), 'ID'),
    revisionId: requiredString(getParam('revisionId'), '修订 ID'),
  };
}
 
export function toElnRecordOfficeConfigQuery(params: ElnRecordOpenParams): OnlyOfficeConfigQuery {
  return {
    bizDataId: params.recordId,
    bizModule: 'eln.record',
    bizScene: 'eln.record.fill',
    fileId: params.document.fileId,
    fileName: params.document.name,
    fileType: 'annex',
    mode: params.mode,
  };
}
 
export function unwrapElnRecordEditorConfig(response: unknown): Record<string, any> {
  const config = ((response as undefined | { data?: unknown })?.data ?? response) as Record<string, any> | undefined;
  if (!config?.document?.key || !config?.token) {
    throw new Error('后端返回的编辑器配置不完整(缺 document.key 或 token)');
  }
  if (!['edit', 'view'].includes(config.editorConfig?.mode)) {
    throw new Error('后端返回的编辑器配置不完整(缺 editorConfig.mode)');
  }
  return config;
}