修复A2C工单蜘蛛

This commit is contained in:
root
2026-07-01 01:26:27 +08:00
parent 9f2e904fab
commit 54c13c54ef
22 changed files with 1614 additions and 272 deletions
+100 -45
View File
@@ -1,105 +1,160 @@
<?php
// A2c云控
// A2c云控 — 测试脚本(短链/长链 + Real Browser 拦截 API,对齐生产 A2cSpider
require_once __DIR__ . '/AbstractScrmSpider.class.php';
require_once __DIR__ . '/SplitPageUrlResolver.php';
class A2c extends AbstractScrmSpider
{
const API_LIST = '/api/talk/counter/share/record/list'; // 列表API地址
const API_DETAILS = '/api/talk/counter/share/detail'; // 详情API地址
const API_COUNT = ''; // 总数API地址
const DEFAULT_PER_PAGE_COUNT = 20; // List默认每页显示的数量
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 */
private $pageUrl;
/** @var string */
private $account;
/** @var string */
private $password;
/** @var UnifiedScrmData */
private $unifiedData;
// 实例化时动态传入账号和密码
/** @var string HTTP 预解析后的落地 URL */
private $landingUrl = '';
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;
$this->landingUrl = SplitPageUrlResolver::resolveFinalUrl($pageUrl);
$this->unifiedData = new UnifiedScrmData();
}
protected function getSpiderConfig()
{
$antiBot = $this->buildAntiBotConfig();
$this->logDebug('a2c spider config', [
'pageUrl' => $this->pageUrl,
'landingUrl' => $this->landingUrl,
'sessionKey' => $antiBot['sessionKey'] ?? '',
'cfClearanceRequired' => $antiBot['cfClearanceRequired'] ?? null,
]);
return [
'pageUrl' => $this->pageUrl,
'apiUrls' => [self::API_LIST, self::API_DETAILS],
// 明确指派角色
'listApi' => self::API_LIST, // 必须
'detailApi' => self::API_DETAILS, // 选填
'listMethod' => 'POST',
'pageUrl' => $this->pageUrl,
'listApi' => self::API_LIST,
'detailApi' => self::API_DETAILS,
'listMethod' => 'POST',
'paginationMode' => self::MODE_UI,
'authActions' => [
// ['type' => 'type', 'selector' => 'input[type="password"]', 'value' => $this->password],
// ['type' => 'type', 'selector' => '#username_input', 'value' => $this->account],
// ['type' => 'press', 'key' => 'Enter'],
['type' => 'wait', 'ms' => 2000]
]
'authActions' => [
['type' => 'wait', 'ms' => 2000],
],
'antiBot' => $antiBot,
];
}
// 只负责解析 List 的总页数
/**
* 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';
}
protected function extractListTotalPages($listFirstPageData, $countData = null)
{
$default_per_page_count = self::DEFAULT_PER_PAGE_COUNT;
$this->unifiedData->total = $listFirstPageData['data']['total'];
if($listFirstPageData['data']['total'] <= $default_per_page_count) {
if ($listFirstPageData['data']['total'] <= $default_per_page_count) {
return 1;
}
return ceil($listFirstPageData['data']['total']/$default_per_page_count);
return (int) ceil($listFirstPageData['data']['total'] / $default_per_page_count);
}
// 只负责组装 List 的翻页参数
protected function buildListPageParams($page)
{
return ['page' => $page, 'pageSize' => self::DEFAULT_PER_PAGE_COUNT];
}
// 提供 List 的下一页按钮信息
protected function getUiPaginationConfig()
{
return [
'nextBtnSelector' => '.btn-next',
'waitMs' => 2000
'nextBtnSelector' => '.btn-next',
'waitMs' => 2000,
];
}
// 清爽至极的数据清洗:详情是详情,列表是列表
protected function parseToUnifiedData($detailData, $allListPagesData)
{
$unifiedData = $this->unifiedData;
// 1. 如果捕获到了详情数据,提取今日新增
if ($detailData) {
$unifiedData->todayNewCount = (int)($detailData['data']['newFollowersToday'] ?? 0);
$unifiedData->todayNewCount = (int) ($detailData['data']['newFollowersToday'] ?? 0);
}
// 2. 循环合并了所有页数的 List 数组
foreach ($allListPagesData as $pageRaw) {
$records = $pageRaw['data']['rows'] ?? [];
$records = $pageRaw['data']['rows'] ?? [];
foreach ($records as $item) {
if(!empty($item['account'])) {
$number = $item['account'] ?? null;
$isOnline = (isset($item['numberStatus']) && $item['numberStatus'] == 1);
$unifiedData->addNumber($number, $isOnline, $item['newFollowersToday']);
if (!empty($item['account'])) {
$number = $item['account'];
$isOnline = (isset($item['numberStatus']) && (int) $item['numberStatus'] === 1);
$unifiedData->addNumber($number, $isOnline, (int) ($item['newFollowersToday'] ?? 0));
}
}
}
return $unifiedData;
}
}
}
+131
View File
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
/**
* 短链 HTTP 重定向预解析(与 puppeteer-api/url-resolve.js 行为对齐)
*
* 用于 antiBot.sessionKey 按落地业务域生成,工单 URL 仍可为 yyk.ink 等短链。
*/
final class SplitPageUrlResolver
{
private const REDIRECT_CODES = [301, 302, 303, 307, 308];
/**
* 跟随 HTTP 重定向,返回最终 URL;失败时返回原始 URL
*/
public static function resolveFinalUrl(string $pageUrl, int $maxRedirects = 10, int $timeoutSec = 12): string
{
$current = trim($pageUrl);
if ($current === '') {
return '';
}
for ($i = 0; $i < $maxRedirects; $i++) {
$response = self::fetchHeadOrGet($current, $timeoutSec);
if ($response === null) {
return $current;
}
$code = $response['code'];
$location = $response['location'];
if (!in_array($code, self::REDIRECT_CODES, true) || $location === '') {
return $current;
}
$current = self::resolveRelativeUrl($current, $location);
}
return $current;
}
/**
* @return array{code: int, location: string}|null
*/
private static function fetchHeadOrGet(string $url, int $timeoutSec): ?array
{
$result = self::curlOnce($url, true, $timeoutSec);
if ($result !== null) {
return $result;
}
return self::curlOnce($url, false, $timeoutSec);
}
/**
* @return array{code: int, location: string}|null
*/
private static function curlOnce(string $url, bool $headOnly, int $timeoutSec): ?array
{
if (!function_exists('curl_init')) {
return null;
}
$ch = curl_init($url);
if ($ch === false) {
return null;
}
curl_setopt_array($ch, [
CURLOPT_NOBODY => $headOnly,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_TIMEOUT => $timeoutSec,
CURLOPT_HEADER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; SplitPageUrlResolver/1.0)',
]);
$body = curl_exec($ch);
if ($body === false) {
curl_close($ch);
return null;
}
$code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$redirectUrl = (string) curl_getinfo($ch, CURLINFO_REDIRECT_URL);
curl_close($ch);
$location = $redirectUrl;
if ($location === '' && is_string($body)) {
$location = self::parseLocationHeader($body);
}
return ['code' => $code, 'location' => trim($location)];
}
private static function parseLocationHeader(string $rawHeaders): string
{
foreach (preg_split('/\r\n|\n|\r/', $rawHeaders) as $line) {
if (stripos($line, 'Location:') === 0) {
return trim(substr($line, 9));
}
}
return '';
}
private static function resolveRelativeUrl(string $baseUrl, string $location): string
{
if (preg_match('#^https?://#i', $location)) {
return $location;
}
$base = parse_url($baseUrl);
if (!is_array($base)) {
return $location;
}
$scheme = (string) ($base['scheme'] ?? 'https');
$host = (string) ($base['host'] ?? '');
if ($host === '') {
return $location;
}
if (strpos($location, '/') === 0) {
return $scheme . '://' . $host . $location;
}
$path = (string) ($base['path'] ?? '/');
$dir = rtrim(str_replace(basename($path), '', $path), '/');
return $scheme . '://' . $host . ($dir !== '' ? $dir . '/' : '/') . $location;
}
}
+65 -62
View File
@@ -11,52 +11,52 @@ require_once __DIR__ . '/SsCustomer.php'; // SS云控(Customer)
require_once __DIR__ . '/Haiwang.php'; // 海王
require_once __DIR__ . '/Whatshub.php'; // Whatshub 工单平台
try {
/*
// try {
// /*
命令行可以测试Cloudflare防火墙
curl -s -X POST http://127.0.0.1:3001/api/auth-and-intercept \
-H 'Content-Type: application/json' \
-d '{
"pageUrl": "https://web.whatshub.cc/m/KMYBBInp2066/1",
"apiUrls": ["/api/whatshub-counter/workShare/open/detail"],
"authActions": [
{"type": "wait", "ms": 1000},
{"type": "vue_fill", "selector": ".el-input__wrapper > .el-input__inner", "value": "602066"},
{"type": "wait", "ms": 500},
{"type": "vue_click", "selector": ".vxe-button-group .theme--primary"},
{"type": "wait", "ms": 2200}
],
"antiBot": {
"enabled": true,
"profile": "real",
"turnstile": true,
"solverFallback": true,
"sessionKey": "whatshub:web.whatshub.cc",
"challengeTimeoutMs": 60000
}
}' | jq .
*/
// 命令行可以测试Cloudflare防火墙
// curl -s -X POST http://127.0.0.1:3001/api/auth-and-intercept \
// -H 'Content-Type: application/json' \
// -d '{
// "pageUrl": "https://web.whatshub.cc/m/KMYBBInp2066/1",
// "apiUrls": ["/api/whatshub-counter/workShare/open/detail"],
// "authActions": [
// {"type": "wait", "ms": 1000},
// {"type": "vue_fill", "selector": ".el-input__wrapper > .el-input__inner", "value": "602066"},
// {"type": "wait", "ms": 500},
// {"type": "vue_click", "selector": ".vxe-button-group .theme--primary"},
// {"type": "wait", "ms": 2200}
// ],
// "antiBot": {
// "enabled": true,
// "profile": "real",
// "turnstile": true,
// "solverFallback": true,
// "sessionKey": "whatshub:web.whatshub.cc",
// "challengeTimeoutMs": 60000
// }
// }' | jq .
// */
echo "🚀 开始执行<Whatshub 工单平台>抓取任务 (多引擎智能调度)...\n\r";
$pageUrl = 'https://web.whatshub.cc/m/KMYBBInp2066/1'; // PageUrl 入口授权页
$username = ""; // 登录账号
$password = "602066"; // 登录密码
$spider = new Whatshub($pageUrl, $username, $password);
$finalData = $spider->run();
// echo "🚀 开始执行<Whatshub 工单平台>抓取任务 (多引擎智能调度)...\n\r";
// $pageUrl = 'https://web.whatshub.cc/m/KMYBBInp2066/1'; // PageUrl 入口授权页
// $username = ""; // 登录账号
// $password = "602066"; // 登录密码
// $spider = new Whatshub($pageUrl, $username, $password);
// $finalData = $spider->run();
echo "✅ 任务完成!统一数据如下:\n\r";
echo "----------------------------------------\n\r";
echo "当日新增:{$finalData->todayNewCount}\n\r";
echo "在线号码:{$finalData->totalOnline}\n\r";
echo "离线号码:{$finalData->totalOffline}\n\r";
echo "Total" . $finalData->total . " 个号码\n\r";
echo "实际总共抓取:" . count($finalData->numbers) . " 个号码\n\r";
echo "号码列表:\n\r";
echo dd($finalData->numbers);
} catch (Exception $e) {
echo "🚨 抓取异常:" . $e->getMessage() . "\n\r";
}
// echo "✅ 任务完成!统一数据如下:\n\r";
// echo "----------------------------------------\n\r";
// echo "当日新增:{$finalData->todayNewCount} 人\n\r";
// echo "在线号码:{$finalData->totalOnline} 个\n\r";
// echo "离线号码:{$finalData->totalOffline} 个\n\r";
// echo "Total" . $finalData->total . " 个号码\n\r";
// echo "实际总共抓取:" . count($finalData->numbers) . " 个号码\n\r";
// echo "号码列表:\n\r";
// echo dd($finalData->numbers);
// } catch (Exception $e) {
// echo "🚨 抓取异常:" . $e->getMessage() . "\n\r";
// }
// try {
@@ -80,27 +80,30 @@ try {
// echo "🚨 抓取异常:" . $e->getMessage() . "\n\r";
// }
// try {
// echo "🚀 开始执行<A2c云控>抓取任务 (多引擎智能调度)...\n\r";
// $pageUrl = 'https://yyk.ink/52oXZRG'; // PageUrl 入口授权页
// $username = ""; // 登录账号
// $password = ""; // 登录密码
// $spider = new A2c($pageUrl, $username, $password);
// $finalData = $spider->run();
try {
echo "🚀 开始执行<A2c云控>抓取任务 (多引擎智能调度)...\n\r";
// 长链:快速验证业务页拦截(推荐先测通)
// $pageUrl = 'https://user.a2c.chat/visitors/counter/share?id=1b2cf9a91e2647c185b252e723c871de';
// 短链:与客户后台一致,上线前必须回归
$pageUrl = 'https://yyk.ink/91SEWrL';
$username = "";
$password = "";
$spider = (new A2c($pageUrl, $username, $password))->setVerbose(true);
$finalData = $spider->run();
// echo "✅ 任务完成!统一数据如下:\n\r";
// echo "----------------------------------------\n\r";
// echo "当日新增:{$finalData->todayNewCount} 人\n\r";
// echo "在线号码:{$finalData->totalOnline} 个\n\r";
// echo "离线号码:{$finalData->totalOffline} 个\n\r";
// echo "Total" . $finalData->total . " 个号码\n\r";
// echo "实际总共抓取:" . count($finalData->numbers) . " 个号码\n\r";
// echo "号码列表:\n\r";
// echo dd($finalData->numbers);
echo "✅ 任务完成!统一数据如下:\n\r";
echo "----------------------------------------\n\r";
echo "当日新增:{$finalData->todayNewCount}\n\r";
echo "在线号码:{$finalData->totalOnline}\n\r";
echo "离线号码:{$finalData->totalOffline}\n\r";
echo "Total" . $finalData->total . " 个号码\n\r";
echo "实际总共抓取:" . count($finalData->numbers) . " 个号码\n\r";
echo "号码列表:\n\r";
echo dd($finalData->numbers);
// } catch (Exception $e) {
// echo "🚨 抓取异常:" . $e->getMessage() . "\n\r";
// }
} catch (Exception $e) {
echo "🚨 抓取异常:" . $e->getMessage() . "\n\r";
}
// try {
// echo "🚀 开始执行<星河云控>抓取任务 (多引擎智能调度)...\n\r";