guoyu/Test/备份/log_备份/ps1/内层_修改服务器地址.ps1

133 lines
4.4 KiB
PowerShell

#!/usr/bin/env pwsh
# ============================================
# 修改服务器地址并重新打包
# ============================================
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 修改服务器地址" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$ProjectRoot = $PSScriptRoot
$ConfigFile = "$ProjectRoot\src\utils\config.js"
# 读取当前配置
if (-Not (Test-Path $ConfigFile)) {
Write-Host "错误:找不到配置文件" -ForegroundColor Red
Write-Host " 期望位置: $ConfigFile" -ForegroundColor Gray
exit 1
}
$ConfigContent = Get-Content $ConfigFile -Raw
# 提取当前 IP
if ($ConfigContent -match "DEFAULT_SERVER_HOST\s*=\s*'([^']+)'") {
$CurrentIP = $Matches[1]
} else {
$CurrentIP = "未找到"
}
if ($ConfigContent -match "DEFAULT_SERVER_PORT\s*=\s*(\d+)") {
$CurrentPort = $Matches[1]
} else {
$CurrentPort = "未找到"
}
Write-Host "当前配置:" -ForegroundColor Yellow
Write-Host " 服务器地址: $CurrentIP" -ForegroundColor White
Write-Host " 服务器端口: $CurrentPort" -ForegroundColor White
Write-Host ""
# 询问新的 IP
Write-Host "请输入新的服务器地址(直接回车保持不变):" -ForegroundColor Cyan
$NewIP = Read-Host " IP 地址"
if ([string]::IsNullOrWhiteSpace($NewIP)) {
$NewIP = $CurrentIP
Write-Host " 保持原地址: $NewIP" -ForegroundColor Gray
}
# 询问新的端口
Write-Host ""
Write-Host "请输入新的服务器端口(直接回车保持不变):" -ForegroundColor Cyan
$NewPortInput = Read-Host " 端口"
if ([string]::IsNullOrWhiteSpace($NewPortInput)) {
$NewPort = $CurrentPort
Write-Host " 保持原端口: $NewPort" -ForegroundColor Gray
} else {
$NewPort = $NewPortInput
}
# 确认修改
Write-Host ""
Write-Host "========================================" -ForegroundColor Yellow
Write-Host " 即将修改为:" -ForegroundColor Yellow
Write-Host " 服务器地址: $NewIP" -ForegroundColor Green
Write-Host " 服务器端口: $NewPort" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Yellow
Write-Host ""
$Confirm = Read-Host "确认修改并重新打包? (Y/n)"
if ($Confirm -eq 'n' -or $Confirm -eq 'N') {
Write-Host "已取消" -ForegroundColor Yellow
exit 0
}
# 执行修改
Write-Host ""
Write-Host "正在修改配置文件..." -ForegroundColor Yellow
try {
# 修改 DEFAULT_SERVER_HOST
$ConfigContent = $ConfigContent -replace "const DEFAULT_SERVER_HOST = '[^']*'", "const DEFAULT_SERVER_HOST = '$NewIP'"
# 修改 DEFAULT_SERVER_PORT
$ConfigContent = $ConfigContent -replace "const DEFAULT_SERVER_PORT = \d+", "const DEFAULT_SERVER_PORT = $NewPort"
# 修改 DEV_SERVER_HOST
$ConfigContent = $ConfigContent -replace "const DEV_SERVER_HOST = '[^']*'", "const DEV_SERVER_HOST = '$NewIP'"
# 修改 DEV_SERVER_PORT
$ConfigContent = $ConfigContent -replace "const DEV_SERVER_PORT = \d+", "const DEV_SERVER_PORT = $NewPort"
# 修改 window.location.hostname 判断
$ConfigContent = $ConfigContent -replace "hostname === '[^']*'", "hostname === '$NewIP'"
# 修改 getBaseURL 的默认返回值
$ConfigContent = $ConfigContent -replace "return 'http://[^:]*:\d+'", "return 'http://${NewIP}:${NewPort}'"
# 保存文件
Set-Content -Path $ConfigFile -Value $ConfigContent -Encoding UTF8
Write-Host " ✓ 配置文件已更新" -ForegroundColor Green
} catch {
Write-Host " ✗ 修改失败: $_" -ForegroundColor Red
exit 1
}
# 询问是否立即重新打包
Write-Host ""
$RebuildNow = Read-Host "是否立即重新打包 APK? (Y/n)"
if ($RebuildNow -ne 'n' -and $RebuildNow -ne 'N') {
Write-Host ""
Write-Host "开始重新打包..." -ForegroundColor Yellow
$RebuildScript = "$ProjectRoot\一键重新打包.ps1"
if (Test-Path $RebuildScript) {
& $RebuildScript
} else {
Write-Host "警告:找不到一键打包脚本" -ForegroundColor Yellow
Write-Host "请手动运行: npm run build:app" -ForegroundColor Gray
}
} else {
Write-Host ""
Write-Host "配置已更新,但未重新打包" -ForegroundColor Yellow
Write-Host "稍后运行 '一键重新打包.ps1' 即可" -ForegroundColor Gray
}
Write-Host ""
Write-Host "按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")