This commit is contained in:
Your Name
2026-11-26 15:46:20 +08:00
parent db6d327e91
commit be79e09c83
170 changed files with 21159 additions and 13217 deletions
@@ -27,8 +27,9 @@ final class DoctorEsignIdentityService
public static function quickEnabled(): bool
{
return PrescriptionEsignService::enabled()
&& filter_var(Config::get('esign.doctor_quicksign_enabled', false), FILTER_VALIDATE_BOOLEAN);
$managed = PrescriptionCaSigningSettings::override();
return $managed ?? (PrescriptionEsignService::enabled()
&& filter_var(Config::get('esign.doctor_quicksign_enabled', false), FILTER_VALIDATE_BOOLEAN));
}
public static function status(int $actor): array
@@ -61,7 +62,13 @@ final class DoctorEsignIdentityService
return $pending;
}
}
$state = self::readiness($client, $snapshot, true, $quick === null)['state'];
$state = self::readiness(
$client,
$snapshot,
allowCertificateRecovery: true,
requireMobile: $quick === null,
reportUnconfirmedCertificate: true
)['state'];
if (isset($status) && $state['status'] !== 'ready') {
$state['message'] = $status === 'FAIL' ? '认证未通过,请重新发起认证' : '认证已过期,请重新发起认证';
}
@@ -75,14 +82,14 @@ final class DoctorEsignIdentityService
self::assertUnchanged($scope, $quick);
}
self::assertUnchanged($scope, $snapshot);
return $state;
return $state + self::reviewerNameEntry($snapshot);
}
public static function start(int $actor, string $mobile): array
public static function start(int $actor, string $mobile, ?string $signerName = null): array
{
$scope = self::scope($actor);
$mobile = trim($mobile);
if (self::quickEnabled() && $mobile === '') {
if (self::quickEnabled() && $mobile === '' && $signerName === null) {
return self::startQuick($scope);
}
if (!self::validMobile($mobile)) {
@@ -90,15 +97,30 @@ final class DoctorEsignIdentityService
}
$mobileHash = self::mobileHash($scope, $mobile);
$snapshot = self::snapshot($scope);
$name = $snapshot['name'];
if ($signerName !== null) {
if (!self::reviewerNameEntry($snapshot)) {
throw new DomainException('仅未绑定签署身份的审核人可填写证件姓名');
}
$name = trim($signerName);
if (!preg_match('/^[\p{Han}A-Za-z][\p{Han}A-Za-z·()\-]{1,49}$/uD', $name)) {
throw new DomainException('请输入与本人证件一致的姓名');
}
}
if (self::hasLease($snapshot['flow'])) {
return self::processing();
}
$client = new EsignClient();
if (self::hasCurrentFlow($snapshot) && hash_equals($snapshot['flow']['mobile_hash'], $mobileHash)) {
$flow = $snapshot['flow'];
$sameSubject = $flow && $name === $snapshot['name'] && hash_equals($flow['mobile_hash'], $mobileHash);
if (self::hasCurrentFlow($snapshot) && ($sameSubject || $signerName !== null)) {
[$status, $person] = self::identityResult($client, $snapshot);
if ($pending = self::pendingState($snapshot, $status, ($person['idNo'] ?? null) === null)) {
self::assertUnchanged($scope, $snapshot);
return $pending;
// A verified flow must finish binding under the name the provider verified.
if ($sameSubject || $status === 'SUCCESS') {
if ($pending = self::pendingState($snapshot, $status, ($person['idNo'] ?? null) === null)) {
self::assertUnchanged($scope, $snapshot);
return $pending;
}
}
}
$state = self::readiness($client, $snapshot, true, !self::quickEnabled())['state'];
@@ -110,16 +132,17 @@ final class DoctorEsignIdentityService
$token = self::claim($scope, $snapshot);
try {
$accountId = $snapshot['account']['account_id'] ?? null;
$flow = $client->startDoctorIdentity($snapshot['name'], 'doctor-enrollment-' . $token, $mobile, $accountId);
$flow = $client->startDoctorIdentity($name, 'doctor-enrollment-' . $token, $mobile, $accountId);
self::finish($scope, $snapshot, $token, [
'doctor_name' => $snapshot['name'],
'doctor_name' => $name,
'account_id' => $accountId ?? '',
'flow_id' => $flow['flow_id'],
'auth_url' => $flow['auth_url'],
'mobile_hash' => $mobileHash,
'flow_started_at' => time(),
]);
return self::state('authenticating', '请完成医师实名认证') + ['auth_url' => $flow['auth_url']];
return self::state('authenticating', '请完成医师实名认证') + ['auth_url' => $flow['auth_url']]
+ self::reviewerNameEntry($snapshot);
} catch (\Throwable $error) {
self::release($scope, $snapshot, $token);
throw $error;
@@ -281,7 +304,7 @@ final class DoctorEsignIdentityService
$token = self::claim($scope, $snapshot);
try {
$client = new EsignClient();
[$status, $person, $completedAt] = self::identityResult($client, $snapshot);
[$status, $person] = self::identityResult($client, $snapshot);
if ($status === 'INIT' || $status === 'ING') {
self::finish($scope, $snapshot, $token, []);
return self::pendingState($snapshot, $status, false);
@@ -293,8 +316,8 @@ final class DoctorEsignIdentityService
self::finish($scope, $snapshot, $token, null);
throw new DomainException('认证证件与签署账号不一致,请联系管理员核实');
}
$providerExpiry = intdiv($client->willingnessValidity($snapshot['flow']['flow_id'])['expires_at'], 1000);
$expires = min($providerExpiry, min(time(), $completedAt) + self::QUICK_LIFETIME);
// 授权有效期以 e签宝返回为准(快捷签授权通常为 30 天)
$expires = intdiv($client->willingnessValidity($snapshot['flow']['flow_id'])['expires_at'], 1000);
}
if ($expires <= time()) {
self::finish($scope, $snapshot, $token, null);
@@ -367,32 +390,46 @@ final class DoctorEsignIdentityService
return ['name' => $signerName, 'idNo' => $ready['idNo'], 'mobile' => $ready['mobile']];
}
private static function readiness(EsignClient $client, array $snapshot, bool $allowCertificateRecovery = false, bool $requireMobile = true): array
{
private static function readiness(
EsignClient $client,
array $snapshot,
bool $allowCertificateRecovery = false,
bool $requireMobile = true,
bool $reportUnconfirmedCertificate = false
): array {
if (!$snapshot['account']) {
return ['state' => self::state('unbound', '请先完成医师实名认证')];
}
$idNo = self::accountDocument($client, $snapshot);
try {
[$start, $end] = self::certificatePeriod($client, $snapshot['account']['account_id']);
} catch (EsignProviderException $error) {
// The SDK does not define a distinct response code for no available certificate.
if (!$allowCertificateRecovery || !str_starts_with($error->providerCode, 'PROVIDER_')) {
} catch (EsignProviderException | DomainException $error) {
// A provider rejection can include the case where no certificate has been issued yet.
$allowRenewal = $allowCertificateRecovery && $error instanceof EsignProviderException
&& str_starts_with($error->providerCode, 'PROVIDER_');
if (!$reportUnconfirmedCertificate && !$allowRenewal) {
throw $error;
}
return ['state' => self::state('renewal_required', '暂时无法确认签署证书有效期,可重新查询或发起续期')];
$message = $allowRenewal ? '暂时无法确认签署证书有效期,可重新查询或发起续期' : '暂时无法确认签署证书有效期,请重新查询';
return ['state' => self::state('renewal_required', $message) + ['certificate_state' => 'unconfirmed']];
}
if ($start === 0 || $start > time() || $end <= time()) {
return ['state' => self::state('renewal_required', '医师签署证书需要续期,请重新认证', $end)];
$now = time();
$valid = $start > 0 && $start <= $now && $end > $now;
$mobile = $valid && $requireMobile ? $client->authorizedMobile($snapshot['name'], $idNo) : null;
if (!$valid || $end <= time()) {
return ['state' => self::state('renewal_required', '医师签署证书需要续期,请重新认证', max(0, $end))
+ ['certificate_state' => 'renewal_required']];
}
$mobile = $requireMobile ? $client->authorizedMobile($snapshot['name'], $idNo) : null;
if ($requireMobile && !self::validMobile($mobile)) {
return ['state' => self::state('needs_mobile', '请完成实名手机号认证', $end)];
return ['state' => self::state('needs_mobile', '请完成实名手机号认证', $end) + ['certificate_state' => 'valid']];
}
if ($end <= time()) {
return ['state' => self::state('renewal_required', '医师签署证书需要续期,请重新认证', $end)];
}
return ['state' => self::state('ready', '医师电子签署已就绪', $end), 'idNo' => $idNo, 'mobile' => $mobile];
return [
'state' => self::state('ready', '医师电子签署已就绪', $end) + ['certificate_state' => 'valid'],
'idNo' => $idNo,
'mobile' => $mobile,
];
}
private static function accountDocument(EsignClient $client, array $snapshot): string
@@ -415,14 +452,14 @@ final class DoctorEsignIdentityService
private static function certificatePeriod(EsignClient $client, string $accountId): array
{
$dates = $client->certificateValidity($accountId);
return [self::certificateTime($dates['startDate'] ?? ''), self::certificateTime($dates['endDate'] ?? '')];
return [self::certificateTime($dates['startDate'] ?? null), self::certificateTime($dates['endDate'] ?? null)];
}
/** SDK validity timestamps are local to the configured SDK timezone. */
private static function certificateTime(string $value): int
private static function certificateTime(mixed $value): int
{
if ($value === '') {
return 0;
if (!is_string($value) || !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/D', $value)) {
throw new DomainException('暂时无法确认医师签署证书有效期,请稍后重试');
}
$zone = new DateTimeZone((string) Config::get('esign.sdk_timezone', 'Asia/Shanghai'));
$time = DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', $value, $zone);
@@ -476,7 +513,8 @@ final class DoctorEsignIdentityService
$quick = $snapshot['flow_table'] === self::QUICK_FLOW_TABLE;
if ($status === 'INIT' || $status === 'ING') {
$message = $quick ? '请完成快捷签人脸认证' : '请完成医师实名认证';
return self::state('authenticating', $message, 0, $quick) + ['auth_url' => $snapshot['flow']['auth_url']];
return self::state('authenticating', $message, 0, $quick) + ['auth_url' => $snapshot['flow']['auth_url']]
+ self::reviewerNameEntry($snapshot);
}
if ($status === 'SUCCESS') {
$needed = !$snapshot['account'] && $documentMissing;
@@ -506,18 +544,30 @@ final class DoctorEsignIdentityService
->whereNull('delete_time')->lock($lock)->value('name'));
$doctor = $actor > 0 && Db::name('admin_role')->where('admin_id', $actor)
->where('role_id', self::DOCTOR_ROLE_ID)->lock($lock)->value('admin_id');
if ($name === '' || (!$doctor && !(PrescriptionReviewEsignService::enabled()
&& PrescriptionReviewEsignService::canEnrollReviewer($actor)))) {
$reviewer = !$doctor && PrescriptionReviewEsignService::enabled()
&& PrescriptionReviewEsignService::canEnrollReviewer($actor);
if ($name === '' || (!$doctor && !$reviewer)) {
throw new DomainException('仅当前医师本人可使用医师电子签署认证');
}
$account = Db::name('doctor_esign_account')->where($scope)->lock($lock)->find();
$flow = Db::name($flowTable)->where($scope)->lock($lock)->find();
$name = PrescriptionEsignService::configuredSignerName(['doctor_name' => $name], $actor);
if ($reviewer) {
// Reviewers can have a display name unrelated to the legal name in the certificate.
$name = $account['doctor_name'] ?? (($flow['doctor_name'] ?? '') ?: $name);
}
return [
'name' => PrescriptionEsignService::configuredSignerName(['doctor_name' => $name], $actor),
'account' => Db::name('doctor_esign_account')->where($scope)->lock($lock)->find(),
'flow' => Db::name($flowTable)->where($scope)->lock($lock)->find(),
'flow_table' => $flowTable,
'name' => $name, 'account' => $account, 'flow' => $flow, 'flow_table' => $flowTable,
'reviewer' => $reviewer,
];
}
private static function reviewerNameEntry(array $snapshot): array
{
return $snapshot['reviewer'] && !$snapshot['account'] && $snapshot['flow_table'] === self::IDENTITY_FLOW_TABLE
? ['can_edit_signer_name' => true] : [];
}
private static function hasCurrentFlow(array $snapshot): bool
{
$flow = $snapshot['flow'];