更新
This commit is contained in:
@@ -0,0 +1,406 @@
|
||||
<template>
|
||||
<el-dialog
|
||||
:model-value="true"
|
||||
:title="reviewer ? '审核签署身份' : '医生电子签署身份'"
|
||||
:width="frameUrl ? 'min(760px, 94vw)' : 'min(520px, 94vw)'"
|
||||
top="5vh"
|
||||
append-to-body
|
||||
:close-on-click-modal="false"
|
||||
:before-close="dismiss"
|
||||
>
|
||||
<p class="doctor-esign__message" role="status">{{ guidance }}</p>
|
||||
<dl v-if="state?.enabled && state.status === 'ready'" class="doctor-esign__expiry">
|
||||
<template v-if="state.cert_expires_at">
|
||||
<dt>签署证书有效期至</dt>
|
||||
<dd>{{ formatExpiry(state.cert_expires_at) }}</dd>
|
||||
</template>
|
||||
<template v-if="grantActive">
|
||||
<dt>快捷签授权有效期至</dt>
|
||||
<dd>{{ formatExpiry(state.quicksign_expires_at) }}</dd>
|
||||
<dt>快捷签剩余次数</dt>
|
||||
<dd>{{ state.quicksign_remaining_times }}</dd>
|
||||
</template>
|
||||
</dl>
|
||||
<p v-if="error" class="doctor-esign__error" role="alert">{{ error }}</p>
|
||||
<label v-if="needsMobile" class="doctor-esign__field">
|
||||
本人手机号码
|
||||
<el-input
|
||||
v-model="mobile"
|
||||
:disabled="!!pending"
|
||||
type="tel"
|
||||
inputmode="numeric"
|
||||
maxlength="11"
|
||||
autocomplete="off"
|
||||
placeholder="请输入 11 位大陆手机号码"
|
||||
/>
|
||||
</label>
|
||||
<label v-if="needsIdentityNumber" class="doctor-esign__field">
|
||||
本次认证使用的身份证号码
|
||||
<el-input
|
||||
v-model="identityNumber"
|
||||
:disabled="!!pending"
|
||||
type="password"
|
||||
maxlength="18"
|
||||
autocomplete="off"
|
||||
:spellcheck="false"
|
||||
placeholder="请输入 15 或 18 位大陆居民身份证号码"
|
||||
/>
|
||||
</label>
|
||||
<iframe
|
||||
v-if="frameUrl"
|
||||
:src="frameUrl"
|
||||
class="doctor-esign__frame"
|
||||
title="e签宝本人身份核验"
|
||||
sandbox="allow-forms allow-scripts allow-same-origin"
|
||||
referrerpolicy="no-referrer"
|
||||
@error="authenticationUnavailable"
|
||||
/>
|
||||
<template #footer>
|
||||
<div class="doctor-esign__actions">
|
||||
<el-button v-if="needsMobile || canAuthorizeQuick" type="primary" :disabled="!!pending" @click="start">
|
||||
{{ startLabel }}
|
||||
</el-button>
|
||||
<el-button v-if="canResume" type="primary" :disabled="!!pending" @click="openAuthentication">
|
||||
{{ state?.quicksign_authorizing ? '继续快捷签授权' : '继续人脸认证' }}
|
||||
</el-button>
|
||||
<el-button v-if="canComplete" type="primary" :disabled="!!pending" @click="complete">
|
||||
{{ state?.quicksign_authorizing ? '确认快捷签授权' : '确认绑定签署身份' }}
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="canResume && !state?.quicksign_authorizing"
|
||||
:disabled="!!pending"
|
||||
@click="enterMobile"
|
||||
>重新输入手机号</el-button>
|
||||
<el-button :disabled="!!pending" @click="refresh">检查状态</el-button>
|
||||
<el-button v-if="frameUrl" link @click="authenticationUnavailable">验证页面不可用</el-button>
|
||||
<el-button @click="dismiss">关闭</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
import {
|
||||
prescriptionEsignDoctorComplete,
|
||||
prescriptionEsignDoctorStart,
|
||||
prescriptionEsignDoctorStatus,
|
||||
type PrescriptionEsignDoctorStatus
|
||||
} from '@/api/tcm'
|
||||
import { safeEsignAuthUrl } from '@/utils/prescription-esign'
|
||||
|
||||
const props = defineProps<{ serverTimeOffsetMs: number; reviewer?: boolean }>()
|
||||
const emit = defineEmits<{ (event: 'close'): void }>()
|
||||
type DoctorRequest = 'status' | 'start' | 'complete'
|
||||
|
||||
const enrollmentStatuses = ['unbound', 'needs_mobile', 'renewal_required']
|
||||
const statuses = [...enrollmentStatuses, 'authenticating', 'binding_required', 'ready']
|
||||
const pollInterval = 5000
|
||||
const authenticationTimeout = 10 * 60 * 1000
|
||||
const expiryFormatter = new Intl.DateTimeFormat('zh-CN', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hourCycle: 'h23'
|
||||
})
|
||||
|
||||
const state = ref<ReturnType<typeof validateStatus> | null>(null)
|
||||
const pending = ref<DoctorRequest | null>(null)
|
||||
const frameUrl = ref('')
|
||||
const mobile = ref('')
|
||||
const identityNumber = ref('')
|
||||
const editingMobile = ref(false)
|
||||
const error = ref('')
|
||||
const now = ref(Date.now() + props.serverTimeOffsetMs)
|
||||
|
||||
const authUrl = computed(() => state.value?.auth_url ?? '')
|
||||
const authenticating = computed(() => state.value?.enabled === true && state.value.status === 'authenticating')
|
||||
const canResume = computed(() => authenticating.value && !!authUrl.value && !frameUrl.value && !editingMobile.value)
|
||||
const canComplete = computed(() => state.value?.enabled === true && state.value.status === 'binding_required')
|
||||
const needsIdentityNumber = computed(() => canComplete.value && !state.value?.quicksign_authorizing
|
||||
&& state.value?.identity_details_required === true)
|
||||
const needsMobile = computed(() => state.value?.enabled === true
|
||||
&& (enrollmentStatuses.includes(state.value.status) || editingMobile.value))
|
||||
const quickReady = computed(() => state.value?.enabled === true && state.value.status === 'ready'
|
||||
&& state.value.quicksign_enabled === true)
|
||||
const grantActive = computed(() => quickReady.value && !!state.value && !state.value.quicksign_unavailable_reason
|
||||
&& (state.value.quicksign_remaining_times ?? 0) > 0 && state.value.quicksign_expires_at * 1000 > now.value)
|
||||
const canAuthorizeQuick = computed(() => quickReady.value && !grantActive.value)
|
||||
const startLabel = computed(() => {
|
||||
if (canAuthorizeQuick.value) return '授权快捷签'
|
||||
if (state.value?.status === 'renewal_required') return '开始续期认证'
|
||||
return '开始人脸认证'
|
||||
})
|
||||
const guidance = computed(() => {
|
||||
const current = state.value
|
||||
if (!current) return pending.value === 'status' ? '正在查询医生签署身份…' : ''
|
||||
if (!current.enabled) return '当前环境未启用医生电子签署身份。'
|
||||
switch (current.status) {
|
||||
case 'unbound':
|
||||
return '首次电子签署前,请验证本人身份并绑定签署证书。'
|
||||
case 'needs_mobile':
|
||||
return '请使用本人手机号码完成签署身份验证。'
|
||||
case 'renewal_required':
|
||||
return '签署证书需要续期,请使用本人手机号码完成人脸认证。'
|
||||
case 'authenticating':
|
||||
if (editingMobile.value) return '请使用本人手机号码完成人脸认证。'
|
||||
if (current.authUrlUnavailable) return '验证链接不可用,请检查状态后重试。'
|
||||
if (!authUrl.value) return '认证请求处理中,请稍后检查状态。'
|
||||
if (!frameUrl.value) return current.quicksign_authorizing ? '请继续完成快捷签授权。' : '请继续完成本人人脸认证。'
|
||||
return current.quicksign_authorizing
|
||||
? '请使用手机支付宝扫码刷脸,并按 e签宝页面提示确认授权。'
|
||||
: '请使用手机支付宝扫描 e签宝页面中的二维码并完成人脸识别。'
|
||||
case 'binding_required':
|
||||
if (current.quicksign_authorizing) return '人脸认证已通过,请确认启用本人的快捷签。'
|
||||
if (needsIdentityNumber.value) return '人脸认证已通过。首次绑定证书需要补充与本次认证一致的身份证号码。'
|
||||
return '人脸认证已通过,请确认完成本人的签署身份认证。'
|
||||
case 'ready': {
|
||||
if (!quickReady.value) return '医生签署身份已就绪。'
|
||||
if (grantActive.value) return props.reviewer ? '快捷签授权有效。每张处方由审核人本人确认签名。'
|
||||
: '快捷签授权有效。每张处方仍需开方医生本人确认签名。'
|
||||
const reason = current.quicksign_unavailable_reason
|
||||
if (reason === 'expired' || (current.quicksign_expires_at > 0 && current.quicksign_expires_at * 1000 <= now.value)) {
|
||||
return '快捷签授权已过期,请重新授权。'
|
||||
}
|
||||
if (reason === 'exhausted') return '快捷签授权次数已用完,请重新授权。'
|
||||
if (reason === 'invalid') return '快捷签授权已失效,请重新授权。'
|
||||
return '签署身份已就绪。请授权使用本人的快捷签,每张处方仍需本人确认签名。'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let closed = false
|
||||
let generation = 0
|
||||
let statusRequest: AbortController | undefined
|
||||
let pollTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let deadlineTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let grantExpiryTimer: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
function formatExpiry(timestamp: number) {
|
||||
return `${expiryFormatter.format(timestamp * 1000)}(北京时间)`
|
||||
}
|
||||
|
||||
function validTime(value: number) {
|
||||
return Number.isSafeInteger(value) && value >= 0 && value <= 253402300799
|
||||
}
|
||||
|
||||
function validateStatus(result: PrescriptionEsignDoctorStatus) {
|
||||
const {
|
||||
enabled, status, cert_expires_at,
|
||||
identity_details_required = false, quicksign_enabled = false, quicksign_authorizing = false,
|
||||
quicksign_expires_at = 0, quicksign_remaining_times = 0, quicksign_unavailable_reason = ''
|
||||
} = result
|
||||
if (!statuses.includes(status) || typeof enabled !== 'boolean'
|
||||
|| typeof quicksign_enabled !== 'boolean' || typeof quicksign_authorizing !== 'boolean'
|
||||
|| !validTime(cert_expires_at) || !validTime(quicksign_expires_at)
|
||||
|| typeof identity_details_required !== 'boolean'
|
||||
|| (identity_details_required && (!enabled || status !== 'binding_required' || quicksign_authorizing))
|
||||
|| (quicksign_authorizing && (!enabled || !quicksign_enabled || !['authenticating', 'binding_required'].includes(status)))
|
||||
|| !Number.isSafeInteger(quicksign_remaining_times) || quicksign_remaining_times < 0
|
||||
|| !['', 'missing', 'expired', 'exhausted', 'invalid'].includes(quicksign_unavailable_reason)) {
|
||||
throw new Error('医生签署状态无效')
|
||||
}
|
||||
const providerUrl = enabled && status === 'authenticating' ? result.auth_url : undefined
|
||||
const auth_url = safeEsignAuthUrl(providerUrl)
|
||||
return {
|
||||
enabled, status, cert_expires_at, identity_details_required, quicksign_enabled, quicksign_authorizing,
|
||||
quicksign_expires_at, quicksign_remaining_times, quicksign_unavailable_reason, auth_url,
|
||||
authUrlUnavailable: Boolean(providerUrl && !auth_url)
|
||||
}
|
||||
}
|
||||
|
||||
function clearInputs() {
|
||||
mobile.value = ''
|
||||
identityNumber.value = ''
|
||||
}
|
||||
|
||||
function stopAuthentication() {
|
||||
clearTimeout(pollTimer)
|
||||
clearTimeout(deadlineTimer)
|
||||
frameUrl.value = ''
|
||||
}
|
||||
|
||||
function cancelWork() {
|
||||
generation++
|
||||
statusRequest?.abort()
|
||||
statusRequest = undefined
|
||||
pending.value = null
|
||||
clearInputs()
|
||||
stopAuthentication()
|
||||
}
|
||||
|
||||
function clearStatus() {
|
||||
cancelWork()
|
||||
clearTimeout(grantExpiryTimer)
|
||||
state.value = null
|
||||
editingMobile.value = false
|
||||
error.value = ''
|
||||
}
|
||||
|
||||
function dismiss() {
|
||||
closed = true
|
||||
clearStatus()
|
||||
emit('close')
|
||||
}
|
||||
|
||||
function canAct() {
|
||||
return !closed && document.visibilityState === 'visible'
|
||||
}
|
||||
|
||||
function accept(result: ReturnType<typeof validateStatus>) {
|
||||
clearTimeout(grantExpiryTimer)
|
||||
now.value = Date.now() + props.serverTimeOffsetMs
|
||||
editingMobile.value = false
|
||||
state.value = result
|
||||
if (!authenticating.value || (frameUrl.value && frameUrl.value !== authUrl.value)) stopAuthentication()
|
||||
if (grantActive.value) {
|
||||
const expiresIn = result.quicksign_expires_at * 1000 - now.value
|
||||
if (expiresIn <= 2147483647) {
|
||||
grantExpiryTimer = setTimeout(() => { now.value = Date.now() + props.serverTimeOffsetMs }, expiresIn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function schedulePoll() {
|
||||
if (frameUrl.value) pollTimer = setTimeout(() => void refresh(), pollInterval)
|
||||
}
|
||||
|
||||
function openAuthentication() {
|
||||
if (!canAct() || pending.value || !canResume.value) return
|
||||
frameUrl.value = authUrl.value
|
||||
error.value = ''
|
||||
deadlineTimer = setTimeout(() => {
|
||||
cancelWork()
|
||||
error.value = '自动检查已结束,请检查状态后继续。'
|
||||
}, authenticationTimeout)
|
||||
schedulePoll()
|
||||
}
|
||||
|
||||
function authenticationUnavailable() {
|
||||
if (!frameUrl.value) return
|
||||
cancelWork()
|
||||
error.value = '无法加载 e签宝认证页面,请检查状态后重试。'
|
||||
}
|
||||
|
||||
function enterMobile() {
|
||||
if (!canAct() || pending.value || !canResume.value || state.value?.quicksign_authorizing) return
|
||||
editingMobile.value = true
|
||||
error.value = ''
|
||||
}
|
||||
|
||||
async function submit(
|
||||
kind: DoctorRequest,
|
||||
request: () => Promise<PrescriptionEsignDoctorStatus>
|
||||
) {
|
||||
if (!canAct() || pending.value) return
|
||||
const current = generation
|
||||
clearInputs()
|
||||
clearTimeout(pollTimer)
|
||||
error.value = ''
|
||||
pending.value = kind
|
||||
try {
|
||||
const result = validateStatus(await request())
|
||||
if (current !== generation || !canAct()) return
|
||||
accept(result)
|
||||
} catch {
|
||||
if (current !== generation || !canAct()) return
|
||||
clearStatus()
|
||||
error.value = '医生签署身份状态待确认,请检查状态后继续。'
|
||||
return
|
||||
} finally {
|
||||
if (current === generation) pending.value = null
|
||||
}
|
||||
if (kind === 'start') openAuthentication()
|
||||
else schedulePoll()
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
if (!canAct() || pending.value) return
|
||||
const controller = new AbortController()
|
||||
statusRequest = controller
|
||||
try {
|
||||
await submit('status', () => prescriptionEsignDoctorStatus(controller.signal))
|
||||
} finally {
|
||||
if (statusRequest === controller) statusRequest = undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function start() {
|
||||
if (!canAct() || pending.value) return
|
||||
if (canAuthorizeQuick.value) {
|
||||
await submit('start', () => prescriptionEsignDoctorStart({}))
|
||||
} else if (needsMobile.value) {
|
||||
const number = mobile.value.trim()
|
||||
clearInputs()
|
||||
if (!/^1[3-9]\d{9}$/.test(number)) {
|
||||
error.value = '请输入有效的大陆手机号码。'
|
||||
return
|
||||
}
|
||||
await submit('start', () => prescriptionEsignDoctorStart({ mobile: number }))
|
||||
}
|
||||
}
|
||||
|
||||
async function complete() {
|
||||
if (!canAct() || pending.value || !canComplete.value) return
|
||||
if (needsIdentityNumber.value) {
|
||||
const number = identityNumber.value.trim().toUpperCase()
|
||||
clearInputs()
|
||||
if (!/^(?:\d{15}|\d{17}[\dX])$/.test(number)) {
|
||||
error.value = '请输入本次认证使用的 15 或 18 位大陆居民身份证号码。'
|
||||
return
|
||||
}
|
||||
await submit('complete', () => prescriptionEsignDoctorComplete({ identity_number: number }))
|
||||
} else {
|
||||
await submit('complete', () => prescriptionEsignDoctorComplete({}))
|
||||
}
|
||||
}
|
||||
|
||||
function onVisibilityChange() {
|
||||
if (document.visibilityState === 'visible') void refresh()
|
||||
else clearStatus()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('visibilitychange', onVisibilityChange)
|
||||
void refresh()
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
closed = true
|
||||
clearStatus()
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.doctor-esign__message, .doctor-esign__error {
|
||||
margin: 0 0 12px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.doctor-esign__message:empty { display: none; }
|
||||
.doctor-esign__error { color: var(--el-color-danger); }
|
||||
.doctor-esign__expiry {
|
||||
display: grid;
|
||||
grid-template-columns: auto minmax(0, 1fr);
|
||||
gap: 8px 16px;
|
||||
margin: 0 0 12px;
|
||||
padding-top: 12px;
|
||||
border-top: 1px solid var(--el-border-color-light);
|
||||
}
|
||||
.doctor-esign__expiry dt { color: var(--el-text-color-secondary); }
|
||||
.doctor-esign__expiry dd { margin: 0; }
|
||||
.doctor-esign__field { display: grid; gap: 6px; max-width: 350px; }
|
||||
.doctor-esign__frame {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: min(60vh, 620px);
|
||||
border: 1px solid var(--el-border-color-light);
|
||||
border-radius: 4px;
|
||||
background: white;
|
||||
}
|
||||
.doctor-esign__actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
|
||||
.doctor-esign__actions .el-button + .el-button { margin-left: 0; }
|
||||
</style>
|
||||
@@ -1,59 +1,137 @@
|
||||
<template>
|
||||
<section class="esign-panel" aria-label="处方电子签署" v-loading="busy">
|
||||
<section v-show="showControls || signingTime || reviewState?.required" class="esign-panel" aria-label="处方电子签署" v-loading="busy">
|
||||
<div class="esign-panel__heading">
|
||||
<strong>医师电子签署</strong>
|
||||
<el-tag v-if="state && confirmed" :type="state.status === 'signed' ? 'success' : 'info'" size="small">
|
||||
{{ state.status_label }}
|
||||
</el-tag>
|
||||
<el-tag v-if="state?.environment === 'sandbox' && state?.status !== 'legacy'" type="warning" size="small">
|
||||
<el-tag v-if="confirmed && state?.environment === 'sandbox' && state.status !== 'legacy'" type="warning" size="small">
|
||||
沙箱测试
|
||||
</el-tag>
|
||||
</div>
|
||||
<p v-if="state?.message" class="esign-panel__message">{{ state.message }}</p>
|
||||
<p v-if="state?.environment === 'sandbox' && state?.status !== 'legacy'" class="esign-panel__message">
|
||||
当前为沙箱测试签署,仅用于接入验证。
|
||||
</p>
|
||||
<p v-if="error" class="esign-panel__error" role="alert">{{ error }}</p>
|
||||
<div class="esign-panel__actions">
|
||||
<el-button
|
||||
v-if="state?.can_sign"
|
||||
type="primary"
|
||||
size="small"
|
||||
:disabled="busy || !confirmed"
|
||||
@click="start"
|
||||
>{{ state.status === 'failed' ? '重试核身签署' : authUrl ? '重新获取核身链接' : '发起核身签署' }}</el-button>
|
||||
<el-link v-if="authUrl && state?.can_sign && confirmed" :href="authUrl" target="_blank" rel="noopener noreferrer" type="primary">
|
||||
打开核身页面
|
||||
</el-link>
|
||||
<el-button
|
||||
v-if="state?.can_sign && (state.status === 'authenticating' || authUrl)"
|
||||
size="small"
|
||||
:disabled="busy || !confirmed"
|
||||
@click="complete"
|
||||
>已完成核身,继续签署</el-button>
|
||||
<el-button size="small" :disabled="busy" @click="refresh">刷新签署状态</el-button>
|
||||
<el-button v-if="state?.can_download" size="small" :disabled="busy || !confirmed" @click="download">
|
||||
下载已签原始 PDF
|
||||
</el-button>
|
||||
<el-steps v-if="(confirmed || busy) && state?.required && state.status !== 'voided' && (!audit || state.can_sign || state.can_retry_verify)" :active="activeStep" finish-status="success" simple>
|
||||
<el-step title="处方草稿已保存" />
|
||||
<el-step :title="verificationMethod === 'quick' ? '核对快捷签授权' : verificationMethod === 'sms' ? '校验短信验证码' : '验证医生身份'" />
|
||||
<el-step title="签名并完成" />
|
||||
</el-steps>
|
||||
<p v-if="message" class="esign-panel__message" aria-live="polite">{{ message }}</p>
|
||||
<div v-if="signingTime" class="esign-panel__message" aria-live="polite">
|
||||
<div v-if="signingTime.issuedTime">开具时间:{{ signingTime.issuedTime }}(北京时间)</div>
|
||||
<div v-if="signingTime.signatureTime">
|
||||
医生签署时间:{{ signingTime.signatureTime }}(北京时间 · {{ signingTime.source }})
|
||||
</div>
|
||||
<template v-else>
|
||||
<div>签署时间待核实</div>
|
||||
<div v-if="signingTime.verifiedTime">系统验签完成时间:{{ signingTime.verifiedTime }}(北京时间)</div>
|
||||
</template>
|
||||
<div v-if="!audit && state?.review_signature_time">
|
||||
审核签署时间:{{ formatSigningTime(state.review_signature_time) }}(北京时间 · 可信时间戳)
|
||||
<span v-if="state.review_signer_name"> · {{ state.review_signer_name }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<p v-if="authUrl && state?.can_sign" class="esign-panel__message">
|
||||
请在新页面完成核身,然后返回继续签署;关闭页面后可从处方详情继续。
|
||||
</p>
|
||||
<p v-if="error" class="esign-panel__error" role="alert">{{ error }}</p>
|
||||
<label v-if="identityDetailsRequired" class="esign-panel__identity">
|
||||
本次验证使用的身份证号
|
||||
<el-input
|
||||
v-model="identityNumber"
|
||||
type="password"
|
||||
maxlength="18"
|
||||
autocomplete="off"
|
||||
:spellcheck="false"
|
||||
placeholder="输入18位大陆居民身份证号"
|
||||
/>
|
||||
</label>
|
||||
<label v-if="smsCodeEntry" class="esign-panel__identity">
|
||||
短信验证码
|
||||
<el-input
|
||||
v-model="smsCode"
|
||||
type="password"
|
||||
inputmode="numeric"
|
||||
maxlength="6"
|
||||
autocomplete="off"
|
||||
:spellcheck="false"
|
||||
:disabled="busy"
|
||||
placeholder="输入6位短信验证码"
|
||||
/>
|
||||
</label>
|
||||
<div v-if="showControls && prescriptionId > 0" class="esign-panel__actions">
|
||||
<el-button type="primary" size="small" :disabled="busy || dialogVisible || (action.run === sendSms && smsSendCoolingDown)" @click="action.run">
|
||||
{{ action.label }}
|
||||
</el-button>
|
||||
<el-button v-if="smsCodeEntry" size="small" :disabled="busy || smsSendCoolingDown" @click="sendSms">
|
||||
{{ smsSendCoolingDown ? '请稍后重新发送' : '重新发送验证码' }}
|
||||
</el-button>
|
||||
<el-button v-if="readiness" size="small" :disabled="busy" @click="closeAuthentication">取消</el-button>
|
||||
</div>
|
||||
<EsignReviewPanel
|
||||
v-if="audit && confirmed && state"
|
||||
:prescription-id="prescriptionId"
|
||||
:order-id="reviewOrderId"
|
||||
:doctor-signed="state.status === 'signed'"
|
||||
@change="updateReview"
|
||||
/>
|
||||
<el-dialog
|
||||
:model-value="dialogVisible"
|
||||
title="验证医生身份"
|
||||
width="min(720px, 94vw)"
|
||||
top="5vh"
|
||||
append-to-body
|
||||
destroy-on-close
|
||||
:before-close="closeAuthentication"
|
||||
:close-on-click-modal="false"
|
||||
>
|
||||
<p class="esign-panel__message">请在下方完成本人身份验证。</p>
|
||||
<iframe
|
||||
v-if="authUrl && dialogVisible"
|
||||
:src="authUrl"
|
||||
class="esign-panel__frame"
|
||||
title="e签宝本人身份核验"
|
||||
sandbox="allow-forms allow-scripts allow-same-origin"
|
||||
referrerpolicy="no-referrer"
|
||||
@error="authenticationUnavailable"
|
||||
/>
|
||||
<template #footer>
|
||||
<el-button link @click="authenticationUnavailable">验证页面不可用</el-button>
|
||||
<el-button @click="closeAuthentication">关闭</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { computed, onBeforeUnmount, onDeactivated, onMounted, ref, toRaw, watch } from 'vue'
|
||||
import {
|
||||
prescriptionEsignComplete,
|
||||
prescriptionEsignDoctorStatus,
|
||||
prescriptionEsignIdentityStatus,
|
||||
prescriptionEsignStart,
|
||||
prescriptionEsignStatus,
|
||||
prescriptionEsignVerifyWillingness,
|
||||
type PrescriptionEsignIdentityStatus,
|
||||
type PrescriptionEsignReviewStatus,
|
||||
type PrescriptionEsignStatus
|
||||
} from '@/api/tcm'
|
||||
import { downloadPrescriptionPdfIfRequired, safeEsignAuthUrl, validateEsignStatus } from '@/utils/prescription-esign'
|
||||
import {
|
||||
canApprovePrescriptionEsign,
|
||||
canCheckEsignIdentity,
|
||||
downloadOriginalEsignPdf,
|
||||
formatSigningTime,
|
||||
prescriptionEsignTiming,
|
||||
safeEsignAuthUrl,
|
||||
validateEsignIdentityStatus,
|
||||
validateEsignStatus
|
||||
} from '@/utils/prescription-esign'
|
||||
import EsignReviewPanel from '@/components/EsignReviewPanel.vue'
|
||||
|
||||
const props = defineProps<{ prescriptionId: number; initialState?: PrescriptionEsignStatus; audit?: boolean; reviewOrderId?: number }>()
|
||||
const emit = defineEmits<{
|
||||
(e: 'status-change', state: PrescriptionEsignStatus): void
|
||||
(e: 'review-change', state: PrescriptionEsignReviewStatus | null): void
|
||||
}>()
|
||||
|
||||
const reviewState = ref<PrescriptionEsignReviewStatus | null>(null)
|
||||
function updateReview(result: PrescriptionEsignReviewStatus | null) {
|
||||
reviewState.value = result
|
||||
emit('review-change', result)
|
||||
}
|
||||
|
||||
const props = defineProps<{ prescriptionId: number; initialState?: PrescriptionEsignStatus }>()
|
||||
const emit = defineEmits<{ (e: 'status-change', state: PrescriptionEsignStatus): void }>()
|
||||
function initialStatus(): PrescriptionEsignStatus | null {
|
||||
try {
|
||||
return validateEsignStatus(props.initialState)
|
||||
@@ -61,80 +139,556 @@ function initialStatus(): PrescriptionEsignStatus | null {
|
||||
return null
|
||||
}
|
||||
}
|
||||
const state = ref<PrescriptionEsignStatus | null>(initialStatus())
|
||||
const busy = ref(false)
|
||||
const confirmed = ref(false)
|
||||
const error = ref('')
|
||||
const authUrl = computed(() => safeEsignAuthUrl(state.value?.auth_url))
|
||||
let generation = 0
|
||||
|
||||
async function run(action: 'refresh' | 'start' | 'complete') {
|
||||
if (busy.value || !props.prescriptionId) return
|
||||
if (action !== 'refresh' && (!confirmed.value || state.value?.can_sign !== true)) return
|
||||
const id = props.prescriptionId
|
||||
const current = generation
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const api = action === 'start' ? prescriptionEsignStart : action === 'complete' ? prescriptionEsignComplete : prescriptionEsignStatus
|
||||
const result = validateEsignStatus(await api({ id }))
|
||||
if (id !== props.prescriptionId || current !== generation) return
|
||||
state.value = result
|
||||
confirmed.value = true
|
||||
emit('status-change', result)
|
||||
// A browser visit is never evidence of authentication or signing.
|
||||
if (result.auth_url && !safeEsignAuthUrl(result.auth_url)) {
|
||||
error.value = '核身链接无效,请重新获取或联系管理员'
|
||||
const state = ref<PrescriptionEsignStatus | null>(initialStatus())
|
||||
const work = ref<'status' | 'start' | 'identity' | 'send-sms' | 'verify-sms' | 'sign' | 'verify' | 'download' | null>(null)
|
||||
const busy = computed(() => work.value !== null)
|
||||
const confirmed = ref(false)
|
||||
const showControls = computed(() => !props.audit || !confirmed.value || !canApprovePrescriptionEsign(state.value))
|
||||
const signingTime = computed(() => prescriptionEsignTiming(state.value))
|
||||
const error = ref('')
|
||||
const dialogVisible = ref(false)
|
||||
const readiness = ref<'ready' | 'details' | null>(null)
|
||||
const restartRequired = ref(false)
|
||||
const identityNumber = ref('')
|
||||
const smsCode = ref('')
|
||||
const smsCodeState = ref<'entry' | 'expired' | null>(null)
|
||||
const smsSendCoolingDown = ref(false)
|
||||
const smsProfileReady = ref(false)
|
||||
const identityDetailsRequired = computed(() => readiness.value === 'details')
|
||||
const authUrl = computed(() => safeEsignAuthUrl(state.value?.auth_url))
|
||||
type VerificationMethod = NonNullable<PrescriptionEsignStatus['verification_method']>
|
||||
const verificationMethod = computed(() => state.value?.verification_method)
|
||||
const accountSetupMessage = computed(() => {
|
||||
const current = state.value
|
||||
if (!current?.can_sign || !verificationMethod.value) return ''
|
||||
const settings = '请返回页面右上角账号菜单,在「医生电子签署身份」中处理。完成后回到处方查询签署状态。'
|
||||
if (current.doctor_identity_required && !(verificationMethod.value === 'sms' && smsProfileReady.value)) {
|
||||
return `${current.message || '请先完成医生签署身份认证。'} ${settings}`
|
||||
}
|
||||
if (!current.signing_authorization_required) return ''
|
||||
const reason = current.signing_authorization_reason
|
||||
const authorization = reason === 'exhausted' ? '快捷签授权次数已用完。' : reason === 'expired' ? '快捷签授权已到期。'
|
||||
: reason === 'invalid' ? '快捷签授权已失效。' : '快捷签授权待完成。'
|
||||
return `${authorization}${settings}`
|
||||
})
|
||||
const smsCodeEntry = computed(() => confirmed.value && smsCodeState.value === 'entry'
|
||||
&& canUseMethod(state.value, 'sms') && state.value?.sms_sent === true && !accountSetupMessage.value && !readiness.value)
|
||||
const identityResumable = computed(() => !verificationMethod.value && canCheckEsignIdentity(state.value)
|
||||
&& (state.value?.can_resume_identity === true || Boolean(authUrl.value)))
|
||||
const activeStep = computed(() => {
|
||||
if (state.value?.status === 'signed') return 3
|
||||
if (readiness.value || work.value === 'sign' || work.value === 'verify'
|
||||
|| state.value?.status === 'signing' || state.value?.status === 'uncertain') return 2
|
||||
return 1
|
||||
})
|
||||
const message = computed(() => {
|
||||
if (props.prescriptionId <= 0) return '缺少关联处方,无法核对签署状态。'
|
||||
if (props.audit && work.value === 'status') return '正在查询签署状态…'
|
||||
if (work.value === 'identity' && verificationMethod.value === 'quick') return '正在核对快捷签授权…'
|
||||
if (work.value === 'identity' && verificationMethod.value === 'sms') return '正在查询短信验证状态…'
|
||||
if (work.value === 'send-sms') return '正在向医生绑定的手机号发送验证码…'
|
||||
if (work.value === 'verify-sms') return '正在校验短信验证码…'
|
||||
if (work.value === 'sign') return '正在签署处方…'
|
||||
if (work.value === 'verify') return '正在校验已签文件…'
|
||||
if (!confirmed.value || error.value) return ''
|
||||
if (readiness.value) {
|
||||
if (verificationMethod.value === 'quick') return '快捷签授权已核对。请核对处方内容后确认签名。'
|
||||
if (verificationMethod.value === 'sms') return '短信验证码已通过。请核对处方内容后确认签名。'
|
||||
return '医生身份已验证。'
|
||||
}
|
||||
const current = state.value
|
||||
if (!current) return ''
|
||||
if (current.status === 'legacy' && !current.enabled) return current.message || '此处方使用手写签名。'
|
||||
if (current.status === 'signed') return current.can_download ? '处方已签署。' : '处方已签署,原件暂不可用。'
|
||||
if (current.required && ['pending', 'authenticating', 'failed'].includes(current.status)) {
|
||||
if (accountSetupMessage.value) return accountSetupMessage.value
|
||||
if (canUseMethod(current, 'quick')) {
|
||||
return current.message || '请查询签署状态,核对快捷签授权。'
|
||||
}
|
||||
} catch (e: any) {
|
||||
if (id === props.prescriptionId && current === generation) {
|
||||
confirmed.value = false
|
||||
error.value = e?.msg || e?.message || '签署服务暂时不可用,请刷新状态后重试'
|
||||
if (canUseMethod(current, 'sms')) {
|
||||
if (smsCodeEntry.value) return '请输入医生绑定的手机号收到的验证码。'
|
||||
if (smsCodeState.value === 'expired') return '本次短信验证码已失效,请重新发送。'
|
||||
if (smsProfileReady.value) return '医生签署身份已就绪,请发送本张处方的验证码。'
|
||||
if (current.message) return current.message
|
||||
return current.sms_sent ? '请查询短信验证状态。' : '请将本张处方的验证码发送到医生绑定的手机号。'
|
||||
}
|
||||
} finally {
|
||||
if (current === generation) busy.value = false
|
||||
if (current.message) return current.message
|
||||
if (!current.enabled) return '电子签署暂不可用,请联系管理员。'
|
||||
if (props.audit && !current.can_sign) {
|
||||
return '请等待开方医生完成电子签署后,再审核通过。'
|
||||
}
|
||||
if (props.audit) return '请完成核身及电子签署后,再审核通过。'
|
||||
}
|
||||
return current.message || (current.can_sign ? '' : current.status_label)
|
||||
})
|
||||
const action = computed(() => {
|
||||
if (!confirmed.value) return { label: '查询签署状态', run: refresh }
|
||||
if (state.value?.can_download && (state.value.status === 'signed' || state.value.status === 'voided')) {
|
||||
return { label: '下载已签 PDF', run: download }
|
||||
}
|
||||
if (state.value?.status === 'uncertain' && state.value.can_retry_verify) {
|
||||
return { label: '重试校验已签文件', run: retryVerification }
|
||||
}
|
||||
if (state.value?.can_sign && ['pending', 'authenticating', 'failed'].includes(state.value.status)) {
|
||||
if (accountSetupMessage.value) return { label: '查询签署状态', run: refresh }
|
||||
if (readiness.value) return { label: '确认签名', run: confirmSign }
|
||||
if (verificationMethod.value === 'quick') return { label: '查询签署状态', run: refresh }
|
||||
if (verificationMethod.value === 'sms') {
|
||||
if (smsCodeEntry.value) return { label: '校验验证码', run: verifySms }
|
||||
if (state.value.sms_sent && smsCodeState.value !== 'expired') return { label: '查询签署状态', run: refresh }
|
||||
const label = smsSendCoolingDown.value ? '请稍后重新发送' : state.value.sms_sent ? '重新发送验证码' : '发送验证码'
|
||||
return { label, run: sendSms }
|
||||
}
|
||||
if (restartRequired.value) return { label: '重新验证医生身份', run: () => start(true) }
|
||||
if (identityResumable.value) return { label: '检查身份验证', run: recheckIdentity }
|
||||
return { label: state.value.status === 'failed' ? '重新验证医生身份' : '验证医生身份', run: () => start() }
|
||||
}
|
||||
return { label: '查询签署状态', run: refresh }
|
||||
})
|
||||
|
||||
const pollInterval = 5000
|
||||
const authenticationTimeout = 10 * 60 * 1000
|
||||
const smsSendCooldown = 60 * 1000
|
||||
let operation = 0
|
||||
let disposed = false
|
||||
let pollTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let deadlineTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let identityRequest: AbortController | undefined
|
||||
let resendTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let expiresAt = 0
|
||||
|
||||
function canAct() {
|
||||
return !disposed && !busy.value && props.prescriptionId > 0 && document.visibilityState === 'visible'
|
||||
}
|
||||
|
||||
function isCurrent(current: number, id: number) {
|
||||
return !disposed && current === operation && id === props.prescriptionId && document.visibilityState === 'visible'
|
||||
}
|
||||
|
||||
function canUseMethod(result: PrescriptionEsignStatus | null, method: VerificationMethod): boolean {
|
||||
return result?.verification_method === method && result.enabled && result.required && result.can_sign
|
||||
&& ['pending', 'authenticating', 'failed'].includes(result.status)
|
||||
}
|
||||
|
||||
function accept(result: PrescriptionEsignStatus) {
|
||||
state.value = result
|
||||
confirmed.value = true
|
||||
if (!result.can_sign || !['authenticating', 'failed'].includes(result.status)) restartRequired.value = false
|
||||
emit('status-change', result)
|
||||
}
|
||||
|
||||
function clearVerification() {
|
||||
clearTimeout(pollTimer)
|
||||
clearTimeout(deadlineTimer)
|
||||
pollTimer = undefined
|
||||
deadlineTimer = undefined
|
||||
identityRequest?.abort()
|
||||
identityRequest = undefined
|
||||
dialogVisible.value = false
|
||||
readiness.value = null
|
||||
identityNumber.value = ''
|
||||
smsCode.value = ''
|
||||
smsCodeState.value = null
|
||||
}
|
||||
|
||||
function cancelWork() {
|
||||
operation++
|
||||
work.value = null
|
||||
clearVerification()
|
||||
smsProfileReady.value = false
|
||||
}
|
||||
|
||||
function closeAuthentication() {
|
||||
if (dialogVisible.value || readiness.value) cancelWork()
|
||||
}
|
||||
|
||||
function stopVerification(message: string) {
|
||||
clearVerification()
|
||||
error.value = message
|
||||
}
|
||||
|
||||
function authenticationUnavailable() {
|
||||
if (!dialogVisible.value) return
|
||||
cancelWork()
|
||||
error.value = '验证页面无法加载,请重试或联系管理员。'
|
||||
}
|
||||
|
||||
function beginVerification(current: number, id: number) {
|
||||
expiresAt = Date.now() + authenticationTimeout
|
||||
deadlineTimer = setTimeout(() => {
|
||||
if (!isCurrent(current, id)) return
|
||||
cancelWork()
|
||||
error.value = '等待验证超时,请检查身份验证。'
|
||||
}, authenticationTimeout)
|
||||
}
|
||||
|
||||
function schedulePoll(current: number, id: number) {
|
||||
if (dialogVisible.value && isCurrent(current, id)) {
|
||||
pollTimer = setTimeout(() => void checkIdentity(current, id), pollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
const refresh = () => run('refresh')
|
||||
const start = () => run('start')
|
||||
const complete = () => run('complete')
|
||||
async function requestIdentity<T>(request: (signal: AbortSignal) => Promise<T>): Promise<T> {
|
||||
const controller = new AbortController()
|
||||
identityRequest = controller
|
||||
try {
|
||||
return await request(controller.signal)
|
||||
} finally {
|
||||
if (identityRequest === controller) identityRequest = undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function queryIdentity(id: number) {
|
||||
return validateEsignIdentityStatus(await requestIdentity((signal) => prescriptionEsignIdentityStatus({ id }, signal)))
|
||||
}
|
||||
|
||||
async function checkIdentity(current: number, id: number) {
|
||||
if (!isCurrent(current, id) || identityRequest || (!dialogVisible.value && work.value !== 'identity')) return
|
||||
try {
|
||||
const result = await queryIdentity(id)
|
||||
if (!isCurrent(current, id)) return
|
||||
if (Date.now() >= expiresAt) {
|
||||
stopVerification('等待验证超时,请检查身份验证。')
|
||||
return
|
||||
}
|
||||
accept(result)
|
||||
// Only this read-only server response can unlock the explicit confirmation button.
|
||||
if (result.verification_method) {
|
||||
stopVerification('签署方式已变化,请查询签署状态。')
|
||||
} else if (result.identity_status === 'FAIL' || result.identity_status === 'EXPIRED') {
|
||||
restartRequired.value = result.can_sign && ['authenticating', 'failed'].includes(result.status)
|
||||
stopVerification(result.identity_status === 'EXPIRED' ? '本次身份验证已过期。' : '本次身份验证未通过。')
|
||||
} else if (result.status === 'signed') {
|
||||
clearVerification()
|
||||
} else if (!canCheckEsignIdentity(result) || result.identity_status === 'UNAVAILABLE') {
|
||||
stopVerification(result.message || '暂时无法核对身份验证,请重试。')
|
||||
} else if (result.identity_details_required || result.can_complete) {
|
||||
dialogVisible.value = false
|
||||
readiness.value = result.identity_details_required ? 'details' : 'ready'
|
||||
} else if (authUrl.value) {
|
||||
dialogVisible.value = true
|
||||
} else {
|
||||
stopVerification('验证链接不可用,请重试或联系管理员。')
|
||||
}
|
||||
} catch {
|
||||
if (isCurrent(current, id)) {
|
||||
confirmed.value = false
|
||||
stopVerification('身份验证查询失败,请查询签署状态后重试。')
|
||||
}
|
||||
}
|
||||
schedulePoll(current, id)
|
||||
}
|
||||
|
||||
async function recheckIdentity() {
|
||||
if (!canAct() || !confirmed.value || !identityResumable.value || dialogVisible.value || readiness.value || restartRequired.value) return
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
work.value = 'identity'
|
||||
error.value = ''
|
||||
beginVerification(current, id)
|
||||
try {
|
||||
await checkIdentity(current, id)
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function start(restart = false) {
|
||||
if (!canAct() || !confirmed.value || !state.value?.can_sign || verificationMethod.value || dialogVisible.value || readiness.value
|
||||
|| !['pending', 'authenticating', 'failed'].includes(state.value.status)
|
||||
|| restart !== restartRequired.value || (!restart && identityResumable.value)) return
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
work.value = 'start'
|
||||
confirmed.value = false
|
||||
error.value = ''
|
||||
try {
|
||||
const result = validateEsignStatus(await prescriptionEsignStart(restart ? { id, restart_identity: true } : { id }))
|
||||
if (!isCurrent(current, id)) return
|
||||
accept(result)
|
||||
restartRequired.value = false
|
||||
if (result.status === 'authenticating' && result.can_sign) {
|
||||
if (!authUrl.value) {
|
||||
error.value = '验证链接无效,请重试或联系管理员。'
|
||||
return
|
||||
}
|
||||
dialogVisible.value = true
|
||||
beginVerification(current, id)
|
||||
schedulePoll(current, id)
|
||||
}
|
||||
} catch {
|
||||
if (isCurrent(current, id)) error.value = '验证请求结果待确认,请查询签署状态。'
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
function applyMethodIdentity(result: PrescriptionEsignIdentityStatus, method: VerificationMethod, current: number, id: number) {
|
||||
if (result.can_sign && !canUseMethod(result, method)) throw new Error('签署方式已变化')
|
||||
accept(result)
|
||||
if (!canUseMethod(result, method) || accountSetupMessage.value) return
|
||||
if (result.can_complete) {
|
||||
beginVerification(current, id)
|
||||
readiness.value = 'ready'
|
||||
} else if (method === 'sms') {
|
||||
if (result.sms_sent && result.identity_status === 'ING') smsCodeState.value = 'entry'
|
||||
if (result.identity_status === 'EXPIRED' || result.identity_status === 'FAIL') smsCodeState.value = 'expired'
|
||||
}
|
||||
}
|
||||
|
||||
async function readMethodIdentity(current: number, id: number, method: VerificationMethod) {
|
||||
const result = await queryIdentity(id)
|
||||
if (!isCurrent(current, id)) return
|
||||
applyMethodIdentity(result, method, current, id)
|
||||
return result
|
||||
}
|
||||
|
||||
async function prepareQuick(result: PrescriptionEsignStatus, current: number, id: number) {
|
||||
work.value = 'identity'
|
||||
const retained = result.can_resume_identity === true
|
||||
if (retained) {
|
||||
const identity = await readMethodIdentity(current, id, 'quick')
|
||||
if (!identity || identity.identity_status !== 'EXPIRED' || !canUseMethod(identity, 'quick') || accountSetupMessage.value) return
|
||||
}
|
||||
if (!isCurrent(current, id)) return
|
||||
const prepared = validateEsignStatus(await prescriptionEsignStart(retained ? { id, restart_identity: true } : { id }))
|
||||
if (!isCurrent(current, id)) return
|
||||
accept(prepared)
|
||||
if (canUseMethod(prepared, 'quick') && prepared.can_resume_identity && !accountSetupMessage.value) {
|
||||
await readMethodIdentity(current, id, 'quick')
|
||||
}
|
||||
}
|
||||
|
||||
function startSmsCooldown() {
|
||||
clearTimeout(resendTimer)
|
||||
smsSendCoolingDown.value = true
|
||||
resendTimer = setTimeout(() => { smsSendCoolingDown.value = false }, smsSendCooldown)
|
||||
}
|
||||
|
||||
async function sendSms() {
|
||||
if (!canAct() || !confirmed.value || !canUseMethod(state.value, 'sms') || accountSetupMessage.value || readiness.value
|
||||
|| smsSendCoolingDown.value || (state.value?.sms_sent && !smsCodeEntry.value && smsCodeState.value !== 'expired')) return
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
const restart = state.value?.sms_sent === true || state.value?.can_resume_identity === true
|
||||
clearVerification()
|
||||
smsProfileReady.value = false
|
||||
confirmed.value = false
|
||||
work.value = 'send-sms'
|
||||
error.value = ''
|
||||
startSmsCooldown()
|
||||
try {
|
||||
const result = validateEsignStatus(await prescriptionEsignStart(restart ? { id, restart_identity: true } : { id }))
|
||||
if (!isCurrent(current, id)) return
|
||||
accept(result)
|
||||
if (canUseMethod(result, 'sms') && result.sms_sent && result.can_resume_identity && !accountSetupMessage.value) {
|
||||
startSmsCooldown()
|
||||
work.value = 'identity'
|
||||
await readMethodIdentity(current, id, 'sms')
|
||||
}
|
||||
} catch {
|
||||
if (isCurrent(current, id)) {
|
||||
confirmed.value = false
|
||||
error.value = '短信发送状态待确认,请查询签署状态。'
|
||||
}
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function verifySms() {
|
||||
const code = smsCode.value.trim()
|
||||
smsCode.value = ''
|
||||
if (!canAct() || !smsCodeEntry.value) return
|
||||
if (!/^\d{6}$/.test(code)) {
|
||||
error.value = '请输入6位短信验证码。'
|
||||
return
|
||||
}
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
clearVerification()
|
||||
confirmed.value = false
|
||||
work.value = 'verify-sms'
|
||||
error.value = ''
|
||||
try {
|
||||
const result = validateEsignIdentityStatus(await prescriptionEsignVerifyWillingness({ id, auth_code: code }))
|
||||
if (!isCurrent(current, id)) return
|
||||
applyMethodIdentity(result, 'sms', current, id)
|
||||
if (smsCodeEntry.value) error.value = '验证码未通过,请检查后重试。'
|
||||
} catch {
|
||||
if (isCurrent(current, id)) error.value = '短信校验结果待确认,请查询签署状态。'
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmSign() {
|
||||
const number = identityNumber.value.trim().toUpperCase()
|
||||
identityNumber.value = ''
|
||||
if (!canAct() || !confirmed.value || !readiness.value || !canCheckEsignIdentity(state.value)) return
|
||||
if (verificationMethod.value && (!canUseMethod(state.value, verificationMethod.value) || accountSetupMessage.value)) return
|
||||
if (Date.now() >= expiresAt) {
|
||||
stopVerification('等待验证超时,请检查身份验证。')
|
||||
return
|
||||
}
|
||||
if (identityDetailsRequired.value && !/^[1-9]\d{16}[\dX]$/.test(number)) {
|
||||
error.value = '请输入本次验证使用的18位大陆居民身份证号。'
|
||||
return
|
||||
}
|
||||
const id = props.prescriptionId
|
||||
await submitCompletion('sign', identityDetailsRequired.value ? { id, identity_number: number } : { id })
|
||||
}
|
||||
|
||||
async function retryVerification() {
|
||||
if (!canAct() || !confirmed.value || state.value?.status !== 'uncertain' || state.value.can_retry_verify !== true) return
|
||||
await submitCompletion('verify', { id: props.prescriptionId, verify_only: true })
|
||||
}
|
||||
|
||||
async function submitCompletion(kind: 'sign' | 'verify', params: Parameters<typeof prescriptionEsignComplete>[0]) {
|
||||
const current = ++operation
|
||||
const { id } = params
|
||||
// Consume readiness before dispatch. Neither an error nor a late reply replays the POST.
|
||||
clearVerification()
|
||||
confirmed.value = false
|
||||
work.value = kind
|
||||
error.value = ''
|
||||
try {
|
||||
const result = validateEsignStatus(await prescriptionEsignComplete(params))
|
||||
if (isCurrent(current, id)) accept(result)
|
||||
} catch {
|
||||
if (isCurrent(current, id)) {
|
||||
error.value = kind === 'sign' ? '签署结果待确认,请查询签署状态。' : '校验结果待确认,请查询签署状态。'
|
||||
}
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
if (!canAct()) return
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
clearVerification()
|
||||
smsProfileReady.value = false
|
||||
work.value = 'status'
|
||||
confirmed.value = false
|
||||
error.value = ''
|
||||
try {
|
||||
const result = validateEsignStatus(await prescriptionEsignStatus({ id }))
|
||||
if (!isCurrent(current, id)) return
|
||||
accept(result)
|
||||
// A previous SMS failure can retain the setup flag after the doctor fixes the profile.
|
||||
if (canUseMethod(result, 'sms') && result.doctor_identity_required) {
|
||||
work.value = 'identity'
|
||||
const profile = await requestIdentity(prescriptionEsignDoctorStatus)
|
||||
if (!isCurrent(current, id)) return
|
||||
smsProfileReady.value = profile?.enabled === true && profile.status === 'ready'
|
||||
}
|
||||
if (accountSetupMessage.value) return
|
||||
if (canUseMethod(result, 'quick')) {
|
||||
await prepareQuick(result, current, id)
|
||||
} else if (canUseMethod(result, 'sms') && result.sms_sent && result.can_resume_identity) {
|
||||
work.value = 'identity'
|
||||
await readMethodIdentity(current, id, 'sms')
|
||||
}
|
||||
} catch {
|
||||
if (isCurrent(current, id)) {
|
||||
confirmed.value = false
|
||||
error.value = work.value === 'identity' ? '暂时无法核对签署条件,请查询签署状态后重试。' : '签署状态查询失败,请重试。'
|
||||
}
|
||||
} finally {
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function download() {
|
||||
if (busy.value || !confirmed.value || state.value?.can_download !== true) return
|
||||
busy.value = true
|
||||
if (!canAct() || !confirmed.value || !state.value?.can_download
|
||||
|| (state.value.status !== 'signed' && state.value.status !== 'voided')) return
|
||||
const id = props.prescriptionId
|
||||
const current = ++operation
|
||||
work.value = 'download'
|
||||
error.value = ''
|
||||
try {
|
||||
await downloadPrescriptionPdfIfRequired(props.prescriptionId)
|
||||
} catch (e: any) {
|
||||
confirmed.value = false
|
||||
error.value = e?.msg || e?.message || '下载签署文件失败,请重试'
|
||||
const result = validateEsignStatus(await prescriptionEsignStatus({ id }))
|
||||
if (!isCurrent(current, id)) return
|
||||
accept(result)
|
||||
if (result.can_download && (result.status === 'signed' || result.status === 'voided')) {
|
||||
await downloadOriginalEsignPdf(id, () => isCurrent(current, id))
|
||||
} else {
|
||||
error.value = '已签 PDF 暂不可下载。'
|
||||
}
|
||||
} catch {
|
||||
if (isCurrent(current, id)) error.value = '下载已签 PDF 失败,请重试。'
|
||||
} finally {
|
||||
busy.value = false
|
||||
if (isCurrent(current, id)) work.value = null
|
||||
}
|
||||
}
|
||||
|
||||
watch(() => props.prescriptionId, () => {
|
||||
generation++
|
||||
busy.value = false
|
||||
function onVisibilityChange() {
|
||||
if (document.visibilityState === 'visible') return
|
||||
if (work.value === 'sign') error.value = '签署结果待确认,请查询签署状态。'
|
||||
if (work.value === 'verify') error.value = '校验结果待确认,请查询签署状态。'
|
||||
if (work.value === 'send-sms') error.value = '短信发送状态待确认,请查询签署状态。'
|
||||
if (work.value === 'verify-sms') error.value = '短信校验结果待确认,请查询签署状态。'
|
||||
cancelWork()
|
||||
}
|
||||
|
||||
function resetStatus() {
|
||||
cancelWork()
|
||||
restartRequired.value = false
|
||||
confirmed.value = false
|
||||
error.value = ''
|
||||
state.value = initialStatus()
|
||||
void refresh()
|
||||
}, { immediate: true })
|
||||
}
|
||||
|
||||
watch(() => props.prescriptionId, resetStatus, { immediate: true })
|
||||
watch(() => props.initialState, (value) => {
|
||||
if (value) {
|
||||
state.value = initialStatus()
|
||||
if (!state.value) confirmed.value = false
|
||||
}
|
||||
// An emitted state echoed by the parent preserves this interaction.
|
||||
if (toRaw(value) !== toRaw(state.value)) resetStatus()
|
||||
})
|
||||
|
||||
onMounted(() => document.addEventListener('visibilitychange', onVisibilityChange))
|
||||
onDeactivated(cancelWork)
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true
|
||||
cancelWork()
|
||||
clearTimeout(resendTimer)
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.esign-panel { margin: 12px 0; padding: 14px 16px; border: 1px solid var(--el-border-color-light); border-radius: 6px; background: var(--el-fill-color-light); }
|
||||
.esign-panel__heading, .esign-panel__actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
|
||||
.esign-panel__message { margin: 8px 0; color: var(--el-text-color-secondary); font-size: 13px; }
|
||||
.esign-panel__error { color: var(--el-color-danger); font-size: 13px; }
|
||||
.esign-panel {
|
||||
margin: 12px 0;
|
||||
padding: 14px 16px;
|
||||
border: 1px solid var(--el-border-color-light);
|
||||
border-radius: 6px;
|
||||
background: var(--el-bg-color);
|
||||
}
|
||||
.esign-panel__heading, .esign-panel__actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.esign-panel :deep(.el-steps) { margin: 10px 0; padding: 10px 12px; }
|
||||
.esign-panel :deep(.el-step.is-simple .el-step__title) { font-size: 13px; line-height: 1.4; word-break: normal; }
|
||||
.esign-panel__actions { margin-top: 10px; }
|
||||
.esign-panel__actions .el-button + .el-button { margin-left: 0; }
|
||||
.esign-panel__message, .esign-panel__error { margin: 8px 0; font-size: 13px; }
|
||||
.esign-panel__message { color: var(--el-text-color-secondary); }
|
||||
.esign-panel__error { color: var(--el-color-danger); }
|
||||
.esign-panel__identity { display: grid; gap: 6px; max-width: 320px; margin: 10px 0; font-size: 13px; }
|
||||
.esign-panel__frame {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: min(62vh, 620px);
|
||||
min-height: 300px;
|
||||
border: 1px solid var(--el-border-color-light);
|
||||
border-radius: 4px;
|
||||
background: white;
|
||||
}
|
||||
@media print { .esign-panel { display: none; } }
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
<template>
|
||||
<div v-if="state?.required || error" class="esign-review" v-loading="busy" aria-label="处方审核电子签署">
|
||||
<strong>审核电子签署</strong>
|
||||
<p v-if="state?.actor_name">当前审核人 {{ state.actor_name }}</p>
|
||||
<p v-if="state?.state === 'signed'">
|
||||
{{ state.signer_name }} · {{ formatSigningTime(state.signature_time) }}(北京时间 · 可信时间戳)
|
||||
</p>
|
||||
<p v-if="state?.message" aria-live="polite">{{ state.message }}</p>
|
||||
<p v-if="error" class="text-danger" role="alert">{{ error }}</p>
|
||||
<div class="esign-review__actions">
|
||||
<el-button v-if="state?.can_confirm" type="primary" size="small" :disabled="busy" @click="confirm(false)">
|
||||
确认签名
|
||||
</el-button>
|
||||
<el-button v-if="state?.can_retry_verify" type="primary" size="small" :disabled="busy" @click="confirm(true)">
|
||||
重试验签
|
||||
</el-button>
|
||||
<el-button v-if="state?.required && doctorSigned" size="small" :disabled="busy" @click="download">
|
||||
{{ state.state === 'signed' ? '下载双签 PDF' : '下载医生已签 PDF' }}
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="state?.can_setup && state.state !== 'signed'"
|
||||
size="small"
|
||||
:disabled="busy"
|
||||
@click="identityOpen = true"
|
||||
>
|
||||
审核签署身份
|
||||
</el-button>
|
||||
<el-button v-if="state?.required && !state.can_approve" size="small" :disabled="busy" @click="switchReviewer">
|
||||
切换审核账号
|
||||
</el-button>
|
||||
<el-button v-if="error || state?.state !== 'signed'" size="small" :disabled="busy" @click="refresh">
|
||||
刷新签署状态
|
||||
</el-button>
|
||||
</div>
|
||||
<EsignDoctorDialog
|
||||
v-if="identityOpen"
|
||||
reviewer
|
||||
:server-time-offset-ms="userStore.serverTimeOffsetMs"
|
||||
@close="closeIdentity"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
|
||||
import {
|
||||
prescriptionEsignReviewConfirm,
|
||||
prescriptionEsignReviewStatus,
|
||||
type PrescriptionEsignReviewStatus
|
||||
} from '@/api/tcm'
|
||||
import EsignDoctorDialog from '@/components/EsignDoctorDialog.vue'
|
||||
import useUserStore from '@/stores/modules/user'
|
||||
import feedback from '@/utils/feedback'
|
||||
import { downloadOriginalEsignPdf, formatSigningTime } from '@/utils/prescription-esign'
|
||||
|
||||
const props = defineProps<{ prescriptionId: number; orderId?: number; doctorSigned: boolean }>()
|
||||
const emit = defineEmits<{ (e: 'change', value: PrescriptionEsignReviewStatus | null): void }>()
|
||||
const state = ref<PrescriptionEsignReviewStatus | null>(null)
|
||||
const identityOpen = ref(false)
|
||||
const busy = ref(false)
|
||||
const error = ref('')
|
||||
const userStore = useUserStore()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
let generation = 0
|
||||
let statusRequest: AbortController | undefined
|
||||
|
||||
function errorMessage(cause: unknown, fallback: string): string {
|
||||
if (typeof cause === 'object' && cause !== null && 'msg' in cause && typeof cause.msg === 'string') {
|
||||
return cause.msg
|
||||
}
|
||||
return cause instanceof Error ? cause.message : fallback
|
||||
}
|
||||
|
||||
function accept(result: PrescriptionEsignReviewStatus) {
|
||||
if (!result || ['required', 'can_confirm', 'can_approve', 'can_retry_verify'].some((field) => typeof (result as any)[field] !== 'boolean')
|
||||
|| result.actor_id !== Number(userStore.userInfo.id)) {
|
||||
throw new Error('登录账号或审核状态已变化,请刷新页面')
|
||||
}
|
||||
state.value = result
|
||||
emit('change', result)
|
||||
}
|
||||
|
||||
function target() {
|
||||
return { id: props.prescriptionId, ...(props.orderId ? { order_id: props.orderId } : {}) }
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
const ticket = ++generation
|
||||
statusRequest?.abort()
|
||||
statusRequest = new AbortController()
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
emit('change', null)
|
||||
try {
|
||||
const result = await prescriptionEsignReviewStatus(target(), statusRequest.signal)
|
||||
if (ticket === generation) accept(result)
|
||||
} catch (cause) {
|
||||
if (ticket === generation) error.value = errorMessage(cause, '无法查询审核签署状态')
|
||||
} finally {
|
||||
if (ticket === generation) busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function confirm(verifyOnly: boolean) {
|
||||
const actorId = state.value?.actor_id
|
||||
if (busy.value || !actorId || !(verifyOnly ? state.value?.can_retry_verify : state.value?.can_confirm)) return
|
||||
const ticket = ++generation
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const result = await prescriptionEsignReviewConfirm({ ...target(), actor_id: actorId, verify_only: verifyOnly })
|
||||
if (ticket === generation) accept(result)
|
||||
} catch (cause) {
|
||||
if (ticket === generation) {
|
||||
await refresh()
|
||||
if (generation === ticket + 1) {
|
||||
error.value = errorMessage(cause, '请刷新状态或续期本人的快捷签授权后重试')
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (ticket === generation) busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function switchReviewer() {
|
||||
try {
|
||||
await feedback.confirm('当前账号将退出。请所选审核人本人登录。')
|
||||
if (busy.value) return
|
||||
busy.value = true
|
||||
const returnTo = router.resolve({
|
||||
path: route.path,
|
||||
query: props.orderId ? { review_order: props.orderId } : { review_prescription: props.prescriptionId }
|
||||
}).fullPath
|
||||
await userStore.logout(returnTo)
|
||||
} catch {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function download() {
|
||||
if (busy.value || !props.doctorSigned) return
|
||||
const ticket = generation
|
||||
const actor = userStore.userInfo.id
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await downloadOriginalEsignPdf(props.prescriptionId,
|
||||
() => generation === ticket && userStore.userInfo.id === actor)
|
||||
} catch (cause) {
|
||||
if (generation === ticket) error.value = errorMessage(cause, '下载签署原件失败,请重试')
|
||||
} finally {
|
||||
if (generation === ticket) busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function closeIdentity() {
|
||||
identityOpen.value = false
|
||||
void refresh()
|
||||
}
|
||||
|
||||
watch(() => [props.prescriptionId, props.orderId, props.doctorSigned, userStore.userInfo.id],
|
||||
() => { if (props.prescriptionId > 0) void refresh() }, { immediate: true })
|
||||
onBeforeUnmount(() => {
|
||||
generation++
|
||||
statusRequest?.abort()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.esign-review {
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
border-top: 1px solid var(--el-border-color-light);
|
||||
margin-top: 12px;
|
||||
padding-top: 12px;
|
||||
}
|
||||
|
||||
.esign-review p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.esign-review__actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.esign-review__actions :deep(.el-button + .el-button) {
|
||||
margin-left: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -514,7 +514,7 @@
|
||||
{{ esignError }} <el-button link type="primary" @click="loadEsignConfig">重试</el-button>
|
||||
</el-alert>
|
||||
<el-alert v-else-if="esignEnabled" type="info" :closable="false" class="mb-3"
|
||||
:title="esignConfig?.environment === 'sandbox' ? '沙箱测试:保存处方后,请完成医师核身与电子签署' : '保存处方后,请完成医师核身与电子签署'" />
|
||||
:title="esignConfig?.environment === 'sandbox' ? '沙箱测试。保存草稿后,由开方医生本人确认签署。' : '保存草稿后,由开方医生本人确认签署。'" />
|
||||
<el-form-item v-else-if="esignReady" label="医师签名" prop="doctor_signature" required>
|
||||
<div class="signature-pad-wrap">
|
||||
<canvas
|
||||
@@ -540,6 +540,7 @@
|
||||
|
||||
<div v-else class="rx-wrap">
|
||||
<EsignPrescriptionPanel
|
||||
v-if="visible"
|
||||
:key="savedPrescription.id"
|
||||
:prescription-id="Number(savedPrescription.id)"
|
||||
:initial-state="savedPrescription.esign"
|
||||
@@ -726,7 +727,7 @@
|
||||
<div class="flex justify-end gap-2">
|
||||
<el-button @click="handleClose">取消</el-button>
|
||||
<el-button type="primary" @click="handleSave" :loading="saving || esignLoading" :disabled="!esignReady">
|
||||
{{ esignEnabled ? '保存后核身签署' : '保存处方' }}
|
||||
{{ esignEnabled ? '保存处方草稿' : '保存处方' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
@@ -2133,11 +2134,16 @@ const handleSave = async () => {
|
||||
if (id) {
|
||||
// Keep the saved id even if the follow-up detail request fails; retrying must not add twice.
|
||||
savedPrescription.value = { ...formData, id, audit_status: 0, esign: res.esign }
|
||||
feedback.msgSuccess(res.esign?.required ? '处方已保存,请继续完成核身签署' : '处方已保存')
|
||||
feedback.msgSuccess(res.esign?.required ? '处方草稿已保存' : '处方已保存')
|
||||
try {
|
||||
savedPrescription.value = await prescriptionDetail({ id })
|
||||
const detail = await prescriptionDetail({ id })
|
||||
if (savedPrescription.value?.id === id) {
|
||||
savedPrescription.value = { ...detail, esign: savedPrescription.value.esign ?? detail.esign }
|
||||
}
|
||||
} catch {
|
||||
feedback.msgWarning('处方已保存,详情暂未加载,可从已开处方继续签署')
|
||||
if (savedPrescription.value?.id === id) {
|
||||
feedback.msgWarning(res.esign?.required ? '处方详情暂未加载,可继续操作' : '处方已保存,详情暂未加载,可从已开处方继续签署')
|
||||
}
|
||||
}
|
||||
emit('success')
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user