diff --git a/frontend/src/views/settings/SettingsPage.vue b/frontend/src/views/settings/SettingsPage.vue index 55f56f4..ca1064e 100644 --- a/frontend/src/views/settings/SettingsPage.vue +++ b/frontend/src/views/settings/SettingsPage.vue @@ -157,10 +157,9 @@ function getDisplayValue(row: SysConfig): string { return String(row.configValue) } -// 扩展:敏感字段识别规则(包含原有规则,且支持子串匹配) -function isSensitive(key: string): boolean { - const k = (key || '').toLowerCase() - return k === 'api_token' || k === 'collector_api_key' || k.includes('token') || k.includes('key') || k.includes('secret') || k.includes('password') +// 扩展:敏感字段识别规则(系统设置全部明文显示,不脱敏) +function isSensitive(_key: string): boolean { + return false } // ===== Tab1: 系统配置 ===== diff --git a/src/CncCollector/log4net.config b/src/CncCollector/log4net.config index c1b1b5f..8074135 100644 --- a/src/CncCollector/log4net.config +++ b/src/CncCollector/log4net.config @@ -1,12 +1,15 @@ + + + - + @@ -16,9 +19,25 @@ + + + + + + + + + + + + + + + + diff --git a/src/CncCollector/scripts/install.ps1 b/src/CncCollector/scripts/install.ps1 index 741e214..e48b1bf 100644 --- a/src/CncCollector/scripts/install.ps1 +++ b/src/CncCollector/scripts/install.ps1 @@ -10,7 +10,7 @@ param( [string]$Method = "installutil" ) -$ErrorActionPreference = "Stop" +$ErrorActionPreference = "Continue" $serviceName = "CncCollector" $installDir = "C:\CncCollector" $projectDir = Split-Path -Parent $PSScriptRoot @@ -18,7 +18,7 @@ $binDir = Join-Path $projectDir "bin" $exePath = Join-Path $binDir "CncCollector.exe" Write-Host "================================================" -ForegroundColor Cyan -Write-Host " CNC 机床数据采集服务 - 安装脚本 v2.0" -ForegroundColor Cyan +Write-Host " CNC 机床数据采集服务 - 安装脚本 v2.1" -ForegroundColor Cyan Write-Host " 安装方式: $Method" -ForegroundColor Cyan Write-Host "================================================" -ForegroundColor Cyan @@ -26,6 +26,8 @@ Write-Host "================================================" -ForegroundColor C $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "[错误] 请以管理员权限运行此脚本!" -ForegroundColor Red + Write-Host "`n按回车键退出..." -ForegroundColor Gray + Read-Host exit 1 } @@ -34,6 +36,8 @@ if (-not (Test-Path $exePath)) { Write-Host "[错误] 找不到 CncCollector.exe,请先编译项目。" -ForegroundColor Red Write-Host " 预期路径: $exePath" -ForegroundColor Yellow Write-Host " 编译命令: dotnet build src\CncCollector\CncCollector.csproj -c Debug" -ForegroundColor Yellow + Write-Host "`n按回车键退出..." -ForegroundColor Gray + Read-Host exit 1 } @@ -133,6 +137,8 @@ switch ($Method) { } else { Write-Host " [错误] 未找到 NSSM,请先安装 NSSM 或使用其他安装方式。" -ForegroundColor Red Write-Host " 替代方案: .\install.ps1 -Method installutil" -ForegroundColor Yellow + Write-Host "`n按回车键退出..." -ForegroundColor Gray + Read-Host exit 1 } } @@ -165,14 +171,23 @@ if ($svc -and $svc.Status -eq 'Running') { # 尝试调用管理API验证 try { - $response = Invoke-WebRequest -Uri "http://localhost:5800/api/collector/status" -Headers @{ "X-Api-Key" = "collector_api_key_2026" } -TimeoutSec 5 -ErrorAction Stop + # 从collector.json读取apiKey + $configFile = Join-Path $installDir "collector.json" + $apiKey = "collector_api_key_2026" + if (Test-Path $configFile) { + $configJson = Get-Content $configFile -Raw | ConvertFrom-Json + if ($configJson.apiKey) { $apiKey = $configJson.apiKey } + } + $response = Invoke-WebRequest -Uri "http://localhost:5800/api/collector/status" -Headers @{ "X-Api-Key" = $apiKey } -TimeoutSec 5 -ErrorAction Stop Write-Host " API状态: $($response.StatusCode) - 服务运行正常" -ForegroundColor Green } catch { Write-Host " API状态: 未响应(可能正在初始化)" -ForegroundColor Yellow } } else { Write-Host "`n[警告] 服务已安装但未能启动,请检查配置和日志。" -ForegroundColor Yellow - Write-Host " 日志目录: $installDir" -ForegroundColor Gray + Write-Host " 日志目录: $installDir\logs\" -ForegroundColor Gray + Write-Host " 运行日志: collector.log" -ForegroundColor Gray + Write-Host " 错误日志: collector_error.log" -ForegroundColor Gray Write-Host " 查看事件日志: Get-EventLog -LogName Application -Source CncCollector -Newest 10" -ForegroundColor Gray } @@ -182,3 +197,6 @@ Write-Host " 停止: Stop-Service $serviceName" -ForegroundColor Gray Write-Host " 状态: Get-Service $serviceName" -ForegroundColor Gray Write-Host " 卸载: .\uninstall.ps1" -ForegroundColor Gray Write-Host " 控制台调试: CncCollector.exe --console" -ForegroundColor Gray + +Write-Host "`n按回车键退出..." -ForegroundColor Gray +Read-Host diff --git a/src/CncCollector/scripts/uninstall.ps1 b/src/CncCollector/scripts/uninstall.ps1 index b60589a..9ad466a 100644 --- a/src/CncCollector/scripts/uninstall.ps1 +++ b/src/CncCollector/scripts/uninstall.ps1 @@ -2,24 +2,28 @@ # 需要管理员权限运行 # 用法: .\uninstall.ps1 -$ErrorActionPreference = "Stop" +$ErrorActionPreference = "Continue" $serviceName = "CncCollector" $installDir = "C:\CncCollector" Write-Host "================================================" -ForegroundColor Cyan -Write-Host " CNC 机床数据采集服务 - 卸载脚本 v2.0" -ForegroundColor Cyan +Write-Host " CNC 机床数据采集服务 - 卸载脚本 v2.1" -ForegroundColor Cyan Write-Host "================================================" -ForegroundColor Cyan # 检查管理员权限 $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "[错误] 请以管理员权限运行此脚本!" -ForegroundColor Red + Write-Host "`n按回车键退出..." -ForegroundColor Gray + Read-Host exit 1 } $svc = Get-Service -Name $serviceName -ErrorAction SilentlyContinue if (-not $svc) { Write-Host "[提示] 服务未安装,无需卸载。" -ForegroundColor Yellow + Write-Host "`n按回车键退出..." -ForegroundColor Gray + Read-Host exit 0 } @@ -105,5 +109,7 @@ if (-not $check) { } else { Write-Host "[错误] 服务删除失败,可能需要重启后再试。" -ForegroundColor Red Write-Host " 手动删除: sc.exe delete $serviceName" -ForegroundColor Yellow - exit 1 } + +Write-Host "`n按回车键退出..." -ForegroundColor Gray +Read-Host diff --git a/src/CncWebApi/Controllers/DashboardController.cs b/src/CncWebApi/Controllers/DashboardController.cs index 4d27066..d885a3a 100644 --- a/src/CncWebApi/Controllers/DashboardController.cs +++ b/src/CncWebApi/Controllers/DashboardController.cs @@ -5,6 +5,7 @@ using System.Text; using System.Web.Http; using CncModels.Dto; using CncModels.Dto.Dashboard; +using CncRepository.Interface; using CncService.Interface; using CncWebApi.Infrastructure; using Newtonsoft.Json; @@ -19,14 +20,16 @@ namespace CncWebApi.Controllers public class DashboardController : ApiController { private readonly IDashboardService _dashboardService; + private readonly ISysConfigRepository _sysConfigRepository; private static readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; /// /// 构造函数 /// - public DashboardController(IDashboardService dashboardService) + public DashboardController(IDashboardService dashboardService, ISysConfigRepository sysConfigRepository) { _dashboardService = dashboardService; + _sysConfigRepository = sysConfigRepository; } /// @@ -198,8 +201,12 @@ namespace CncWebApi.Controllers { try { + // 从数据库读取采集服务API Key + var apiKeyConfig = _sysConfigRepository.GetByKey("collector_api_key"); + string apiKey = apiKeyConfig?.ConfigValue ?? "collector_api_key_2026"; + var request = new HttpRequestMessage(HttpMethod.Post, $"http://localhost:5800{path}"); - request.Headers.Add("X-Api-Key", "collector_api_key_2026"); + request.Headers.Add("X-Api-Key", apiKey); var response = _httpClient.SendAsync(request).Result; var body = response.Content.ReadAsStringAsync().Result; return Ok(ApiResponse.Success(new { forwarded = true, collectorResponse = JsonConvert.DeserializeObject(body) })); diff --git a/src/CncWebApi/Infrastructure/ServiceResolver.cs b/src/CncWebApi/Infrastructure/ServiceResolver.cs index 1467858..89b0c3f 100644 --- a/src/CncWebApi/Infrastructure/ServiceResolver.cs +++ b/src/CncWebApi/Infrastructure/ServiceResolver.cs @@ -35,7 +35,8 @@ namespace CncWebApi.Infrastructure ResolveAuthService()); if (serviceType == typeof(Controllers.DashboardController)) return new Controllers.DashboardController( - ResolveDashboardService()); + ResolveDashboardService(), + ResolveSysConfigRepository()); if (serviceType == typeof(Controllers.MachineController)) return new Controllers.MachineController( ResolveMachineService()); @@ -203,6 +204,11 @@ namespace CncWebApi.Infrastructure new CncRepository.Impl.Log.CollectCycleRepository(_logConn)); } + private ISysConfigRepository ResolveSysConfigRepository() + { + return new CncRepository.Impl.SysConfigRepository(_businessConn); + } + private IReplayService ResolveReplayService() { return new CncService.Impl.ReplayService(_businessConn, _logConn); diff --git a/tests/CncWebApi.Tests/ControllerFactory.cs b/tests/CncWebApi.Tests/ControllerFactory.cs index 2ce8fc1..d94fa47 100644 --- a/tests/CncWebApi.Tests/ControllerFactory.cs +++ b/tests/CncWebApi.Tests/ControllerFactory.cs @@ -82,7 +82,7 @@ namespace CncWebApi.Tests public static WorkerController CreateWorkerController() => new WorkerController(CreateWorkerService()); /// 创建DashboardController - public static DashboardController CreateDashboardController() => new DashboardController(CreateDashboardService()); + public static DashboardController CreateDashboardController() => new DashboardController(CreateDashboardService(), SysConfigRepo()); /// 创建SettingsController(系统配置+车间管理) public static SettingsController CreateSettingsController() => new SettingsController(SysConfigRepo(), CreateWorkshopService());