Files
links/patches/application/common/library/scrm/spider/WhatshubSpider.php
T

362 lines
12 KiB
PHP
Raw Normal View History

2026-06-20 04:47:34 +08:00
<?php
declare(strict_types=1);
namespace app\common\library\scrm\spider;
use app\common\library\scrm\AbstractScrmSpider;
use app\common\library\scrm\AntiBotConfigBuilder;
2026-06-20 04:47:34 +08:00
use app\common\library\scrm\UnifiedScrmData;
2026-06-29 04:54:41 +08:00
use app\common\service\SplitTicketSyncLogger;
2026-06-20 04:47:34 +08:00
/**
2026-06-29 04:54:41 +08:00
* Whatshub 云控蜘蛛
2026-06-20 04:47:34 +08:00
*
* 时序:cURL detailByShareCode 拉号码 → Node 监听 statistics 算完成量 → 失败则完整 Node 爬虫
2026-06-20 04:47:34 +08:00
*/
class WhatshubSpider extends AbstractScrmSpider
{
/** 列表 API 路径 */
private const API_LIST = '/api/whatshub-counter/workShare/open/detail';
/** 详情/统计 API 路径 */
private const API_DETAILS = '/api/whatshub-counter/workShare/open/statistics';
2026-06-29 04:54:41 +08:00
/** shareCode + password 直连接口(一次返回全量号码,无需翻页) */
private const API_DETAIL_BY_SHARE_CODE = '/api/whatshub-counter/workShare/open/detailByShareCode';
2026-06-20 04:47:34 +08:00
/** 默认每页条数 */
private const DEFAULT_PER_PAGE_COUNT = 20;
2026-06-29 04:54:41 +08:00
private const API_FETCH_TIMEOUT_SEC = 20;
2026-06-20 04:47:34 +08:00
private string $pageUrl;
private string $account;
private string $password;
private UnifiedScrmData $unifiedData;
public function __construct(
string $pageUrl,
string $account = '',
string $password = '',
string $nodeHost = 'http://127.0.0.1:3001'
) {
parent::__construct($nodeHost);
$this->pageUrl = $pageUrl;
$this->account = $account;
$this->password = $password;
$this->unifiedData = new UnifiedScrmData();
}
2026-06-29 04:54:41 +08:00
/**
* 混合同步:cURL 拉号码 + Node 监听 statistics 算完成量;任一失败则完整 Node fallback
2026-06-29 04:54:41 +08:00
*/
public function run(): UnifiedScrmData
{
$apiResult = $this->tryFetchViaShareCodeApi();
$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,
2026-06-29 04:54:41 +08:00
]);
return $apiResult;
}
SplitTicketSyncLogger::log('spider', 'whatshub hybrid incomplete, fallback full node', [
'pageUrl' => $this->pageUrl,
'apiOk' => $apiResult instanceof UnifiedScrmData,
'statisticsOk' => $statsCount !== null,
2026-06-29 04:54:41 +08:00
]);
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;
}
2026-06-29 04:54:41 +08:00
/**
* 从短链 pageUrl 解析 shareCode,例如 /m/KMYBBInp2066/1 → KMYBBInp2066
*/
private function extractShareCodeFromPageUrl(): ?string
{
$path = (string) parse_url($this->pageUrl, PHP_URL_PATH);
if ($path === '') {
return null;
}
if (preg_match('#/m/([^/]+)/#', $path, $matches)) {
return $matches[1];
}
return null;
}
/**
* 拼接 detailByShareCode 完整 GET URL
*/
private function buildShareCodeApiUrl(string $shareCode): ?string
{
$parsed = parse_url($this->pageUrl);
$scheme = isset($parsed['scheme']) ? (string) $parsed['scheme'] : 'https';
$host = isset($parsed['host']) ? (string) $parsed['host'] : '';
if ($host === '') {
return null;
}
$query = http_build_query([
'shareCode' => $shareCode,
'password' => $this->password,
]);
return $scheme . '://' . $host . self::API_DETAIL_BY_SHARE_CODE . '?' . $query;
}
/**
* 尝试通过 shareCode API 抓取;成功返回 UnifiedScrmData,否则返回 null
*/
private function tryFetchViaShareCodeApi(): ?UnifiedScrmData
{
if ($this->password === '') {
SplitTicketSyncLogger::log('spider', 'whatshub api skip: empty password', []);
return null;
}
$shareCode = $this->extractShareCodeFromPageUrl();
if ($shareCode === null || $shareCode === '') {
SplitTicketSyncLogger::log('spider', 'whatshub api skip: cannot parse shareCode', [
'pageUrl' => $this->pageUrl,
]);
return null;
}
$apiUrl = $this->buildShareCodeApiUrl($shareCode);
if ($apiUrl === null) {
SplitTicketSyncLogger::log('spider', 'whatshub api skip: invalid pageUrl host', [
'pageUrl' => $this->pageUrl,
]);
return null;
}
SplitTicketSyncLogger::log('spider', 'whatshub api request', [
'url' => preg_replace('/password=[^&]+/', 'password=***', $apiUrl),
]);
$ch = curl_init($apiUrl);
if ($ch === false) {
SplitTicketSyncLogger::log('spider', 'whatshub api curl init failed', []);
return null;
}
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => self::API_FETCH_TIMEOUT_SEC,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
],
]);
$response = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErr = curl_error($ch);
curl_close($ch);
if ($response === false || $curlErr !== '') {
SplitTicketSyncLogger::log('spider', 'whatshub api curl failed', [
'error' => $curlErr,
]);
return null;
}
if ($httpCode !== 200) {
SplitTicketSyncLogger::log('spider', 'whatshub api http error', [
'httpCode' => $httpCode,
]);
return null;
}
$payload = json_decode((string) $response, true);
if (!is_array($payload)) {
SplitTicketSyncLogger::log('spider', 'whatshub api invalid json', []);
return null;
}
if ((int) ($payload['code'] ?? 0) !== 200) {
SplitTicketSyncLogger::log('spider', 'whatshub api business error', [
'code' => $payload['code'] ?? null,
'msg' => $payload['msg'] ?? '',
]);
return null;
}
if (!isset($payload['data']) || !is_array($payload['data'])) {
SplitTicketSyncLogger::log('spider', 'whatshub api invalid data field', []);
return null;
}
return $this->parseShareCodeApiResponse($payload);
}
/**
* 将 detailByShareCode 响应映射为 UnifiedScrmData
*
* @param array<string, mixed> $payload
*/
private function parseShareCodeApiResponse(array $payload): UnifiedScrmData
{
$unifiedData = new UnifiedScrmData();
foreach ($payload['data'] as $item) {
if (!is_array($item) || empty($item['account'])) {
continue;
}
$number = (string) $item['account'];
$isOnline = isset($item['isOnline']) && (int) $item['isOnline'] === 1;
$dayNewFans = (int) ($item['dayNewFans'] ?? 0);
// 号码同步仅使用 dayNewFans,不含 reDayNewFans;完成量由 Node statistics 提供
2026-06-29 04:54:41 +08:00
$unifiedData->addNumber($number, $isOnline, $dayNewFans);
}
$unifiedData->total = count($unifiedData->numbers);
return $unifiedData;
}
2026-06-20 04:47:34 +08:00
/** @return array<string, mixed> */
protected function getSpiderConfig(): array
{
return [
'pageUrl' => $this->pageUrl,
'listApi' => self::API_LIST,
'detailApi' => self::API_DETAILS,
'listMethod' => 'POST',
'paginationMode' => self::MODE_UI,
'authActions' => [
// Element Plus 密码弹窗:仅注入可见 input
['type' => 'vue_fill', 'selector' => '.el-input__inner', 'value' => $this->password],
['type' => 'wait', 'ms' => 500],
['type' => 'vue_click', 'selector' => '.vxe-button-group .theme--primary'],
['type' => 'wait', 'ms' => 2200],
],
'antiBot' => AntiBotConfigBuilder::build('whatshub', $this->pageUrl),
2026-06-20 04:47:34 +08:00
];
}
/**
* @param array<string, mixed>|null $listFirstPageData
* @param array<string, mixed>|null $countData
*/
protected function extractListTotalPages($listFirstPageData, $countData = null)
{
$defaultPerPage = self::DEFAULT_PER_PAGE_COUNT;
$this->unifiedData->total = (int) ($listFirstPageData['data']['total'] ?? 0);
if ($this->unifiedData->total <= $defaultPerPage) {
return 1;
}
return (int) ceil($this->unifiedData->total / $defaultPerPage);
}
/** @return array<string, mixed> */
protected function buildListPageParams(int $page): array
{
return ['pageNum' => $page, 'pageSize' => self::DEFAULT_PER_PAGE_COUNT];
}
/** @return array<string, mixed> */
protected function getUiPaginationConfig(): array
{
return [
'nextBtnSelector' => '.vxe-pager--next-btn',
'waitMs' => 1500,
];
}
protected function parseToUnifiedData($detailData, array $allListPagesData): UnifiedScrmData
{
$unifiedData = $this->unifiedData;
if (is_array($detailData)) {
$unifiedData->todayNewCount = (int) ($detailData['data']['dayNewFans'] ?? 0) + (int) ($detailData['data']['reDayNewFans'] ?? 0);
2026-06-20 04:47:34 +08:00
}
foreach ($allListPagesData as $pageRaw) {
if (!is_array($pageRaw)) {
continue;
}
$records = $pageRaw['data']['rows'] ?? [];
if (!is_array($records)) {
continue;
}
foreach ($records as $item) {
if (!is_array($item) || empty($item['account'])) {
continue;
}
$number = (string) $item['account'];
$isOnline = isset($item['isOnline']) && (int) $item['isOnline'] === 1;
$unifiedData->addNumber($number, $isOnline, (int) ($item['dayNewFans'] ?? 0));
}
}
$unifiedData->total = count($unifiedData->numbers);
return $unifiedData;
}
}