修正 Proxied、 SSL/TLS 设为 Flexible、开启 Always Use HTTPS
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\scrm;
|
||||
|
||||
use app\common\service\SplitTicketSyncLogger;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
@@ -47,6 +48,12 @@ abstract class AbstractScrmSpider implements ScrmSpiderInterface
|
||||
public function run(): UnifiedScrmData
|
||||
{
|
||||
$config = $this->getSpiderConfig();
|
||||
SplitTicketSyncLogger::log('spider', 'run start', [
|
||||
'nodeHost' => $this->nodeHost,
|
||||
'pageUrl' => $config['pageUrl'] ?? '',
|
||||
'listApi' => $config['listApi'] ?? '',
|
||||
'paginationMode' => $config['paginationMode'] ?? self::MODE_FETCH,
|
||||
]);
|
||||
|
||||
$listApi = (string) ($config['listApi'] ?? '');
|
||||
$detailApi = $config['detailApi'] ?? null;
|
||||
@@ -67,10 +74,16 @@ abstract class AbstractScrmSpider implements ScrmSpiderInterface
|
||||
]);
|
||||
|
||||
if (empty($initResult['success'])) {
|
||||
SplitTicketSyncLogger::log('spider', 'auth-and-intercept failed', [
|
||||
'error' => $initResult['error'] ?? '未知',
|
||||
]);
|
||||
throw new Exception('初始化失败: ' . ($initResult['error'] ?? '未知'));
|
||||
}
|
||||
|
||||
$interceptedApis = $initResult['interceptedApis'];
|
||||
SplitTicketSyncLogger::log('spider', 'auth-and-intercept ok', [
|
||||
'intercepted' => array_keys($interceptedApis),
|
||||
]);
|
||||
$cookies = $initResult['cookies'];
|
||||
|
||||
if (!isset($interceptedApis[$listApi])) {
|
||||
@@ -87,6 +100,10 @@ abstract class AbstractScrmSpider implements ScrmSpiderInterface
|
||||
|
||||
$totalPages = $this->extractListTotalPages($listApiNode['data'], $countData);
|
||||
$mode = $config['paginationMode'] ?? self::MODE_FETCH;
|
||||
SplitTicketSyncLogger::log('spider', 'pagination plan', [
|
||||
'totalPages' => $totalPages,
|
||||
'mode' => $mode,
|
||||
]);
|
||||
|
||||
if ($totalPages > 1 || $totalPages === null) {
|
||||
if ($mode === self::MODE_FETCH && $totalPages !== null) {
|
||||
@@ -137,7 +154,15 @@ abstract class AbstractScrmSpider implements ScrmSpiderInterface
|
||||
}
|
||||
}
|
||||
|
||||
return $this->parseToUnifiedData($detailData, $allListPagesData);
|
||||
$result = $this->parseToUnifiedData($detailData, $allListPagesData);
|
||||
SplitTicketSyncLogger::log('spider', 'run done', [
|
||||
'todayNewCount' => $result->todayNewCount,
|
||||
'totalOnline' => $result->totalOnline,
|
||||
'totalOffline' => $result->totalOffline,
|
||||
'total' => $result->total,
|
||||
'numberCount' => count($result->numbers),
|
||||
]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,20 +171,63 @@ abstract class AbstractScrmSpider implements ScrmSpiderInterface
|
||||
*/
|
||||
protected function requestNode(string $endpoint, array $payload, int $timeout = 60): array
|
||||
{
|
||||
$ch = curl_init($this->nodeHost . $endpoint);
|
||||
$url = $this->nodeHost . $endpoint;
|
||||
$started = microtime(true);
|
||||
SplitTicketSyncLogger::log('node_request', 'POST ' . $endpoint, [
|
||||
'url' => $url,
|
||||
'timeout' => $timeout,
|
||||
'payload' => $payload,
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload, JSON_UNESCAPED_UNICODE));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$elapsedMs = (int) round((microtime(true) - $started) * 1000);
|
||||
if (curl_errno($ch)) {
|
||||
$err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
SplitTicketSyncLogger::log('node_response', 'curl error on ' . $endpoint, [
|
||||
'httpCode' => $httpCode,
|
||||
'elapsedMs' => $elapsedMs,
|
||||
'error' => $err,
|
||||
]);
|
||||
throw new Exception($err);
|
||||
}
|
||||
curl_close($ch);
|
||||
$decoded = json_decode((string) $response, true);
|
||||
$summary = is_array($decoded) ? self::summarizeNodeResponse($decoded) : ['raw' => mb_substr((string) $response, 0, 300, 'UTF-8')];
|
||||
SplitTicketSyncLogger::log('node_response', 'POST ' . $endpoint, array_merge([
|
||||
'httpCode' => $httpCode,
|
||||
'elapsedMs' => $elapsedMs,
|
||||
'responseSize' => strlen((string) $response),
|
||||
], $summary));
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $decoded
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function summarizeNodeResponse(array $decoded): array
|
||||
{
|
||||
$summary = [
|
||||
'success' => $decoded['success'] ?? null,
|
||||
'error' => $decoded['error'] ?? null,
|
||||
];
|
||||
if (isset($decoded['interceptedApis']) && is_array($decoded['interceptedApis'])) {
|
||||
$summary['interceptedApis'] = array_keys($decoded['interceptedApis']);
|
||||
}
|
||||
if (isset($decoded['results']) && is_array($decoded['results'])) {
|
||||
$summary['resultApis'] = array_keys($decoded['results']);
|
||||
}
|
||||
if (isset($decoded['data']) && is_array($decoded['data'])) {
|
||||
$summary['dataPages'] = count($decoded['data']);
|
||||
}
|
||||
return $summary;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user