104 lines
3.4 KiB
PHP
104 lines
3.4 KiB
PHP
<?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)), '+/', '-_'), '=');
|
||
}
|
||
}
|