From 680fff84dfc7e9b64dd8ea14c5bfbf9146e69b01 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 17 Oct 2026 10:13:09 +0800 Subject: [PATCH] =?UTF-8?q?desktop=EF=BC=9A=E6=96=B0=E5=A2=9E=20WPF=20?= =?UTF-8?q?=E6=9C=AC=E6=9C=BA=E8=BD=AF=E4=BB=B6=EF=BC=88=E6=9C=AC=E6=9C=BA?= =?UTF-8?q?=20HTTP=20=E6=9C=8D=E5=8A=A1=20+=20yuncang://=20=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=8D=8F=E8=AE=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 给前端 `/local` 公共页与 Chrome 插件对接的本机软件(net8.0-windows + UseWPF): - Services/LocalHttpServer.cs:HttpListener 监听 localhost:17311(同时注册 127.0.0.1 前缀) - GET /api/health 探活(2xx 即认为已启动) - GET /api/query/{code} 按运单号查信息,成功时字段平铺返回 - CORS 全量响应头 + Access-Control-Allow-Private-Network(公网 https 页面访问 localhost 的 PNA 预检),预检 OPTIONS 直接 204 - 根路径给一个说明页,方便浏览器里手工排查 - Services/OrderQueryService.cs:★ 接真实数据源的位置(现为演示数据) - Services/ProtocolHelper.cs:HKCU\Software\Classes\yuncang 注册/反注册、解析 yuncang://open?code=xxx(免管理员) - Services/SingleInstance.cs:互斥体 + 命名管道,被协议再次拉起时把参数转给已运行窗口, 不会去抢 17311 端口 - MainWindow:运行状态、服务地址(复制/浏览器打开)、注册启动协议、测试查询、运行日志 - App.xaml.cs:单实例判断 + 协议参数处理;启动即自动起服务 README 里写了编译/发布命令、接口契约、协议注册(含 reg 命令与打包工具做法)、 接真实数据的示例,以及端口权限(netsh http add urlalloc)/端口占用等常见问题。 ⚠️ 本机没装 .NET SDK,无法在这里编译验证;XAML/manifest/csproj 已做 XML 良构校验。 --- .gitignore | 9 + desktop/App.xaml | 13 ++ desktop/App.xaml.cs | 64 ++++++ desktop/MainWindow.xaml | 92 ++++++++ desktop/MainWindow.xaml.cs | 227 ++++++++++++++++++++ desktop/README.md | 144 +++++++++++++ desktop/Services/JsonUtil.cs | 26 +++ desktop/Services/LocalHttpServer.cs | 255 +++++++++++++++++++++++ desktop/Services/OrderQueryService.cs | 48 +++++ desktop/Services/ProtocolHelper.cs | 98 +++++++++ desktop/Services/SingleInstance.cs | 117 +++++++++++ desktop/YuncangTrace.LocalService.csproj | 24 +++ desktop/app.manifest | 20 ++ 13 files changed, 1137 insertions(+) create mode 100644 desktop/App.xaml create mode 100644 desktop/App.xaml.cs create mode 100644 desktop/MainWindow.xaml create mode 100644 desktop/MainWindow.xaml.cs create mode 100644 desktop/README.md create mode 100644 desktop/Services/JsonUtil.cs create mode 100644 desktop/Services/LocalHttpServer.cs create mode 100644 desktop/Services/OrderQueryService.cs create mode 100644 desktop/Services/ProtocolHelper.cs create mode 100644 desktop/Services/SingleInstance.cs create mode 100644 desktop/YuncangTrace.LocalService.csproj create mode 100644 desktop/app.manifest diff --git a/.gitignore b/.gitignore index 234fc04..1f76942 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,15 @@ coverage/ .vite/ *.tsbuildinfo +# ---------- .NET / WPF(desktop/) ---------- +bin/ +obj/ +publish/ +artifacts/ +.vs/ +*.user +*.suo + # ---------- 运行期 / 日志 ---------- # backend 的 logs/、多实例自测时的 logs-test/、脚本产出的 run/ logs/ diff --git a/desktop/App.xaml b/desktop/App.xaml new file mode 100644 index 0000000..6f476eb --- /dev/null +++ b/desktop/App.xaml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/desktop/App.xaml.cs b/desktop/App.xaml.cs new file mode 100644 index 0000000..e47265c --- /dev/null +++ b/desktop/App.xaml.cs @@ -0,0 +1,64 @@ +using System.Windows; +using YuncangTrace.LocalService.Services; + +namespace YuncangTrace.LocalService; + +/// +/// 应用入口 +/// +/// 两件事:**单实例**(协议被点开时系统会再启动一次本程序,要把参数转给已运行的实例) +/// 和**把窗口拉起来**。 +/// +public partial class App : Application +{ + private SingleInstance? _single; + private MainWindow? _window; + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + _single = new SingleInstance("YuncangTrace.LocalService", OnSecondInstanceArgs); + + // 已经有一个实例在跑:把自己的参数(可能是 yuncang://open?code=xxx)转过去,然后退出 + if (!_single.IsFirstInstance) + { + _single.SendArgs(e.Args); + Shutdown(); + return; + } + + _window = new MainWindow(); + MainWindow = _window; + _window.Show(); + + // 起命名管道监听(接收后续「再次启动」带来的参数) + _single.StartListening(); + + // 本次就是被协议拉起来的:直接进入「按 code 查信息」 + var launchUrl = ProtocolHelper.FirstLaunchUrl(e.Args); + if (launchUrl is not null) _window.HandleLaunchUrl(launchUrl); + } + + /// 另一个实例把参数送过来了(工作线程回调,切回 UI 线程) + private void OnSecondInstanceArgs(string[] args) + { + Dispatcher.Invoke(() => + { + if (MainWindow is MainWindow window) + { + if (window.WindowState == WindowState.Minimized) window.WindowState = WindowState.Normal; + window.Activate(); + + var url = ProtocolHelper.FirstLaunchUrl(args); + if (url is not null) window.HandleLaunchUrl(url); + } + }); + } + + protected override void OnExit(ExitEventArgs e) + { + _single?.Dispose(); + base.OnExit(e); + } +} diff --git a/desktop/MainWindow.xaml b/desktop/MainWindow.xaml new file mode 100644 index 0000000..ec3291f --- /dev/null +++ b/desktop/MainWindow.xaml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +