packServer/desktop/Services/ProtocolHelper.cs
liufumingren 51abccbaaf desktop:加登录页、托盘图标、防多开(单实例 + 参数转发)
- LoginWindow:服务器地址 / 账号 / 密码 / 记住账号 → POST /api/web/auth/login 拿 user JWT;
  本地已有令牌时先用 GET /api/web/me 验证,有效就跳过登录(窗口一闪而过),无效清掉令牌留下重登。
- TrayIconService(WinForms NotifyIcon,csproj 开 UseWindowsForms):
  关窗口 = 隐藏到托盘(本机服务继续跑),双击托盘恢复,右键菜单「打开主界面 / 服务地址 / 版本 / 退出」;
  第一次缩到托盘冒一次气泡提示。退出只走托盘「退出」(App 改 ShutdownMode=OnExplicitShutdown)。
- 防多开:SingleInstance(Local\ 命名互斥体 + 命名管道)。第二个实例不抢 17311 端口,
  把 yuncang://open?code=… 转给已运行实例(在托盘里也会被弹出来并按 code 查询)后自己退出;
  还在登录界面时先把 URL 存起来,登录完成后再查。
- 生命周期调整:本机 HTTP 服务与托盘在 App 里创建(登录前就可用),主窗口只管展示与操作;
  日志改成全局 AppLog(窗口没开时的日志也会在窗口打开后补上)。
- 新增 AppSettings / SessionStore:%LOCALAPPDATA%\YuncangTrace\settings.json、session.json
  (服务器地址默认 http://127.0.0.1:7300;换服务器不会复用旧令牌)。
- 新增 ApiClient(登录 / 校验令牌)、MainWindow「退出登录」(清令牌回登录页)。
- 程序/托盘图标:Assets/app.ico + tools/make-icon.ps1(品牌蓝 + 白色播放三角)。
- csproj 关闭 ImplicitUsings:WPF 与 WinForms 同时开启时隐式命名空间会让
  MessageBox / Application / Clipboard 等产生歧义。

⚠️ 本机没有 .NET SDK,无法编译验证;已做 XAML/XML 良构与括号平衡检查。
2026-10-17 10:23:50 +08:00

101 lines
4.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.

using System;
using System.Linq;
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);
}
}