修改Whatshub工单的完成数量总计的计算方法
This commit is contained in:
Executable → Regular
+65
-11
@@ -12,7 +12,7 @@ use app\common\service\SplitTicketSyncLogger;
|
||||
/**
|
||||
* Whatshub 云控蜘蛛
|
||||
*
|
||||
* 时序:detailByShareCode API 直抓(优先)→ 失败则 Real Browser + Turnstile + UI 翻页
|
||||
* 时序:cURL detailByShareCode 拉号码 → Node 监听 statistics 算完成量 → 失败则完整 Node 爬虫
|
||||
*/
|
||||
class WhatshubSpider extends AbstractScrmSpider
|
||||
{
|
||||
@@ -52,25 +52,81 @@ class WhatshubSpider extends AbstractScrmSpider
|
||||
}
|
||||
|
||||
/**
|
||||
* API 优先:detailByShareCode 直抓;失败则回退 Node 爬虫
|
||||
* 混合同步:cURL 拉号码 + Node 监听 statistics 算完成量;任一失败则完整 Node fallback
|
||||
*/
|
||||
public function run(): UnifiedScrmData
|
||||
{
|
||||
$apiResult = $this->tryFetchViaShareCodeApi();
|
||||
if ($apiResult instanceof UnifiedScrmData) {
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub api path ok', [
|
||||
'count' => count($apiResult->numbers),
|
||||
$statsCount = $this->tryFetchStatisticsViaNode();
|
||||
|
||||
if ($apiResult instanceof UnifiedScrmData && $statsCount !== null) {
|
||||
$apiResult->todayNewCount = $statsCount;
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub hybrid path ok', [
|
||||
'numberCount' => count($apiResult->numbers),
|
||||
'todayNewCount' => $statsCount,
|
||||
]);
|
||||
return $apiResult;
|
||||
}
|
||||
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub api skipped or failed, fallback node', [
|
||||
'pageUrl' => $this->pageUrl,
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub hybrid incomplete, fallback full node', [
|
||||
'pageUrl' => $this->pageUrl,
|
||||
'apiOk' => $apiResult instanceof UnifiedScrmData,
|
||||
'statisticsOk' => $statsCount !== null,
|
||||
]);
|
||||
|
||||
return parent::run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Node 监听 statistics API,返回 dayNewFans + reDayNewFans 总和;失败返回 null
|
||||
*/
|
||||
private function tryFetchStatisticsViaNode(): ?int
|
||||
{
|
||||
$config = $this->getSpiderConfig();
|
||||
$antiBot = $config['antiBot'] ?? null;
|
||||
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub stats node request', [
|
||||
'pageUrl' => $this->pageUrl,
|
||||
'api' => self::API_DETAILS,
|
||||
]);
|
||||
|
||||
$result = $this->requestNode('/api/auth-and-intercept', [
|
||||
'pageUrl' => $config['pageUrl'],
|
||||
'apiUrls' => [self::API_DETAILS],
|
||||
'authActions' => $config['authActions'] ?? [],
|
||||
'antiBot' => $antiBot,
|
||||
], $this->resolveAuthInterceptTimeout(is_array($antiBot) ? $antiBot : null));
|
||||
|
||||
if (empty($result['success'])) {
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub stats node failed', [
|
||||
'error' => $result['error'] ?? '未知',
|
||||
'code' => $result['code'] ?? null,
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$intercepted = $result['interceptedApis'] ?? [];
|
||||
if (!isset($intercepted[self::API_DETAILS]['data']) || !is_array($intercepted[self::API_DETAILS]['data'])) {
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub stats node missing payload', [
|
||||
'intercepted' => array_keys(is_array($intercepted) ? $intercepted : []),
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$detailData = $intercepted[self::API_DETAILS]['data'];
|
||||
$dayNewFans = (int) ($detailData['data']['dayNewFans'] ?? 0);
|
||||
$reDayNewFans = (int) ($detailData['data']['reDayNewFans'] ?? 0);
|
||||
$total = $dayNewFans + $reDayNewFans;
|
||||
|
||||
SplitTicketSyncLogger::log('spider', 'whatshub stats node ok', [
|
||||
'dayNewFans' => $dayNewFans,
|
||||
'reDayNewFans' => $reDayNewFans,
|
||||
'total' => $total,
|
||||
]);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从短链 pageUrl 解析 shareCode,例如 /m/KMYBBInp2066/1 → KMYBBInp2066
|
||||
*/
|
||||
@@ -202,7 +258,6 @@ class WhatshubSpider extends AbstractScrmSpider
|
||||
private function parseShareCodeApiResponse(array $payload): UnifiedScrmData
|
||||
{
|
||||
$unifiedData = new UnifiedScrmData();
|
||||
$todayNewSum = 0;
|
||||
|
||||
foreach ($payload['data'] as $item) {
|
||||
if (!is_array($item) || empty($item['account'])) {
|
||||
@@ -213,11 +268,10 @@ class WhatshubSpider extends AbstractScrmSpider
|
||||
$isOnline = isset($item['isOnline']) && (int) $item['isOnline'] === 1;
|
||||
$dayNewFans = (int) ($item['dayNewFans'] ?? 0);
|
||||
|
||||
// 号码同步仅使用 dayNewFans,不含 reDayNewFans;完成量由 Node statistics 提供
|
||||
$unifiedData->addNumber($number, $isOnline, $dayNewFans);
|
||||
$todayNewSum += $dayNewFans;
|
||||
}
|
||||
|
||||
$unifiedData->todayNewCount = $todayNewSum;
|
||||
$unifiedData->total = count($unifiedData->numbers);
|
||||
|
||||
return $unifiedData;
|
||||
@@ -279,7 +333,7 @@ class WhatshubSpider extends AbstractScrmSpider
|
||||
$unifiedData = $this->unifiedData;
|
||||
|
||||
if (is_array($detailData)) {
|
||||
$unifiedData->todayNewCount = (int) ($detailData['data']['dayNewFans'] ?? 0);
|
||||
$unifiedData->todayNewCount = (int) ($detailData['data']['dayNewFans'] ?? 0) + (int) ($detailData['data']['reDayNewFans'] ?? 0);
|
||||
}
|
||||
|
||||
foreach ($allListPagesData as $pageRaw) {
|
||||
|
||||
Reference in New Issue
Block a user