Files
ALL-teach_sys/start-industry.ps1

257 lines
8.3 KiB
PowerShell
Raw Permalink Normal View History

# 教务系统 - PowerShell智能启动脚本
# 使用方法: PowerShell中运行 .\start-industry.ps1
# 设置执行策略(如果需要)
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 产业配置
$industries = @(
@{Number=1; Dir="frontend"; Name="文旅产业"; Port=5150},
@{Number=2; Dir="frontend_智能制造"; Name="智能制造"; Port=5151},
@{Number=3; Dir="frontend_智能开发"; Name="智能开发"; Port=5152},
@{Number=4; Dir="frontend_财经商贸"; Name="财经商贸"; Port=5153},
@{Number=5; Dir="frontend_视觉设计"; Name="视觉设计"; Port=5154},
@{Number=6; Dir="frontend_交通物流"; Name="交通物流"; Port=5155},
@{Number=7; Dir="frontend_大健康"; Name="大健康"; Port=5156},
@{Number=8; Dir="frontend_土木水利"; Name="土木水利"; Port=5157},
@{Number=9; Dir="frontend_食品"; Name="食品产业"; Port=5158},
@{Number=10; Dir="frontend_化工"; Name="化工产业"; Port=5159},
@{Number=11; Dir="frontend_能源"; Name="能源产业"; Port=5160},
@{Number=12; Dir="frontend_环保"; Name="环保产业"; Port=5161}
)
# 颜色输出函数
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
# 检查端口是否被占用
function Test-Port {
param([int]$Port)
$connection = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
return $null -ne $connection
}
# 停止占用端口的进程
function Stop-PortProcess {
param([int]$Port)
$connection = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
if ($connection) {
$processId = $connection.OwningProcess
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
Write-ColorOutput "已停止端口 $Port 的进程" "Yellow"
Start-Sleep -Seconds 1
}
}
# 启动单个产业
function Start-Industry {
param(
[hashtable]$Industry
)
Write-Host ""
Write-ColorOutput ("=" * 50) "Cyan"
Write-ColorOutput "启动 $($Industry.Name) (端口: $($Industry.Port))" "Cyan"
Write-ColorOutput ("=" * 50) "Cyan"
# 检查目录
if (-not (Test-Path $Industry.Dir)) {
Write-ColorOutput "[错误] 目录 $($Industry.Dir) 不存在!" "Red"
return $false
}
# 检查端口
if (Test-Port -Port $Industry.Port) {
Write-ColorOutput "[警告] 端口 $($Industry.Port) 已被占用" "Yellow"
$choice = Read-Host "是否停止现有服务并重启?(y/n)"
if ($choice -eq 'y' -or $choice -eq 'Y') {
Stop-PortProcess -Port $Industry.Port
} else {
Write-ColorOutput "跳过 $($Industry.Name)" "Yellow"
return $false
}
}
# 进入目录
Push-Location $Industry.Dir
try {
# 检查并安装依赖
if (-not (Test-Path "node_modules")) {
Write-ColorOutput "[信息] 检测到依赖未安装,正在安装..." "Yellow"
Write-ColorOutput " 这可能需要1-3分钟请稍候..." "Gray"
$process = Start-Process npm -ArgumentList "install" -PassThru -NoNewWindow -Wait
if ($process.ExitCode -eq 0) {
Write-ColorOutput "[成功] $($Industry.Name) 依赖安装成功!" "Green"
} else {
Write-ColorOutput "[错误] $($Industry.Name) 依赖安装失败!" "Red"
Pop-Location
return $false
}
} else {
Write-ColorOutput "[信息] $($Industry.Name) 依赖已存在" "Green"
}
# 启动服务
Write-ColorOutput "[信息] 正在启动 $($Industry.Name)..." "Cyan"
Start-Process powershell -ArgumentList "-Command", "npm run dev" -WindowStyle Minimized
# 等待服务启动
Start-Sleep -Seconds 3
# 验证启动
if (Test-Port -Port $Industry.Port) {
Write-ColorOutput "[成功] $($Industry.Name) 启动成功!" "Green"
Write-ColorOutput " 访问地址: http://localhost:$($Industry.Port)" "White"
return $true
} else {
Write-ColorOutput "[错误] $($Industry.Name) 启动失败!" "Red"
return $false
}
}
finally {
Pop-Location
}
}
# 启动所有产业
function Start-AllIndustries {
Write-ColorOutput "`n启动所有产业..." "Cyan"
$successCount = 0
$failCount = 0
foreach ($industry in $industries) {
if (Start-Industry -Industry $industry) {
$successCount++
} else {
$failCount++
}
}
Write-Host ""
Write-ColorOutput ("=" * 50) "Cyan"
Write-ColorOutput "成功启动: $successCount 个产业" "Green"
if ($failCount -gt 0) {
Write-ColorOutput "启动失败: $failCount 个产业" "Red"
}
}
# 停止所有产业
function Stop-AllIndustries {
Write-ColorOutput "`n停止所有产业服务..." "Yellow"
# 停止所有node进程
Get-Process node -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Write-ColorOutput "所有服务已停止" "Green"
}
# 查看运行状态
function Show-Status {
Clear-Host
Write-ColorOutput ("=" * 50) "Cyan"
Write-ColorOutput " 产业运行状态检查" "Cyan"
Write-ColorOutput ("=" * 50) "Cyan"
Write-Host ""
foreach ($industry in $industries) {
if (Test-Port -Port $industry.Port) {
Write-ColorOutput "[运行中] $($industry.Name.PadRight(10)) (端口: $($industry.Port)) - http://localhost:$($industry.Port)" "Green"
} else {
Write-ColorOutput "[未启动] $($industry.Name.PadRight(10)) (端口: $($industry.Port))" "Red"
}
}
}
# 显示菜单
function Show-Menu {
Clear-Host
Write-ColorOutput ("=" * 50) "Cyan"
Write-ColorOutput " 教务系统 - 智能启动脚本 (PowerShell)" "Cyan"
Write-ColorOutput ("=" * 50) "Cyan"
Write-Host ""
Write-ColorOutput "请选择操作:" "White"
Write-Host ""
Write-ColorOutput " 0) 启动所有产业" "White"
Write-ColorOutput " --------------" "Gray"
foreach ($industry in $industries) {
$status = if (Test-Port -Port $industry.Port) { "[运行中]" } else { "[未启动]" }
$color = if (Test-Port -Port $industry.Port) { "Green" } else { "Yellow" }
Write-Host (" {0,2}) {1,-10} (端口: {2}) " -f $industry.Number, $industry.Name, $industry.Port) -NoNewline
Write-Host $status -ForegroundColor $color
}
Write-ColorOutput " --------------" "Gray"
Write-ColorOutput " a) 停止所有产业" "White"
Write-ColorOutput " s) 查看运行状态" "White"
Write-ColorOutput " q) 退出" "White"
Write-Host ""
}
# 主循环
function Main {
# 检查是否有命令行参数
if ($args.Count -gt 0) {
switch ($args[0]) {
"all" { Start-AllIndustries }
"stop" { Stop-AllIndustries }
"status" { Show-Status }
default {
if ([int]::TryParse($args[0], [ref]$null)) {
$num = [int]$args[0]
if ($num -ge 1 -and $num -le $industries.Count) {
Start-Industry -Industry $industries[$num - 1]
}
}
}
}
return
}
# 交互模式
while ($true) {
Show-Menu
$choice = Read-Host "请输入选项"
switch ($choice) {
"0" {
Start-AllIndustries
Read-Host "`n按回车键继续..."
}
{$_ -match "^[1-9]$|^1[0-2]$"} {
$num = [int]$choice
Start-Industry -Industry $industries[$num - 1]
Read-Host "`n按回车键继续..."
}
{$_ -eq "a" -or $_ -eq "A"} {
Stop-AllIndustries
Read-Host "`n按回车键继续..."
}
{$_ -eq "s" -or $_ -eq "S"} {
Show-Status
Read-Host "`n按回车键继续..."
}
{$_ -eq "q" -or $_ -eq "Q"} {
Write-ColorOutput "再见!" "Cyan"
return
}
default {
Write-ColorOutput "无效选项!" "Red"
Start-Sleep -Seconds 1
}
}
}
}
# 启动主程序
Main