给前端 `/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 良构校验。
99 lines
4.0 KiB
C#
99 lines
4.0 KiB
C#
using Microsoft.Win32;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 自定义协议(URL Scheme)——「网页叫醒本机应用」的唯一合法方式
|
||
///
|
||
/// 网页里写 <c>yuncang://open?code=123</c>,浏览器问一句「要打开云仓视频溯源软件吗?」,
|
||
/// 系统就会按注册表 <c>HKCU\Software\Classes\yuncang\shell\open\command</c> 找到本 exe 并拉起,
|
||
/// 形如:<c>"C:\...\YuncangTrace.LocalService.exe" "yuncang://open?code=123"</c>
|
||
///
|
||
/// 注意:
|
||
/// - 网页**不能**直接执行 exe,也不能拿到返回值(所以还要配合 127.0.0.1 上的 HTTP 服务);
|
||
/// - 协议注册在 HKCU,**不需要管理员权限**;
|
||
/// - 卸载时应该调用 <see cref="Unregister"/> 把注册表清掉。
|
||
/// </summary>
|
||
public static class ProtocolHelper
|
||
{
|
||
/// <summary>协议名(前端用 VITE_APP_PROTOCOL 对齐;改了这里要一起改)</summary>
|
||
public const string Scheme = "yuncang";
|
||
|
||
/// <summary>资源管理器 / 浏览器的提示名</summary>
|
||
public const string DisplayName = "云仓视频溯源软件";
|
||
|
||
/// <summary>拼出唤醒地址:<c>yuncang://open?code=xxx</c></summary>
|
||
public static string LaunchUrl(string? code)
|
||
{
|
||
return string.IsNullOrWhiteSpace(code)
|
||
? $"{Scheme}://open"
|
||
: $"{Scheme}://open?code={Uri.EscapeDataString(code)}";
|
||
}
|
||
|
||
/// <summary>从命令行参数里找出协议地址(被协议拉起时 Windows 会把它作为第一个参数)</summary>
|
||
public static string? FirstLaunchUrl(IEnumerable<string> args)
|
||
{
|
||
return args.FirstOrDefault(arg =>
|
||
arg.StartsWith($"{Scheme}://", StringComparison.OrdinalIgnoreCase));
|
||
}
|
||
|
||
/// <summary>从 <c>yuncang://open?code=xxx</c> 里取出 code(没有就返回 null)</summary>
|
||
public static string? ParseCode(string? url)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(url)) return null;
|
||
|
||
var index = url.IndexOf('?');
|
||
if (index < 0 || index == url.Length - 1) return null;
|
||
|
||
foreach (var pair in url[(index + 1)..].Split('&', StringSplitOptions.RemoveEmptyEntries))
|
||
{
|
||
var parts = pair.Split('=', 2);
|
||
if (parts.Length == 2 && parts[0].Equals("code", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var value = Uri.UnescapeDataString(parts[1].Replace('+', ' ')).Trim();
|
||
return value.Length == 0 ? null : value;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>协议是否已注册</summary>
|
||
public static bool IsRegistered()
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{Scheme}\shell\open\command");
|
||
return key?.GetValue(null) is string value && value.Length > 0;
|
||
}
|
||
|
||
/// <summary>当前注册的命令行(界面上展示,方便排查指向了哪个 exe)</summary>
|
||
public static string? RegisteredCommand()
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{Scheme}\shell\open\command");
|
||
return key?.GetValue(null) as string;
|
||
}
|
||
|
||
/// <summary>注册协议(指向当前 exe;HKCU,免管理员)</summary>
|
||
public static void Register()
|
||
{
|
||
var exePath = Environment.ProcessPath
|
||
?? throw new InvalidOperationException("拿不到当前程序的路径,无法注册启动协议");
|
||
|
||
using (var scheme = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{Scheme}"))
|
||
{
|
||
scheme.SetValue(null, $"URL:{DisplayName}");
|
||
scheme.SetValue("URL Protocol", string.Empty);
|
||
}
|
||
|
||
using (var command = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{Scheme}\shell\open\command"))
|
||
{
|
||
command.SetValue(null, $"\"{exePath}\" \"%1\"");
|
||
}
|
||
}
|
||
|
||
/// <summary>取消注册(卸载时调用)</summary>
|
||
public static void Unregister()
|
||
{
|
||
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{Scheme}", throwOnMissingSubKey: false);
|
||
}
|
||
}
|