90 lines
3.1 KiB
PowerShell
90 lines
3.1 KiB
PowerShell
#Requires -Version 5.1
|
||
|
||
[CmdletBinding()]
|
||
param(
|
||
[string] $AccountShort = '朱俊祥支付宝',
|
||
[switch] $SkipLogin
|
||
)
|
||
|
||
Set-StrictMode -Version Latest
|
||
$ErrorActionPreference = 'Stop'
|
||
|
||
. (Join-Path $PSScriptRoot 'account-config.ps1')
|
||
|
||
$existing = Read-AlipayAccountConfig
|
||
$accountPrompt = if ($null -ne $existing) {
|
||
'支付宝账号(手机号/邮箱/登录账号,留空沿用 ' + $existing.account + ')'
|
||
} else {
|
||
'支付宝账号(手机号/邮箱/登录账号)'
|
||
}
|
||
|
||
do {
|
||
$account = Read-Host $accountPrompt
|
||
if ([string]::IsNullOrWhiteSpace($account) -and $null -ne $existing) {
|
||
$account = [string] $existing.account
|
||
}
|
||
if ($account -notmatch '^[A-Za-z0-9@._+-]{6,100}$') {
|
||
Write-Host '账号格式无效,请使用手机号、邮箱或支付宝登录账号。' -ForegroundColor Yellow
|
||
}
|
||
} while ($account -notmatch '^[A-Za-z0-9@._+-]{6,100}$')
|
||
|
||
do {
|
||
$password = Read-Host '支付宝登录密码(输入时不显示)' -AsSecureString
|
||
$confirmPassword = Read-Host '再次输入支付宝登录密码' -AsSecureString
|
||
$passwordText = ConvertFrom-SecureStringForAlipay $password
|
||
$confirmText = ConvertFrom-SecureStringForAlipay $confirmPassword
|
||
try {
|
||
if ([string]::IsNullOrEmpty($passwordText)) {
|
||
Write-Host '密码不能为空,请重新输入。' -ForegroundColor Yellow
|
||
continue
|
||
}
|
||
if ($passwordText -cne $confirmText) {
|
||
Write-Host '两次输入的密码不一致,请重新输入。' -ForegroundColor Yellow
|
||
continue
|
||
}
|
||
break
|
||
} finally {
|
||
$confirmText = $null
|
||
$passwordText = $null
|
||
}
|
||
} while ($true)
|
||
|
||
$defaultShort = if ($null -ne $existing -and -not [string]::IsNullOrWhiteSpace($existing.accountShort)) {
|
||
[string] $existing.accountShort
|
||
} else {
|
||
$AccountShort
|
||
}
|
||
$enteredShort = Read-Host ('财务台账账户简称,直接回车使用“' + $defaultShort + '”')
|
||
if (-not [string]::IsNullOrWhiteSpace($enteredShort)) {
|
||
$defaultShort = $enteredShort.Trim()
|
||
}
|
||
|
||
Save-AlipayEncryptedAccountConfig -Account $account -Password $password -AccountShort $defaultShort
|
||
$configPath = Get-AlipayEncryptedConfigPath
|
||
Write-Host ('账号信息已使用 Windows 当前用户加密保存:' + $configPath) -ForegroundColor Green
|
||
Write-Host ('导入账户简称:' + $defaultShort) -ForegroundColor Green
|
||
|
||
if ($SkipLogin) {
|
||
return
|
||
}
|
||
|
||
$saved = Read-AlipayEncryptedAccountConfig
|
||
$node = Find-AlipayNodeExecutable
|
||
if ([string]::IsNullOrWhiteSpace($node)) {
|
||
throw '未找到 Node.js,无法打开支付宝登录窗口。'
|
||
}
|
||
|
||
Set-AlipayNodeEnvironment $saved
|
||
try {
|
||
Stop-AlipayBrowserSessions
|
||
Write-Host '正在打开支付宝登录窗口并自动检测登录状态,请完成页面上的安全验证…' -ForegroundColor Yellow
|
||
& $node (Join-Path $PSScriptRoot 'open-login.mjs')
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw '支付宝登录未完成或验证失败。'
|
||
}
|
||
} finally {
|
||
Clear-AlipayNodeEnvironment
|
||
}
|
||
|
||
Write-Host '登录验证成功,登录会话已保存。下一步可以安装或运行每日任务。' -ForegroundColor Green
|