From 7149bdad1cc2d2b30f46a94af3d2ad782dc5c61d Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 18 Nov 2026 17:48:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AD=89=E5=BE=85=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=9D=E7=99=BB=E5=BD=95=E9=AA=8C=E8=AF=81=E5=90=8E=E5=86=8D?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=BC=9A=E8=AF=9D=20(#1247)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../company-alipay-direct/account-config.ps1 | 12 +++ scripts/company-alipay-direct/open-login.mjs | 81 ++++++++++++++----- scripts/company-alipay-direct/open-login.ps1 | 9 ++- .../run-alipay-daily.ps1 | 1 + .../company-alipay-direct/setup-account.ps1 | 9 +-- 5 files changed, 84 insertions(+), 28 deletions(-) diff --git a/scripts/company-alipay-direct/account-config.ps1 b/scripts/company-alipay-direct/account-config.ps1 index bd01c4c2..588e80ee 100644 --- a/scripts/company-alipay-direct/account-config.ps1 +++ b/scripts/company-alipay-direct/account-config.ps1 @@ -134,6 +134,18 @@ function Find-AlipayNodeExecutable { return $candidates | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } | Select-Object -First 1 } +function Stop-AlipayBrowserSessions { + $profile = Join-Path (Get-AlipayRepositoryRoot) 'runtime\private\alipay-profile' + $processes = Get-CimInstance Win32_Process -Filter "Name = 'msedge.exe'" | + Where-Object { $_.CommandLine -like ('*' + $profile + '*') } + foreach ($process in $processes) { + Stop-Process -Id $process.ProcessId -Force -ErrorAction SilentlyContinue + } + if (@($processes).Count -gt 0) { + Start-Sleep -Seconds 1 + } +} + function Set-AlipayNodeEnvironment { param([Parameter(Mandatory = $true)] $Config) diff --git a/scripts/company-alipay-direct/open-login.mjs b/scripts/company-alipay-direct/open-login.mjs index f836a80e..308b296a 100644 --- a/scripts/company-alipay-direct/open-login.mjs +++ b/scripts/company-alipay-direct/open-login.mjs @@ -8,6 +8,8 @@ const PROFILE = path.resolve(BASE, '../../runtime/private/alipay-profile'); const SECRET_FILE = process.env.ALIPAY_SECRET_FILE || path.resolve(BASE, '../../runtime/private/敏感信息.md'); const PORT = 9335; +const LOGIN_URL = 'https://auth.alipay.com/login/index.htm?goto=https%3A%2F%2Fconsumeprod.alipay.com%2Frecord%2Fadvanced.htm'; +const TARGET_URL = 'https://consumeprod.alipay.com/record/advanced.htm'; let text = ''; let account = process.env.ALIPAY_ACCOUNT?.trim() || ''; @@ -23,31 +25,36 @@ const { child, version } = await launchEdge({ port: PORT, profileDir: PROFILE, headless: false, - detached: true, + detached: false, }); -child.unref(); const cdp = await CDP.connect(version.webSocketDebuggerUrl); const { sessionId } = await openPage(cdp, 'about:blank'); -await goto(cdp, sessionId, 'https://auth.alipay.com/login/index.htm?goto=https%3A%2F%2Fconsumeprod.alipay.com%2Frecord%2Fadvanced.htm'); +await goto(cdp, sessionId, LOGIN_URL); await sleep(1800); -let loginInputs = await evaluate(cdp, sessionId, `({ - user: !!document.querySelector('#J-input-user'), - password: !!document.querySelector('input[type="password"]') -})`); -if (!loginInputs.user || !loginInputs.password) { - await evaluate(cdp, sessionId, `(() => { - const el = [...document.querySelectorAll('*')].find((node) => (node.innerText || '').trim() === '账密登录'); - el && el.click(); - return true; - })()`); - await sleep(800); - loginInputs = await evaluate(cdp, sessionId, `({ +async function pageInfo() { + return evaluate(cdp, sessionId, `({ + url: location.href, + title: document.title, + text: document.body?.innerText || '', user: !!document.querySelector('#J-input-user'), password: !!document.querySelector('input[type="password"]') })`); } -if (loginInputs.user && loginInputs.password) { + +async function fillCredentials() { + let inputs = await pageInfo(); + if (!inputs.user || !inputs.password) { + await evaluate(cdp, sessionId, `(() => { + const el = [...document.querySelectorAll('*')].find((node) => (node.innerText || '').trim() === '账密登录'); + el && el.click(); + return true; + })()`); + await sleep(800); + inputs = await pageInfo(); + } + if (!inputs.user || !inputs.password) return false; + await evaluate(cdp, sessionId, `(() => { const setValue = (el, value) => { const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set; @@ -58,10 +65,46 @@ if (loginInputs.user && loginInputs.password) { document.querySelector('#J-input-user') && setValue(document.querySelector('#J-input-user'), ${JSON.stringify(account)}); const password = document.querySelector('input[type="password"]'); password && setValue(password, ${JSON.stringify(password)}); + const button = [...document.querySelectorAll('button')].find((node) => (node.innerText || '').replace(/\\s+/g, '') === '登录'); + button && button.click(); return true; })()`); + return true; } -console.log('支付宝登录窗口已打开。账号已预填,请完成图形验证码或短信验证;登录成功后关闭窗口即可。'); -cdp.close(); -process.exit(0); +let verified = false; +try { + console.log('支付宝登录窗口已打开。脚本会自动填写账号密码,请完成扫码、短信、图形验证码或其他安全确认。'); + await fillCredentials(); + + for (let attempt = 0; attempt < 600; attempt++) { + const state = await pageInfo(); + if (/密码错误|账号或密码错误/.test(state.text)) { + throw new Error('支付宝账号或密码错误,请重新运行 setup-account.ps1 修改账号密码'); + } + + if ((!state.user && !state.password && !/auth\.alipay\.com\/login/.test(state.url)) + || (/我的账单/.test(state.title) && !state.user && !state.password)) { + await goto(cdp, sessionId, TARGET_URL); + await sleep(2200); + const target = await pageInfo(); + if (!target.user && !target.password && !/auth\.alipay\.com\/login/.test(target.url)) { + verified = true; + console.log('支付宝登录已验证:已成功进入账单页面,登录会话已保存。'); + break; + } + } + await sleep(1000); + } + + if (!verified) { + throw new Error('等待支付宝登录验证超时,请重新运行重新登录入口并完成安全验证'); + } +} finally { + try { + await cdp.send('Browser.close'); + } catch {} + cdp.close(); + await sleep(1200); + if (!child.killed) child.kill(); +} diff --git a/scripts/company-alipay-direct/open-login.ps1 b/scripts/company-alipay-direct/open-login.ps1 index f5e9e2f6..fca823c3 100644 --- a/scripts/company-alipay-direct/open-login.ps1 +++ b/scripts/company-alipay-direct/open-login.ps1 @@ -19,13 +19,14 @@ if ([string]::IsNullOrWhiteSpace($node)) { Set-AlipayNodeEnvironment $config try { + Stop-AlipayBrowserSessions + Write-Host '正在打开支付宝登录窗口并自动检测登录状态,请完成页面上的安全验证…' -ForegroundColor Yellow & $node (Join-Path $PSScriptRoot 'open-login.mjs') if ($LASTEXITCODE -ne 0) { - throw '支付宝登录窗口启动失败。' + throw '支付宝登录未完成或验证失败。' } - Write-Host '' - Write-Host '请完成图形验证码、短信或扫码确认。' -ForegroundColor Yellow - Read-Host '确认已经登录成功并看到支付宝页面后,回到这里按 Enter' | Out-Null } finally { Clear-AlipayNodeEnvironment } + +Write-Host '支付宝登录验证成功,登录会话已保存。' -ForegroundColor Green diff --git a/scripts/company-alipay-direct/run-alipay-daily.ps1 b/scripts/company-alipay-direct/run-alipay-daily.ps1 index f0849785..38d5df90 100644 --- a/scripts/company-alipay-direct/run-alipay-daily.ps1 +++ b/scripts/company-alipay-direct/run-alipay-daily.ps1 @@ -52,6 +52,7 @@ if (-not [string]::IsNullOrWhiteSpace($InputFile)) { Write-Log ('开始导出支付宝账单:' + $Date) Set-AlipayNodeEnvironment $config try { + Stop-AlipayBrowserSessions $exportOutput = (& $node $exportScript ('--date=' + $Date) ('--out=' + $outputDir) 2>&1 | Out-String) } finally { Clear-AlipayNodeEnvironment diff --git a/scripts/company-alipay-direct/setup-account.ps1 b/scripts/company-alipay-direct/setup-account.ps1 index 893ce646..d4e6edcf 100644 --- a/scripts/company-alipay-direct/setup-account.ps1 +++ b/scripts/company-alipay-direct/setup-account.ps1 @@ -76,15 +76,14 @@ if ([string]::IsNullOrWhiteSpace($node)) { Set-AlipayNodeEnvironment $saved try { + Stop-AlipayBrowserSessions + Write-Host '正在打开支付宝登录窗口并自动检测登录状态,请完成页面上的安全验证…' -ForegroundColor Yellow & $node (Join-Path $PSScriptRoot 'open-login.mjs') if ($LASTEXITCODE -ne 0) { - throw '支付宝登录窗口启动失败。' + throw '支付宝登录未完成或验证失败。' } - Write-Host '' - Write-Host '请在弹出的 Edge 窗口中完成图形验证码、短信或扫码确认。' -ForegroundColor Yellow - Read-Host '确认已经登录成功并看到支付宝页面后,回到这里按 Enter' | Out-Null } finally { Clear-AlipayNodeEnvironment } -Write-Host '登录会话已保存。下一步可以安装或运行每日任务。' -ForegroundColor Green +Write-Host '登录验证成功,登录会话已保存。下一步可以安装或运行每日任务。' -ForegroundColor Green