import { hasRemoteConfig } from '@/utils/runtime-config.js' import { normalizeBusinessFileAccess } from './business-file-contract.js' import { normalizeGenealogyPathId } from './genealogy-contract.js' import { normalizePersonDocument, normalizePersonDocumentPayload, normalizePersonDocumentResource, normalizePersonDocumentResourcePayload, normalizePersonDocuments } from './person-document-contract.js' import { assertPlainPayload, normalizeResourcePathId } from './request-normalizers.js' import { contentAccessHeader, normalizeContentAccessGrant, normalizeContentPasswordPayload } from './protected-content-contract.js' import { createRequestError, requestStrict } from './request-client.js' const requireRemotePersonDocument = (operation) => { if (hasRemoteConfig()) return throw createRequestError( `重要证件${operation}需要真实服务,当前本地预览不会伪造结果`, operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED' ) } const writePersonDocument = (url, method, data, label, requestOptions) => { if (!hasRemoteConfig()) { throw createRequestError( `${label}写入需要真实服务,当前本地预览不会伪造结果`, 'REMOTE_WRITE_REQUIRED' ) } return requestStrict({ url, method, data }, { requestController: requestOptions.requestController ?? null }) } const deletePersonDocumentEndpoint = async (url, label, requestOptions) => { if (!hasRemoteConfig()) { throw createRequestError( `${label}写入需要真实服务,当前本地预览不会伪造结果`, 'REMOTE_WRITE_REQUIRED' ) } await requestStrict({ url, method: 'DELETE' }, { requireData: false, requestController: requestOptions.requestController ?? null }) return null } const normalizeDocumentIds = (genealogyId, documentId) => ({ genealogyId: normalizeGenealogyPathId(genealogyId), documentId: normalizeResourcePathId(documentId, '重要证件标识') }) export const personDocumentApi = { async getPersonDocuments(genealogyId, query = {}, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) assertPlainPayload(query, new Set(['lineagePersonId']), '重要证件查询') const queryParams = {} if (query.lineagePersonId !== undefined && query.lineagePersonId !== null && query.lineagePersonId !== '') { queryParams.lineagePersonId = normalizeResourcePathId(query.lineagePersonId, '世系人物标识') } requireRemotePersonDocument('读取') const personDocuments = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/person-documents`, method: 'GET', data: queryParams }, { requestController: requestOptions.requestController ?? null }) return normalizePersonDocuments(personDocuments, normalizedGenealogyId) }, async getPersonDocument(genealogyId, documentId, accessToken = '', requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const personDocument = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, method: 'GET', header: contentAccessHeader(accessToken) }, { requestController: requestOptions.requestController ?? null }) return normalizePersonDocument(personDocument, normalizedIds.genealogyId) }, async createPersonDocument(genealogyId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const personDocument = await writePersonDocument( `/genealogy/app/genealogies/${normalizedGenealogyId}/person-documents`, 'POST', normalizePersonDocumentPayload(payload), '重要证件', requestOptions ) return normalizePersonDocument(personDocument, normalizedGenealogyId) }, async updatePersonDocument(genealogyId, documentId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const personDocument = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, 'PUT', normalizePersonDocumentPayload(payload), '重要证件', requestOptions ) return normalizePersonDocument(personDocument, normalizedIds.genealogyId) }, async deletePersonDocument(genealogyId, documentId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}`, '重要证件', requestOptions ) }, async addPersonDocumentResource(genealogyId, documentId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const documentResource = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources`, 'POST', normalizePersonDocumentResourcePayload(payload), '证件文件', requestOptions ) return normalizePersonDocumentResource(documentResource) }, async updatePersonDocumentResource(genealogyId, documentId, resourceId, payload, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') const documentResource = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}`, 'PUT', normalizePersonDocumentResourcePayload(payload), '证件文件', requestOptions ) return normalizePersonDocumentResource(documentResource) }, async deletePersonDocumentResource(genealogyId, documentId, resourceId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}`, '证件文件', requestOptions ) }, async getPersonDocumentResourceAccess(genealogyId, documentId, resourceId, accessToken = '', requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const normalizedResourceId = normalizeResourcePathId(resourceId, '证件文件标识') const fileAccess = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/resources/${normalizedResourceId}/access`, method: 'GET', header: contentAccessHeader(accessToken) }, { requestController: requestOptions.requestController ?? null }) return normalizeBusinessFileAccess( fileAccess, '证件文件访问对象', 'PERSON_DOCUMENT_FILE_ACCESS_INVALID', { required: true } ) }, async setPersonDocumentPassword(genealogyId, documentId, password, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-protection`, 'PUT', normalizeContentPasswordPayload(password), '证件内容密码', requestOptions ) return null }, async unlockPersonDocument(genealogyId, documentId, password, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) const accessGrant = await writePersonDocument( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-unlock`, 'POST', normalizeContentPasswordPayload(password), '证件内容解锁', requestOptions ) return normalizeContentAccessGrant(accessGrant) }, async disablePersonDocumentPassword(genealogyId, documentId, requestOptions = {}) { const normalizedIds = normalizeDocumentIds(genealogyId, documentId) return deletePersonDocumentEndpoint( `/genealogy/app/genealogies/${normalizedIds.genealogyId}/person-documents/${normalizedIds.documentId}/content-protection`, '证件内容密码', requestOptions ) } }