- 药师签署验签通过后,立即把最终双签 PDF 以私有对象上传到后台配置的腾讯云 COS, 对象键 private/esign/prescription/<环境>/<处方ID>-<sha256>.pdf,带 Content-MD5 校验,不返回任何 URL - 上传失败不影响签署:记录原因并指数退避,由 esign:archive-work 每分钟补传(最长 1 小时一次) - 签署日志新增「签署件已归档至云存储」/「归档未完成,系统将自动重试」 - 迁移 2026_11_27_prescription_review_archive.sql:归档字段 + 定时任务登记 Co-Authored-By: Claude Opus 5 (1M context) <[EMAIL]>
67 lines
3.2 KiB
PHP
67 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\esign;
|
|
|
|
use DomainException;
|
|
use think\facade\Db;
|
|
|
|
/** 药师签署后的最终双签 PDF 归档到私有 COS;本地原件不变,归档失败不影响签署。 */
|
|
final class PrescriptionEsignArchive
|
|
{
|
|
private const TABLE = 'prescription_review_esign';
|
|
private const MAX_RETRY_DELAY = 3600;
|
|
|
|
/** 上传一张处方的最终 PDF。从不抛出:失败只记录并按退避时间交给 esign:archive-work 重试。 */
|
|
public static function upload(int $id): bool
|
|
{
|
|
try {
|
|
$signed = PrescriptionReviewEsignService::verifiedPdf($id);
|
|
if ($signed === null) {
|
|
throw new DomainException('药师签署尚未通过验签,暂不归档');
|
|
}
|
|
$archived = (string) Db::name(self::TABLE)->where('prescription_id', $id)->value('archive_hash');
|
|
if ($archived === $signed['sha256']) {
|
|
return true;
|
|
}
|
|
$cos = app(PrescriptionEsignCos::class);
|
|
$object = $cos->location() + ['key' => 'private/esign/prescription/' . $signed['environment'] . '/'
|
|
. $id . '-' . $signed['sha256'] . '.pdf'];
|
|
$cos->put($object, $signed['pdf']);
|
|
// 以 signed_hash 作条件:上传期间若重新签署,则不把旧文件记为新签署件的归档。
|
|
Db::name(self::TABLE)->where('prescription_id', $id)->where('signed_hash', $signed['sha256'])->update([
|
|
'archive_bucket' => $object['bucket'], 'archive_region' => $object['region'], 'archive_key' => $object['key'],
|
|
'archive_hash' => $signed['sha256'], 'archived_at' => time(), 'archive_attempts' => 0,
|
|
'archive_attempted_at' => time(), 'archive_next_retry_at' => 0, 'archive_error' => '',
|
|
]);
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::defer($id, $e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** 待归档:已签署、归档哈希与签署件不一致、且已到重试时间。 */
|
|
public static function pending(int $limit): array
|
|
{
|
|
return Db::name(self::TABLE)->where('state', 'signed')->where('signed_hash', '<>', '')
|
|
->whereColumn('archive_hash', '<>', 'signed_hash')->where('archive_next_retry_at', '<=', time())
|
|
->order('archive_next_retry_at')->limit($limit)->column('prescription_id');
|
|
}
|
|
|
|
private static function defer(int $id, \Throwable $error): void
|
|
{
|
|
try {
|
|
$attempts = (int) Db::name(self::TABLE)->where('prescription_id', $id)->value('archive_attempts') + 1;
|
|
$message = $error instanceof DomainException ? $error->getMessage() : '归档暂不可用,系统将自动重试';
|
|
Db::name(self::TABLE)->where('prescription_id', $id)->update([
|
|
'archive_attempts' => $attempts, 'archive_attempted_at' => time(),
|
|
'archive_next_retry_at' => time() + min(self::MAX_RETRY_DELAY, 60 * 2 ** min(6, $attempts - 1)),
|
|
'archive_error' => mb_substr($message, 0, 240),
|
|
]);
|
|
} catch (\Throwable) {
|
|
// 记录失败本身也不能影响签署;定时任务会再次选中该处方。
|
|
}
|
|
}
|
|
}
|