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;
|
}
|