packServer/desktop/Services/OrderQueryService.cs
liufumingren 680fff84df desktop:新增 WPF 本机软件(本机 HTTP 服务 + yuncang:// 启动协议)
给前端 `/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 良构校验。
2026-10-17 10:13:09 +08:00

49 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace YuncangTrace.LocalService.Services;
/// <summary>一次查询的结果(HTTP 层和界面都复用)</summary>
/// <param name="Ok">是否查到</param>
/// <param name="StatusCode">HTTP 状态码(200 / 400 / 404 / 500)</param>
/// <param name="Message">失败原因</param>
/// <param name="Data">成功时的数据(**平铺**一个对象,前端按「项目/值」两列直接渲染)</param>
public sealed record QueryResult(bool Ok, int StatusCode, string Message, object? Data)
{
public static QueryResult Success(object data) => new(true, 200, "ok", data);
public static QueryResult BadRequest(string message) => new(false, 400, message, null);
public static QueryResult NotFound(string message) => new(false, 404, message, null);
public static QueryResult Failed(string message) => new(false, 500, message, null);
}
/// <summary>
/// 按运单号查信息
///
/// ⚠️ **这里是接你们真实数据的地方**:现在返回演示数据(任何 code 都返回同一份)。
/// 真实实现通常是:查本地 SQLite / 扫描本地打包视频目录 / 调内部接口,
/// 然后把结果按字段平铺返回(前端页面按 key-value 展示,字段随你加)。
/// </summary>
public sealed class OrderQueryService
{
public QueryResult Query(string? code)
{
var value = (code ?? string.Empty).Trim();
if (value.Length == 0) return QueryResult.BadRequest("缺少运单号");
// ↓↓↓ 换成真实查询逻辑 ↓↓↓
var data = new Dictionary<string, object?>
{
["code"] = value,
["orderNo"] = "SO-20261017-001",
["channel"] = "CH-1",
["printStatus"] = "已打印",
["scanTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
["videoPath"] = @"D:\video\2026-10-17\demo.mp4",
["source"] = "演示数据(请替换 OrderQueryService.Query)",
};
// ↑↑↑ 换成真实查询逻辑 ↑↑↑
return QueryResult.Success(data);
}
}