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;
|
2026-06-20 15:49:40 +08:00
|
|
|
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
|
|
|
*
|
2026-06-29 04:54:41 +08:00
|
|
|
* 时序:detailByShareCode API 直抓(优先)→ 失败则 Real Browser + Turnstile + UI 翻页
|
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-07-04 22:09:39 +08:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* API 优先:detailByShareCode 直抓;失败则回退 Node 爬虫
|
|
|
|
|
*/
|
|
|
|
|
public function run(): UnifiedScrmData
|
|
|
|
|
{
|
|
|
|
|
$apiResult = $this->tryFetchViaShareCodeApi();
|
|
|
|
|
if ($apiResult instanceof UnifiedScrmData) {
|
|
|
|
|
SplitTicketSyncLogger::log('spider', 'whatshub api path ok', [
|
|
|
|
|
'count' => count($apiResult->numbers),
|
|
|
|
|
]);
|
|
|
|
|
return $apiResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SplitTicketSyncLogger::log('spider', 'whatshub api skipped or failed, fallback node', [
|
|
|
|
|
'pageUrl' => $this->pageUrl,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return parent::run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从短链 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();
|
|
|
|
|
$todayNewSum = 0;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
$unifiedData->addNumber($number, $isOnline, $dayNewFans);
|
|
|
|
|
$todayNewSum += $dayNewFans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$unifiedData->todayNewCount = $todayNewSum;
|
|
|
|
|
$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],
|
|
|
|
|
],
|
2026-06-20 15:49:40 +08:00
|
|
|
'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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|