From 3f1e998f8e25e7f30b3739a63ad9388fc18290db Mon Sep 17 00:00:00 2001
From: haoliang <821644@qq.com>
Date: Thu, 7 May 2026 11:34:02 +0800
Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE=E6=98=8E?=
=?UTF-8?q?=E6=96=87=E5=8C=96+=E4=BB=AA=E8=A1=A8=E7=9B=98apiKey=E6=95=B0?=
=?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=8C=96+=E6=97=A5=E5=BF=97=E5=88=86?=
=?UTF-8?q?=E7=B1=BB+=E5=AE=89=E8=A3=85=E8=84=9A=E6=9C=AC=E9=98=B2?=
=?UTF-8?q?=E9=97=AA=E9=80=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
frontend/src/views/settings/SettingsPage.vue | 7 +++--
src/CncCollector/log4net.config | 21 ++++++++++++++-
src/CncCollector/scripts/install.ps1 | 26 ++++++++++++++++---
src/CncCollector/scripts/uninstall.ps1 | 12 ++++++---
.../Controllers/DashboardController.cs | 11 ++++++--
.../Infrastructure/ServiceResolver.cs | 8 +++++-
tests/CncWebApi.Tests/ControllerFactory.cs | 2 +-
7 files changed, 71 insertions(+), 16 deletions(-)
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