CaiWuXiTong/modules/EcommerceFinance/Services/SupplierSummaryShareLink.php
苏木 770ddea8a9 feat(供应商对账): 增加免登录送货确认链接 (#1180)
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-15 11:36:27 +08:00

104 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Modules\EcommerceFinance\Services;
use DateTimeInterface;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\URL;
use Throwable;
/**
* 厂家月度货款确认页的临时免登录链接。
*
* 令牌承载公司与确认记录,URL 再做 Laravel 签名绑定。厂家只能打开本次
* 推送对应的账期,不暴露其他厂家、其他月份或财务端内部页面。
*/
class SupplierSummaryShareLink
{
public const TTL_DAYS = 30;
public function pageUrl(
int $companyId,
string $supplier,
string $month,
string $pushBasis,
string $requestId,
): string {
return $this->signedRoute(
'signed.supplier-summary-confirmations.show',
$this->token($companyId, $supplier, $month, $pushBasis, $requestId),
$companyId,
now()->addDays(self::TTL_DAYS),
);
}
public function signedRoute(
string $routeName,
string $token,
int $companyId,
?DateTimeInterface $expires = null,
): string {
return URL::temporarySignedRoute($routeName, $expires ?? now()->addDays(self::TTL_DAYS), [
'company_id' => $companyId,
'token' => $token,
]);
}
/** @return array{supplier:string,month:string,push_basis:string,request_id:string}|null */
public function resolveToken(string $token, int $companyId): ?array
{
try {
$padding = (4 - strlen($token) % 4) % 4;
$encoded = strtr($token . str_repeat('=', $padding), '-_', '+/');
$encrypted = base64_decode($encoded, true);
if ($encrypted === false) {
return null;
}
$payload = json_decode(Crypt::decryptString($encrypted), true, flags: JSON_THROW_ON_ERROR);
} catch (Throwable) {
return null;
}
$supplier = is_array($payload) ? trim((string) ($payload['supplier'] ?? '')) : '';
$month = is_array($payload) ? trim((string) ($payload['month'] ?? '')) : '';
$pushBasis = is_array($payload) ? trim((string) ($payload['push_basis'] ?? '')) : '';
$requestId = is_array($payload) ? trim((string) ($payload['request_id'] ?? '')) : '';
$payloadCompanyId = is_array($payload) ? (int) ($payload['company_id'] ?? 0) : 0;
if (
$supplier === ''
|| $payloadCompanyId !== $companyId
|| preg_match('/^\d{4}-\d{2}$/', $month) !== 1
|| ! in_array($pushBasis, ['settlement', 'mine'], true)
|| preg_match('/^supplier-summary-l\d+-[a-f0-9]{16,128}$/', $requestId) !== 1
) {
return null;
}
return [
'supplier' => $supplier,
'month' => $month,
'push_basis' => $pushBasis,
'request_id' => $requestId,
];
}
private function token(
int $companyId,
string $supplier,
string $month,
string $pushBasis,
string $requestId,
): string {
$payload = json_encode([
'company_id' => $companyId,
'supplier' => $supplier,
'month' => $month,
'push_basis' => $pushBasis,
'request_id' => $requestId,
], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
return rtrim(strtr(base64_encode(Crypt::encryptString($payload)), '+/', '-_'), '=');
}
}