fix: 等待支付宝登录验证后再保存会话 (#1247)

This commit is contained in:
shanshi 2026-11-18 17:48:13 +08:00
parent a3a391236f
commit 7149bdad1c
5 changed files with 84 additions and 28 deletions

View File

@ -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)

View File

@ -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();
}

View File

@ -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

View File

@ -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

View File

@ -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