Files
links/_php_code/A2c.php
T

161 lines
5.0 KiB
PHP
Raw Normal View History

2026-06-09 03:36:30 +08:00
<?php
2026-07-01 01:26:27 +08:00
// A2c云控 — 测试脚本(短链/长链 + Real Browser 拦截 API,对齐生产 A2cSpider
2026-06-09 03:36:30 +08:00
require_once __DIR__ . '/AbstractScrmSpider.class.php';
2026-07-01 01:26:27 +08:00
require_once __DIR__ . '/SplitPageUrlResolver.php';
2026-06-09 03:36:30 +08:00
class A2c extends AbstractScrmSpider
{
2026-07-01 01:26:27 +08:00
const API_LIST = '/api/talk/counter/share/record/list';
const API_DETAILS = '/api/talk/counter/share/detail';
const API_COUNT = '';
const DEFAULT_PER_PAGE_COUNT = 20;
/** @var string */
2026-06-09 03:36:30 +08:00
private $pageUrl;
2026-07-01 01:26:27 +08:00
/** @var string */
2026-06-09 03:36:30 +08:00
private $account;
2026-07-01 01:26:27 +08:00
/** @var string */
2026-06-09 03:36:30 +08:00
private $password;
2026-07-01 01:26:27 +08:00
/** @var UnifiedScrmData */
2026-06-09 03:36:30 +08:00
private $unifiedData;
2026-07-01 01:26:27 +08:00
/** @var string HTTP 预解析后的落地 URL */
private $landingUrl = '';
2026-06-09 03:36:30 +08:00
public function __construct($pageUrl, $account, $password, $nodeHost = 'http://127.0.0.1:3001')
{
parent::__construct($nodeHost);
$this->account = $account;
$this->password = $password;
$this->pageUrl = $pageUrl;
2026-07-01 01:26:27 +08:00
$this->landingUrl = SplitPageUrlResolver::resolveFinalUrl($pageUrl);
2026-06-09 03:36:30 +08:00
$this->unifiedData = new UnifiedScrmData();
}
protected function getSpiderConfig()
{
2026-07-01 01:26:27 +08:00
$antiBot = $this->buildAntiBotConfig();
$this->logDebug('a2c spider config', [
'pageUrl' => $this->pageUrl,
'landingUrl' => $this->landingUrl,
'sessionKey' => $antiBot['sessionKey'] ?? '',
'cfClearanceRequired' => $antiBot['cfClearanceRequired'] ?? null,
]);
2026-06-09 03:36:30 +08:00
return [
2026-07-01 01:26:27 +08:00
'pageUrl' => $this->pageUrl,
'listApi' => self::API_LIST,
'detailApi' => self::API_DETAILS,
'listMethod' => 'POST',
2026-06-09 03:36:30 +08:00
'paginationMode' => self::MODE_UI,
2026-07-01 01:26:27 +08:00
'authActions' => [
['type' => 'wait', 'ms' => 2000],
],
'antiBot' => $antiBot,
2026-06-09 03:36:30 +08:00
];
}
2026-07-01 01:26:27 +08:00
/**
* A2C:业务域无需 cf_clearance;短链仍作 pageUrlsessionKey 固定业务域
*
* @return array<string, mixed>
*/
private function buildAntiBotConfig()
{
$sessionHost = $this->resolveA2cSessionHost();
return [
'enabled' => true,
'profile' => 'real',
'turnstile' => true,
'solverFallback' => false,
'cfClearanceRequired' => false,
'sessionKey' => 'a2c:' . $sessionHost,
'challengeTimeoutMs' => 60000,
'landingUrlHint' => $this->landingUrl,
'businessHostHint' => $sessionHost,
'postCfReadyUrlContains' => '/visitors/counter/share',
'postCfReadySelector' => '.btn-next',
'postCfReadyTimeoutMs' => 30000,
'postCfSettleMs' => 500,
'postCfSpaWaitMs' => 4000,
];
}
/**
* A2C 会话域:HTTP 预解析无法穿透短链 CF 时,回退到业务域 user.a2c.chat
*/
private function resolveA2cSessionHost()
{
$candidates = [];
if ($this->landingUrl !== '') {
$candidates[] = (string) parse_url($this->landingUrl, PHP_URL_HOST);
}
$candidates[] = (string) parse_url($this->pageUrl, PHP_URL_HOST);
foreach ($candidates as $host) {
$host = strtolower(trim($host));
if ($host !== '' && strpos($host, 'a2c.chat') !== false) {
return $host;
}
}
return 'user.a2c.chat';
}
2026-06-09 03:36:30 +08:00
protected function extractListTotalPages($listFirstPageData, $countData = null)
{
$default_per_page_count = self::DEFAULT_PER_PAGE_COUNT;
$this->unifiedData->total = $listFirstPageData['data']['total'];
2026-07-01 01:26:27 +08:00
if ($listFirstPageData['data']['total'] <= $default_per_page_count) {
2026-06-09 03:36:30 +08:00
return 1;
}
2026-07-01 01:26:27 +08:00
return (int) ceil($listFirstPageData['data']['total'] / $default_per_page_count);
2026-06-09 03:36:30 +08:00
}
protected function buildListPageParams($page)
{
return ['page' => $page, 'pageSize' => self::DEFAULT_PER_PAGE_COUNT];
}
protected function getUiPaginationConfig()
{
return [
2026-07-01 01:26:27 +08:00
'nextBtnSelector' => '.btn-next',
'waitMs' => 2000,
2026-06-09 03:36:30 +08:00
];
}
protected function parseToUnifiedData($detailData, $allListPagesData)
{
$unifiedData = $this->unifiedData;
if ($detailData) {
2026-07-01 01:26:27 +08:00
$unifiedData->todayNewCount = (int) ($detailData['data']['newFollowersToday'] ?? 0);
2026-06-09 03:36:30 +08:00
}
foreach ($allListPagesData as $pageRaw) {
2026-07-01 01:26:27 +08:00
$records = $pageRaw['data']['rows'] ?? [];
2026-06-09 03:36:30 +08:00
foreach ($records as $item) {
2026-07-01 01:26:27 +08:00
if (!empty($item['account'])) {
$number = $item['account'];
$isOnline = (isset($item['numberStatus']) && (int) $item['numberStatus'] === 1);
$unifiedData->addNumber($number, $isOnline, (int) ($item['newFollowersToday'] ?? 0));
2026-06-09 03:36:30 +08:00
}
}
}
return $unifiedData;
}
2026-07-01 01:26:27 +08:00
}