feat(api): 接入医签通业务接口与类型

This commit is contained in:
yelan
2026-09-01 10:11:49 +08:00
parent c34ef6f79a
commit 2e2d2250dd
12 changed files with 1677 additions and 56 deletions
+121
View File
@@ -0,0 +1,121 @@
import axios from 'axios'
import { unwrapApiResponse } from '@/utils/api-response'
import { request } from '@/utils/request'
import type {
ApiPageResponse,
ApiResponseOf,
SignArtifactResponseDto,
SignArtifactType,
SigningArtifact,
} from './types'
const useMockData = import.meta.env.VITE_USE_MOCK !== 'false'
type ArtifactCollection = SignArtifactResponseDto[] | ApiPageResponse<SignArtifactResponseDto>
const artifactLabels: Record<string, string> = {
ORIGINAL_PDF: 'PDF 原件',
SIGNATURE_IMAGE: '签名原图',
SIGNED_PDF: '签署后 PDF',
}
function formatDateTime(value: string | null | undefined) {
if (!value) {
return ''
}
const date = new Date(value)
if (Number.isNaN(date.getTime())) {
return value
.replace('T', ' ')
.replace(/\.\d+Z$/, '')
.replace(/Z$/, '')
}
const pad = (part: number) => String(part).padStart(2, '0')
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(
date.getHours(),
)}:${pad(date.getMinutes())}`
}
function getCollectionRecords(collection: ArtifactCollection) {
return Array.isArray(collection) ? collection : collection.records
}
function normalizeArtifact(dto: SignArtifactResponseDto): SigningArtifact {
const artifactType: SignArtifactType = dto.artifactType ?? dto.type ?? 'UNKNOWN'
const label = artifactLabels[artifactType] ?? '签署文件'
const mimeType = dto.mimeType ?? dto.contentType ?? 'application/octet-stream'
const extension =
mimeType === 'application/pdf' ? 'pdf' : mimeType === 'image/png' ? 'png' : 'bin'
return {
id: dto.id,
taskId: dto.taskId,
artifactType,
label,
fileName: dto.fileName || `${label}.${extension}`,
mimeType,
size: dto.size ?? dto.byteSize ?? dto.fileSize ?? 0,
sha256: dto.sha256 ?? dto.contentSha256 ?? '',
createdAt: formatDateTime(dto.createdAt),
}
}
export const isArtifactMockEnabled = useMockData
export async function getSigningArtifacts(taskId: string): Promise<SigningArtifact[]> {
if (useMockData) {
return []
}
try {
const response = await request.get<ApiResponseOf<ArtifactCollection>>(
`/v1/sign-artifacts/task/${encodeURIComponent(taskId)}`,
)
return getCollectionRecords(unwrapApiResponse(response)).map(normalizeArtifact)
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
return []
}
throw error
}
}
export async function getSigningArtifact(
taskId: string,
artifactId: string,
): Promise<SigningArtifact | null> {
if (useMockData) {
return null
}
try {
const response = await request.get<ApiResponseOf<SignArtifactResponseDto>>(
`/v1/sign-artifacts/task/${encodeURIComponent(taskId)}/${encodeURIComponent(artifactId)}`,
)
return normalizeArtifact(unwrapApiResponse(response))
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
return null
}
throw error
}
}
export function downloadSigningArtifact(artifactId: string): Promise<Blob> {
if (useMockData) {
return Promise.resolve(new Blob(['Mock 签署文件,仅用于界面演示。'], { type: 'text/plain' }))
}
return request.get<Blob>(`/v1/sign-artifacts/${encodeURIComponent(artifactId)}/download`, {
responseType: 'blob',
})
}