145 lines
4.9 KiB
PowerShell
145 lines
4.9 KiB
PowerShell
#Requires -Version 5.1
|
||
|
||
[CmdletBinding()]
|
||
param(
|
||
[string] $Workbook = 'D:\zyc\网商银行账号密码.xlsx',
|
||
[string] $Output,
|
||
[string] $Python
|
||
)
|
||
|
||
Set-StrictMode -Version Latest
|
||
$ErrorActionPreference = 'Stop'
|
||
|
||
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
|
||
if ([string]::IsNullOrWhiteSpace($Output)) {
|
||
$Output = Join-Path $root 'runtime\private\wangshang-bank-accounts.json'
|
||
}
|
||
$workbookPath = (Resolve-Path -LiteralPath $Workbook -ErrorAction Stop).Path
|
||
$outputPath = [IO.Path]::GetFullPath($Output)
|
||
$outputDirectory = [IO.Path]::GetDirectoryName($outputPath)
|
||
[IO.Directory]::CreateDirectory($outputDirectory) | Out-Null
|
||
|
||
if ([string]::IsNullOrWhiteSpace($Python)) {
|
||
$candidates = @(
|
||
$env:CODEX_PYTHON,
|
||
(Join-Path $env:USERPROFILE '.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe'),
|
||
(Get-Command python.exe -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source)
|
||
) | Where-Object {
|
||
-not [string]::IsNullOrWhiteSpace($_) -and (Test-Path -LiteralPath $_ -PathType Leaf)
|
||
}
|
||
$Python = $candidates | Where-Object { $_ -notmatch 'WindowsApps' } | Select-Object -First 1
|
||
}
|
||
if ([string]::IsNullOrWhiteSpace($Python) -or -not (Test-Path -LiteralPath $Python -PathType Leaf)) {
|
||
throw '未找到可用的 Python;请通过 -Python 指定包含 openpyxl 的 python.exe'
|
||
}
|
||
|
||
$ignored = & git -C $root check-ignore --quiet $outputPath
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw '网商银行账号配置目录未写入项目忽略范围,拒绝保存凭据'
|
||
}
|
||
|
||
$env:WANGSHANG_BANK_WORKBOOK = $workbookPath
|
||
$env:WANGSHANG_BANK_OUTPUT = $outputPath
|
||
$env:PYTHONUTF8 = '1'
|
||
try {
|
||
@'
|
||
import json
|
||
import os
|
||
import re
|
||
from pathlib import Path
|
||
|
||
from openpyxl import load_workbook
|
||
|
||
workbook = Path(os.environ["WANGSHANG_BANK_WORKBOOK"])
|
||
output = Path(os.environ["WANGSHANG_BANK_OUTPUT"])
|
||
sheet = load_workbook(workbook, read_only=True, data_only=True)["Sheet1"]
|
||
headers = [str(cell.value or "").strip() for cell in sheet[1]]
|
||
|
||
aliases = {
|
||
"company": ["公司名称"],
|
||
"alipay_account": ["支付宝账号"],
|
||
"alipay_password": ["支付宝密码"],
|
||
"bank_account": ["网商银行账号"],
|
||
}
|
||
indexes = {}
|
||
for key, names in aliases.items():
|
||
for name in names:
|
||
if name in headers:
|
||
indexes[key] = headers.index(name)
|
||
break
|
||
if key not in indexes:
|
||
raise RuntimeError(f"账号表缺少字段:{names[0]}")
|
||
|
||
def account_short(company):
|
||
value = re.sub(r"[\s ]+", "", company)
|
||
value = re.sub(
|
||
r"^(?:北京市?|上海市?|天津市?|重庆市?|杭州市?|深圳市?|青岛市?|广州市?|"
|
||
r"株洲市?|义乌市?|南召县|永康市?|常州市?)",
|
||
"",
|
||
value,
|
||
)
|
||
pattern = re.compile(
|
||
r"(?:电子商务咨询服务|电子商务|网络科技|科技|商贸|贸易|家居|家具|"
|
||
r"咨询服务|实业|供应链管理|有限公司|有限责任公司)$"
|
||
)
|
||
while True:
|
||
shortened = pattern.sub("", value)
|
||
if shortened == value:
|
||
break
|
||
value = shortened
|
||
return value + "网商银行"
|
||
|
||
accounts = []
|
||
for row in sheet.iter_rows(min_row=2, values_only=True):
|
||
def value(key):
|
||
index = indexes[key]
|
||
return "" if index >= len(row) or row[index] is None else str(row[index]).strip()
|
||
|
||
company = value("company")
|
||
alipay_account = value("alipay_account")
|
||
alipay_password = value("alipay_password")
|
||
bank_account = re.sub(r"\D", "", value("bank_account"))
|
||
if not any([company, alipay_account, alipay_password, bank_account]):
|
||
continue
|
||
if not company or not alipay_account or not alipay_password or len(bank_account) < 6:
|
||
raise RuntimeError(f"第 {len(accounts) + 2} 行账号资料不完整")
|
||
accounts.append({
|
||
"company": company,
|
||
"alipay_account": alipay_account,
|
||
"alipay_password": alipay_password,
|
||
"bank_account": bank_account,
|
||
"account_short": account_short(company),
|
||
})
|
||
|
||
if not accounts:
|
||
raise RuntimeError("账号表没有可导入的数据")
|
||
|
||
temporary = output.with_suffix(output.suffix + ".tmp")
|
||
temporary.write_text(
|
||
json.dumps({"version": 1, "accounts": accounts}, ensure_ascii=False, indent=2),
|
||
encoding="utf-8",
|
||
)
|
||
os.replace(temporary, output)
|
||
print(f"accounts={len(accounts)} output={output}")
|
||
'@ | & $Python -
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw '读取网商银行账号表失败'
|
||
}
|
||
} finally {
|
||
Remove-Item Env:WANGSHANG_BANK_WORKBOOK -ErrorAction SilentlyContinue
|
||
Remove-Item Env:WANGSHANG_BANK_OUTPUT -ErrorAction SilentlyContinue
|
||
}
|
||
|
||
$acl = Get-Acl -LiteralPath $outputPath
|
||
$acl.SetAccessRuleProtection($true, $false)
|
||
$identity = [Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||
$rule = New-Object Security.AccessControl.FileSystemAccessRule(
|
||
$identity,
|
||
'FullControl',
|
||
'Allow'
|
||
)
|
||
$acl.AddAccessRule($rule)
|
||
Set-Acl -LiteralPath $outputPath -AclObject $acl
|
||
|
||
Write-Host ('已生成网商银行私有配置:' + $outputPath)
|