Files
zyt/identity-admin/scripts/start-kefu-local.ps1
T
2026-11-11 13:53:14 +08:00

22 lines
2.1 KiB
PowerShell

param([string]$Python='C:\kefu\wechat_rpa\.venv\Scripts\python.exe', [string]$MetadataKeyFile='')
$ErrorActionPreference='Stop'
$projectRoot=Split-Path -Parent $PSScriptRoot
$database='C:\kefu\backend.db'
if(-not(Test-Path -LiteralPath $database) -or -not(Test-Path -LiteralPath $Python)){throw '客服数据库或Python环境不存在,未启动或初始化任何账号'}
$count=& $Python -c 'import sqlite3; db=sqlite3.connect("file:C:/kefu/backend.db?mode=ro",uri=True); print(db.execute("SELECT COUNT(*) FROM users").fetchone()[0]); db.close()'
if($LASTEXITCODE -ne 0 -or [int]$count -lt 1){throw '客服现有账号库检查失败,拒绝初始化新库'}
if(Get-NetTCPConnection -State Listen -LocalPort 8766 -ErrorAction SilentlyContinue){throw '8766已有服务,未停止未知进程;请核对配置后再启动'}
$env:PYTHONDONTWRITEBYTECODE='1'
$env:ZYT_GROUP_BASE_URL='http://127.0.0.1:5173'
$env:ZYT_GROUP_API_URL='http://127.0.0.1:8081/adminapi'
$env:KEFU_PUBLIC_BASE_URL='http://127.0.0.1:5666'
$runtimePath=Join-Path $projectRoot '.test-runtime'
$metadataState=Join-Path $runtimePath 'kefu-metadata-local.json'
if(-not $PSBoundParameters.ContainsKey('MetadataKeyFile') -and (Test-Path -LiteralPath $metadataState)){
$MetadataKeyFile=(Get-Content -LiteralPath $metadataState -Raw | ConvertFrom-Json).metadata_key_file
}
$env:ZYT_GROUP_METADATA_KEY_FILE=$MetadataKeyFile
$backend=Start-Process -FilePath $Python -ArgumentList @('admin_api.py','--db',$database,'--host','127.0.0.1','--port','8766','--runtime-file',(Join-Path $runtimePath 'kefu-runtime.json')) -WorkingDirectory 'C:\kefu\wechat_rpa' -WindowStyle Hidden -RedirectStandardOutput (Join-Path $runtimePath 'kefu-local-api.out.log') -RedirectStandardError (Join-Path $runtimePath 'kefu-local-api.err.log') -PassThru
@{backend_pid=$backend.Id;database=$database;frontend_url='http://127.0.0.1:5666';api_url='http://127.0.0.1:8766/api/v2'} | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $runtimePath 'kefu-local-state.json') -Encoding utf8
Write-Output ('客服后端已启动,PID='+$backend.Id+',仅监听本机;使用现有账号库。')