From cd85ad0f6362cefd47ea23926c17a4ffe31917ed Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Wed, 25 Nov 2026 21:01:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E9=80=92=E6=8A=A5=E4=BB=B7?= =?UTF-8?q?=E5=92=8C=E5=90=88=E4=BD=9C=E5=90=88=E5=90=8C=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E4=B9=99=E6=96=B9=E6=AD=A3=E6=96=87=E7=AD=BE=E7=AB=A0=20(#1440?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Support/CourierAgreementPdf.php | 39 ++++---------- .../CourierAgreementSignatureService.php | 11 +++- app/Support/CourierQuotePdf.php | 14 ++++- app/Support/CourierQuoteSignatureService.php | 51 +++++++++++++------ app/Support/PdfSigningPositions.php | 46 +++++++++++++++++ .../partials/cooperation-contract.blade.php | 2 +- .../business/pdf/courier-quote.blade.php | 2 +- .../CourierAgreementSignatureWorkflowTest.php | 7 ++- .../CourierQuoteSignatureWorkflowTest.php | 13 ++++- 9 files changed, 132 insertions(+), 53 deletions(-) create mode 100644 app/Support/PdfSigningPositions.php diff --git a/app/Support/CourierAgreementPdf.php b/app/Support/CourierAgreementPdf.php index 40099fb0..cf742594 100644 --- a/app/Support/CourierAgreementPdf.php +++ b/app/Support/CourierAgreementPdf.php @@ -4,7 +4,6 @@ namespace App\Support; use App\Models\Business\CourierAgreementSignature; use DOMDocument; -use DOMElement; use DOMXPath; use Dompdf\FrameReflower\Text; use Illuminate\Support\Carbon; @@ -41,39 +40,19 @@ final class CourierAgreementPdf $pdf->setPaper('a4', 'portrait'); $pdf->loadHTML($html, 'UTF-8'); $this->applyLatinFont($pdf->getDomPDF()->getDom()); - $positions = []; - $callbacks = []; - foreach ($pdf->getDomPDF()->getCallbacks() as $event => $handlers) { - foreach ($handlers as $handler) { - $callbacks[] = ['event' => $event, 'f' => $handler]; - } - } - if ($supplierContract) { - $pdf->getDomPDF()->setCallbacks([...$callbacks, [ - 'event' => 'end_frame', - 'f' => static function ($frame, $canvas) use (&$positions): void { - $node = $frame->get_node(); - if (! $node instanceof DOMElement || $node->getAttribute('data-supplier-signature-party') !== 'b') { - return; - } - $box = $frame->get_position(); - $positions[] = [ - 'positionPage' => (string) $canvas->get_page_number(), - 'positionX' => round($box['x'] + $frame->get_margin_width() / 2, 2), - 'positionY' => round($canvas->get_height() - $box['y'] - $frame->get_margin_height() / 2, 2), - ]; - }, - ]]); - } // Dompdf otherwise treats uninterrupted Chinese text as one word, // moving a whole clause to the next line after its Latin number. $wordBreakPattern = Text::$_wordbreak_pattern; Text::$_wordbreak_pattern = '/([^\S\xA0\x{202F}\x{2007}\n]+|\R|\-+|\xAD+|(?<=[\p{Han}\x{3000}-\x{303F}\x{FF00}-\x{FFEF}])(?![,。、;:?!)】》]))/u'; try { - $bytes = $pdf->output(); + $result = match (true) { + $supplierContract => PdfSigningPositions::render($pdf, 'data-supplier-signature-party', 'b'), + $signature->document_type === 'cooperation-contract' => PdfSigningPositions::render($pdf, 'data-counterparty-signature', 'b'), + default => ['bytes' => $pdf->output(), 'signing_positions' => []], + }; + ['bytes' => $bytes, 'signing_positions' => $positions] = $result; } finally { Text::$_wordbreak_pattern = $wordBreakPattern; - $pdf->getDomPDF()->setCallbacks($callbacks); } if (! is_string($bytes) || ! str_starts_with($bytes, '%PDF-')) { @@ -84,7 +63,11 @@ final class CourierAgreementPdf throw new RuntimeException('供应商合同的必签盖章位置不完整,未推送,请检查合同模板。'); } - return ['bytes' => $bytes, 'signing_positions' => $positions]; + if (! $supplierContract && $signature->document_type === 'cooperation-contract' && count($positions) !== 1) { + throw new RuntimeException('未能定位快递合作合同乙方正文签章区,未推送。'); + } + + return $result; } private function applyLatinFont(DOMDocument $document): void diff --git a/app/Support/CourierAgreementSignatureService.php b/app/Support/CourierAgreementSignatureService.php index 380294f1..a809520f 100644 --- a/app/Support/CourierAgreementSignatureService.php +++ b/app/Support/CourierAgreementSignatureService.php @@ -32,7 +32,7 @@ final class CourierAgreementSignatureService } $payload = is_array($data['payload'] ?? null) ? $data['payload'] : $data; - unset($payload['supplier_signing_positions']); + unset($payload['supplier_signing_positions'], $payload['cooperation_signing_positions']); $payload['authorization_items'] = array_values(array_map('strval', $data['authorization_items'] ?? [])); $idempotencyKey = 'agreement-' . hash('sha256', $companyId . '|' . (string) ($data['idempotency_key'] ?? '')); $businessNo = 'AGR-' . strtoupper(substr(hash('sha256', $idempotencyKey), 0, 20)); @@ -102,7 +102,9 @@ final class CourierAgreementSignatureService $rendered = $this->pdf->renderForSigning($signature); $bytes = $rendered['bytes']; if ($rendered['signing_positions'] !== []) { - $signature->payload = [...$signature->payload, 'supplier_signing_positions' => $rendered['signing_positions']]; + $positionsKey = ($payload['partner_type'] ?? 'courier') === 'supplier' + ? 'supplier_signing_positions' : 'cooperation_signing_positions'; + $signature->payload = [...$signature->payload, $positionsKey => $rendered['signing_positions']]; } $signature->source_pdf_sha256 = hash('sha256', $bytes); if (! Storage::disk('uploads')->put($signature->source_pdf_path, $bytes)) { @@ -408,6 +410,11 @@ final class CourierAgreementSignatureService if (count($positions) !== $expected) { throw new RuntimeException('旧任务缺少必签盖章位置,请重新发起供应商合作合同。'); } + } else { + $positions = $payload['cooperation_signing_positions'] ?? []; + if (count($positions) !== 1) { + throw new RuntimeException('旧任务缺少乙方正文签章位置,请重新发起快递合作合同。'); + } } $counterparty = [ 'signer_type' => $counterpartyIsPerson ? 0 : 1, diff --git a/app/Support/CourierQuotePdf.php b/app/Support/CourierQuotePdf.php index 4531db08..fdd83374 100644 --- a/app/Support/CourierQuotePdf.php +++ b/app/Support/CourierQuotePdf.php @@ -13,6 +13,11 @@ final class CourierQuotePdf } public function render(CourierQuoteVersion $version, ?string $signatureDate = null, ?string $signingSubject = null, ?string $counterpartyName = null): string + { + return $this->renderForSigning($version, $signatureDate, $signingSubject, $counterpartyName)['bytes']; + } + + public function renderForSigning(CourierQuoteVersion $version, ?string $signatureDate = null, ?string $signingSubject = null, ?string $counterpartyName = null): array { $data = $this->confirmation->data($version, $signingSubject, $counterpartyName); $signatureDate = Carbon::parse($signatureDate ?: now())->format('Y年n月j日'); @@ -36,12 +41,17 @@ final class CourierQuotePdf $pdf = app('dompdf.wrapper'); $pdf->setPaper('a4', 'landscape'); $pdf->loadHTML($html, 'UTF-8'); - $bytes = $pdf->output(); + $result = PdfSigningPositions::render($pdf, 'data-counterparty-signature', 'b'); + $bytes = $result['bytes']; if (! is_string($bytes) || ! str_starts_with($bytes, '%PDF-')) { throw new RuntimeException('报价 PDF 生成失败。'); } - return $bytes; + if (count($result['signing_positions']) !== 1) { + throw new RuntimeException('未能定位报价确认单乙方正文签章区,未生成待签文件。'); + } + + return $result; } } diff --git a/app/Support/CourierQuoteSignatureService.php b/app/Support/CourierQuoteSignatureService.php index 2255d26e..4ecf8d2f 100644 --- a/app/Support/CourierQuoteSignatureService.php +++ b/app/Support/CourierQuoteSignatureService.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; +use League\Flysystem\UnableToReadFile; use RuntimeException; use Throwable; @@ -26,7 +27,8 @@ final class CourierQuoteSignatureService $existing = CourierQuoteSignature::query()->disableCache() ->where('quote_version_id', $version->id)->lockForUpdate()->first(); - if ($signatureDate === null && $signingSubject === null && $counterpartyName === null && $existing && $this->sourcePdfIsIntact($existing)) { + if ($signatureDate === null && $signingSubject === null && $counterpartyName === null && $existing && $this->sourcePdfIsIntact($existing) + && (! $existing->canSend() || $this->signingPositions($existing) !== [])) { return $existing; } @@ -45,13 +47,16 @@ final class CourierQuoteSignatureService } $counterpartyName = trim($counterpartyName ?? app(CourierQuoteConfirmation::class)->counterpartyName($version)); - $bytes = $this->pdf->render($version, $signatureDate, $signingSubject, $counterpartyName); + $rendered = $this->pdf->renderForSigning($version, $signatureDate, $signingSubject, $counterpartyName); + $bytes = $rendered['bytes']; // Every attempt owns its files, including PDFs regenerated before sending. $path = $this->path($version, Str::uuid() . '/source.pdf'); $sha256 = hash('sha256', $bytes); - if (! Storage::disk('uploads')->put($path, $bytes)) { - throw new RuntimeException('报价 PDF 无法写入私有业务存储。'); + $disk = Storage::disk('uploads'); + if (! $disk->put($path, $bytes) + || ! $disk->put($path . '.signing.json', json_encode($rendered['signing_positions'], JSON_THROW_ON_ERROR))) { + throw new RuntimeException('报价 PDF 或正文签章位置无法写入私有业务存储。'); } $signature = $existing ?? new CourierQuoteSignature([ @@ -135,6 +140,10 @@ final class CourierQuoteSignatureService try { $client = app(EsignClient::class); $client->assertSignFlowInitiator($data['signing_subject'] ?? null); + $positions = $this->signingPositions($signature); + if ($positions === []) { + throw new RuntimeException('待签报价缺少乙方正文签章位置,请重新生成后再推送。'); + } $identity = ($data['signer_type'] ?? 'organization') === 'organization' && blank($signature->operator_name) ? $client->organizationSignerIdentity($signature->organization_name, $signature->operator_account) : []; @@ -142,23 +151,18 @@ final class CourierQuoteSignatureService $fileName = 'quote-' . $signature->version->version_code . '.pdf'; $fileId = $client->uploadPdf($bytes, $fileName); $signer = [ - ...$identity, 'initiator_name' => $data['signing_subject'] ?? null, 'flow_title' => $signature->version->carrier_name . ' ' . $signature->version->version_code . ' 报价确认', - 'organization_name' => $signature->organization_name, - 'operator_name' => $signature->operator_name, - 'operator_account' => $signature->operator_account, - 'notice_types' => $signature->notice_types, - ]; - if (($data['signer_type'] ?? 'organization') === 'person') { - // 个人快递以本人身份签署,不走企业经办人。 - $signer['signers'] = [[ - 'signer_type' => 0, + 'signers' => [[ + ...$identity, + 'signer_type' => ($data['signer_type'] ?? 'organization') === 'person' ? 0 : 1, + 'organization_name' => $signature->organization_name, 'operator_name' => $signature->operator_name, 'operator_account' => $signature->operator_account, 'notice_types' => $signature->notice_types, - ]]; - } + 'signing_positions' => $positions, + ]], + ]; $signFlowId = $client->createSignFlow($fileId, $fileName, $signer, $signature->expires_at->getTimestampMs()); $signature->update([ @@ -318,6 +322,21 @@ final class CourierQuoteSignatureService }); } + private function signingPositions(CourierQuoteSignature $signature): array + { + try { + $metadata = Storage::disk('uploads')->get($signature->source_pdf_path . '.signing.json'); + } catch (UnableToReadFile) { + return []; + } + + $positions = json_decode((string) $metadata, true); + $position = $positions[0] ?? null; + + return is_array($position) && isset($position['positionPage'], $position['positionX'], $position['positionY']) + ? [$position] : []; + } + public function sourcePdfIsIntact(CourierQuoteSignature $signature): bool { if (blank($signature->source_pdf_path) || blank($signature->source_pdf_sha256)) { diff --git a/app/Support/PdfSigningPositions.php b/app/Support/PdfSigningPositions.php new file mode 100644 index 00000000..ebc76289 --- /dev/null +++ b/app/Support/PdfSigningPositions.php @@ -0,0 +1,46 @@ +getDomPDF(); + $callbacks = []; + foreach ($dompdf->getCallbacks() as $event => $handlers) { + foreach ($handlers as $handler) { + $callbacks[] = ['event' => $event, 'f' => $handler]; + } + } + + $dompdf->setCallbacks([...$callbacks, [ + 'event' => 'end_frame', + 'f' => static function ($frame, $canvas) use (&$positions, $attribute, $value): void { + $node = $frame->get_node(); + if (! $node instanceof DOMElement || $node->getAttribute($attribute) !== $value) { + return; + } + + $box = $frame->get_position(); + $positions[] = [ + 'positionPage' => (string) $canvas->get_page_number(), + 'positionX' => round($box['x'] + $frame->get_margin_width() / 2, 2), + 'positionY' => round($canvas->get_height() - $box['y'] - $frame->get_margin_height() / 2, 2), + ]; + }, + ]]); + + try { + $bytes = $pdf->output(); + return ['bytes' => $bytes, 'signing_positions' => $positions]; + } finally { + $dompdf->setCallbacks($callbacks); + } + } +} diff --git a/resources/views/business/partials/cooperation-contract.blade.php b/resources/views/business/partials/cooperation-contract.blade.php index a2b2cd13..d9df2694 100644 --- a/resources/views/business/partials/cooperation-contract.blade.php +++ b/resources/views/business/partials/cooperation-contract.blade.php @@ -165,7 +165,7 @@
乙 方:{!! $contractText($payload['cooperation_party_b_name'] ?? '') !!}
- +代表人:
日 期:{!! $contractText($signatureDate) !!}
diff --git a/resources/views/business/pdf/courier-quote.blade.php b/resources/views/business/pdf/courier-quote.blade.php index b6e02b88..6e5ab141 100644 --- a/resources/views/business/pdf/courier-quote.blade.php +++ b/resources/views/business/pdf/courier-quote.blade.php @@ -150,7 +150,7 @@