i更新
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import {
|
||||
prescriptionEsignDownload,
|
||||
prescriptionEsignStatus,
|
||||
type PrescriptionEsignFile,
|
||||
type PrescriptionEsignStatus
|
||||
} from '@/api/tcm'
|
||||
|
||||
export function safeEsignAuthUrl(value: unknown): string {
|
||||
if (typeof value !== 'string' || !value.trim() || /[\u0000-\u001f\\]/.test(value)) return ''
|
||||
try {
|
||||
const url = new URL(value)
|
||||
const hosts = ['smlt.esign.cn', 'smlfront.esign.cn', 't.esign.cn', 'idverify.esign.cn']
|
||||
const allowedPort = !url.port || (url.hostname === 'smlfront.esign.cn' && url.port === '8890')
|
||||
return url.protocol === 'https:' && hosts.includes(url.hostname) && allowedPort && !url.username && !url.password ? url.href : ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
export function validateEsignStatus(value: unknown): PrescriptionEsignStatus {
|
||||
const state = value as PrescriptionEsignStatus | null
|
||||
const statuses = ['pending', 'authenticating', 'signing', 'signed', 'failed', 'uncertain', 'voided', 'legacy']
|
||||
if (!state || typeof state !== 'object' || !statuses.includes(state.status)
|
||||
|| !['sandbox', 'production'].includes(state.environment)
|
||||
|| ['enabled', 'required', 'can_sign', 'can_download'].some((key) => typeof (state as any)[key] !== 'boolean')) {
|
||||
throw new Error('无法核对处方签署状态,请刷新后重试')
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
export function requiresOriginalEsignPdf(status?: PrescriptionEsignStatus | null): boolean {
|
||||
try {
|
||||
const value = validateEsignStatus(status)
|
||||
return value.required || value.status !== 'legacy' || value.can_download
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/** Verify the received bytes before creating a download; never render or alter the signed PDF. */
|
||||
export async function verifyEsignPdf(file: PrescriptionEsignFile): Promise<Uint8Array> {
|
||||
if (!/^[a-f\d]{64}$/i.test(file.sha256 || '')) throw new Error('签署文件缺少有效校验信息')
|
||||
if (!globalThis.crypto?.subtle) throw new Error('请通过 HTTPS 访问,以校验签署文件')
|
||||
let bytes: Uint8Array
|
||||
try {
|
||||
bytes = Uint8Array.from(atob(file.content_base64), (c) => c.charCodeAt(0))
|
||||
} catch {
|
||||
throw new Error('签署文件内容无效,请重新下载')
|
||||
}
|
||||
if (String.fromCharCode(...bytes.subarray(0, 5)) !== '%PDF-') {
|
||||
throw new Error('签署文件不是有效 PDF,请重新下载')
|
||||
}
|
||||
const digest = await crypto.subtle.digest('SHA-256', bytes)
|
||||
const hash = Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join('')
|
||||
if (hash !== file.sha256.toLowerCase()) throw new Error('签署文件校验失败,已停止下载,请重试')
|
||||
return bytes
|
||||
}
|
||||
|
||||
export async function downloadOriginalEsignPdf(id: number): Promise<void> {
|
||||
const file = await prescriptionEsignDownload({ id })
|
||||
const bytes = await verifyEsignPdf(file)
|
||||
const url = URL.createObjectURL(new Blob([bytes], { type: 'application/pdf' }))
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = (file.filename || `处方_${id}_已签.pdf`).replace(/[\\/:*?"<>|\u0000-\u001f]/g, '_')
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
link.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
||||
}
|
||||
|
||||
/** Legacy exports may render locally; every other state must preserve the server's original. */
|
||||
export async function downloadPrescriptionPdfIfRequired(id: number): Promise<boolean> {
|
||||
if (!Number.isInteger(id) || id <= 0) throw new Error('缺少处方编号,无法核对签署状态')
|
||||
const status = validateEsignStatus(await prescriptionEsignStatus({ id }))
|
||||
if (!requiresOriginalEsignPdf(status)) return false
|
||||
if (!status.can_download) throw new Error(status.message || '处方尚未签署完成,请完成签署后下载')
|
||||
await downloadOriginalEsignPdf(id)
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user