171 lines
5.3 KiB
PHP
171 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Business\Piaoju;
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
use Illuminate\Support\Facades\Http;
|
|
use RuntimeException;
|
|
|
|
class PiaojuSellerQuotaClient
|
|
{
|
|
/**
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
public function fetchAll(): array
|
|
{
|
|
$this->prepare();
|
|
|
|
$rows = [];
|
|
$total = null;
|
|
$pageSize = max((int) config('piaoju.page_size', 100), 1);
|
|
$maxPages = max((int) config('piaoju.max_pages', 20), 1);
|
|
|
|
for ($page = 1; $page <= $maxPages; $page++) {
|
|
[$batch, $pageTotal] = $this->fetchPage($page, $pageSize);
|
|
$total ??= $pageTotal;
|
|
|
|
foreach ($batch as $row) {
|
|
if (! is_array($row)) {
|
|
continue;
|
|
}
|
|
|
|
$key = trim((string) ($row['id'] ?? $row['sellerCode'] ?? ''));
|
|
if ($key === '') {
|
|
throw new RuntimeException('piaoju_seller_key_missing');
|
|
}
|
|
$rows[$key] = $row;
|
|
}
|
|
|
|
if ($batch === []
|
|
|| ($total !== null && count($rows) >= $total)
|
|
|| ($total === null && count($batch) < $pageSize)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($total !== null && count($rows) !== $total) {
|
|
throw new RuntimeException('piaoju_seller_count_mismatch');
|
|
}
|
|
|
|
return array_values($rows);
|
|
}
|
|
|
|
/**
|
|
* @return array{0:list<array<string,mixed>>,1:?int}
|
|
*/
|
|
private function fetchPage(int $page, int $pageSize): array
|
|
{
|
|
$response = Http::acceptJson()
|
|
->withHeaders([
|
|
'Cookie' => $this->cookieHeader(),
|
|
'Origin' => 'https://fp.erp321.com',
|
|
'Referer' => 'https://fp.erp321.com/setting/companyBodyManage',
|
|
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
|
|
. '(KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36 Edg/157.0.0.0',
|
|
])
|
|
->connectTimeout(10)
|
|
->timeout(max((int) config('piaoju.timeout_seconds', 30), 1))
|
|
->retry(2, 300, throw: false)
|
|
->post(
|
|
rtrim((string) config('piaoju.api_base_url'), '/') . '/fp/fms/company/seller/page',
|
|
[
|
|
'data' => [
|
|
'coid' => (string) config('piaoju.coid'),
|
|
'uid' => (string) config('piaoju.uid'),
|
|
'ssProjectType' => (string) config('piaoju.project_type'),
|
|
'sellerNames' => [],
|
|
'status' => null,
|
|
'authStatus' => null,
|
|
],
|
|
'page' => [
|
|
'pageNum' => $page,
|
|
'pageSize' => $pageSize,
|
|
],
|
|
]
|
|
);
|
|
|
|
return $this->parseResponse($response);
|
|
}
|
|
|
|
/**
|
|
* @return array{0:list<array<string,mixed>>,1:?int}
|
|
*/
|
|
private function parseResponse(Response $response): array
|
|
{
|
|
if (! $response->successful()) {
|
|
throw new RuntimeException('piaoju_api_http_error');
|
|
}
|
|
|
|
$payload = $response->json();
|
|
if (! is_array($payload)) {
|
|
throw new RuntimeException('piaoju_api_invalid_response');
|
|
}
|
|
|
|
$code = $payload['code'] ?? null;
|
|
if ($code !== null && ! in_array((string) $code, ['0', '0000', '200'], true)) {
|
|
throw new RuntimeException(
|
|
in_array((string) $code, ['-1', '1006', '403'], true)
|
|
? 'piaoju_api_auth_failed'
|
|
: 'piaoju_api_error'
|
|
);
|
|
}
|
|
|
|
if (($payload['success'] ?? null) === false) {
|
|
throw new RuntimeException('piaoju_api_error');
|
|
}
|
|
|
|
$data = $payload['data'] ?? null;
|
|
$rows = null;
|
|
$total = null;
|
|
|
|
if (is_array($data)) {
|
|
if (array_is_list($data)) {
|
|
$rows = $data;
|
|
$total = isset($payload['total']) ? (int) $payload['total'] : null;
|
|
} else {
|
|
$rows = $data['list'] ?? $data['records'] ?? null;
|
|
$total = $data['total'] ?? $payload['total'] ?? null;
|
|
}
|
|
}
|
|
|
|
if (! is_array($rows)) {
|
|
throw new RuntimeException('piaoju_api_invalid_response');
|
|
}
|
|
|
|
return [
|
|
array_values(array_filter($rows, 'is_array')),
|
|
$total === null ? null : max((int) $total, 0),
|
|
];
|
|
}
|
|
|
|
private function prepare(): void
|
|
{
|
|
$this->cookieHeader();
|
|
|
|
if (trim((string) config('piaoju.coid')) === ''
|
|
|| trim((string) config('piaoju.uid')) === '') {
|
|
throw new RuntimeException('piaoju_api_context_missing');
|
|
}
|
|
}
|
|
|
|
private function cookieHeader(): string
|
|
{
|
|
$cookie = trim((string) config('piaoju.cookie', ''));
|
|
if ($cookie !== '') {
|
|
return $cookie;
|
|
}
|
|
|
|
$path = trim((string) config('piaoju.cookie_file', ''));
|
|
if ($path === '' || ! is_file($path)) {
|
|
throw new RuntimeException('piaoju_cookie_not_configured');
|
|
}
|
|
|
|
$cookie = trim((string) file_get_contents($path));
|
|
if ($cookie === '' || preg_match('/[\r\n]/', $cookie) === 1) {
|
|
throw new RuntimeException('piaoju_cookie_invalid');
|
|
}
|
|
|
|
return $cookie;
|
|
}
|
|
}
|