需求:服务跟随软件运行,不需要单独的启停;唤醒地址由插件生成,界面不必提供复制。 - MainWindow.xaml:删除「停止服务/启动服务」与「复制唤醒地址」按钮; 第③行只保留协议状态文字;标题栏的圆点 + 「运行中 · 地址」继续显示服务状态。 - MainWindow.xaml.cs: - 删除 StartStop_Click(不再允许手动停服务:停了网页看到的现象和"未安装"一模一样); - 删除 CopyLaunchUrl_Click; - UpdateServerState 只刷新状态点/文字;移除 ServiceStateChanged 事件; - 顺手修复「在浏览器打开」指向了已删除的 /api/health → 改为根路径说明页。 - TrayIconService:菜单新增「重启软件」(打开主界面 / 重启软件 / 服务地址 / 版本 / 退出)。 - App:订阅 RestartRequested → RestartApplication(): 用 `cmd /c timeout /t 2 & start "" "<exe>"` 延迟拉起自己,再走正常退出流程 (必须先退出,否则新进程会撞单实例检测、把参数转发给正在退出的旧进程后就没了)。 - README:行为表补「托盘右键重启软件」,并说明主界面不提供启停/复制唤醒地址的原因。 验证:打包 1.0.5 安装后,主界面控件只剩「退出登录 / 复制地址 / 在浏览器打开 / 查询」等, 无启停与复制唤醒地址;协议状态文字正常;托盘菜单代码含「重启软件」。
210 lines
6.7 KiB
C#
210 lines
6.7 KiB
C#
using System;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.Windows;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using YuncangTrace.LocalService.Services;
|
||
|
||
namespace YuncangTrace.LocalService;
|
||
|
||
/// <summary>
|
||
/// 主窗口:本机服务状态、启动协议、测试查询、日志、当前登录用户
|
||
///
|
||
/// 关窗口**不是退出**:只是隐藏到托盘(本机服务得继续跑)。
|
||
/// 真正退出:托盘菜单「退出」,或点「退出登录」后再取消登录。
|
||
/// </summary>
|
||
public partial class MainWindow : Window
|
||
{
|
||
private readonly ObservableCollection<string> _logs = new();
|
||
private readonly LocalHttpServer _server;
|
||
private readonly OrderQueryService _queryService = new();
|
||
|
||
/// <summary>允许真正关闭窗口(退出登录 / 退出程序时由 App 置 true)</summary>
|
||
public bool AllowClose { get; set; }
|
||
|
||
/// <summary>用户点了「退出登录」</summary>
|
||
public event Action? LogoutRequested;
|
||
|
||
/// <summary>窗口被隐藏到托盘(用来弹一次气泡提示)</summary>
|
||
public event Action? HiddenToTray;
|
||
|
||
public MainWindow(LocalHttpServer server, AppSettings settings, Session session)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_server = server;
|
||
|
||
UserText.Text = $"已登录:{session.DisplayName}";
|
||
UserText.ToolTip = $"服务器:{settings.ServerBaseUrl}";
|
||
AddressBox.Text = _server.BaseUrl;
|
||
|
||
// 日志:先灌历史(服务在登录前就起来了),再订阅新的
|
||
foreach (var line in AppLog.History) _logs.Add(line);
|
||
|
||
LogList.ItemsSource = _logs;
|
||
AppLog.Written += OnLogWritten;
|
||
|
||
Loaded += (_, _) =>
|
||
{
|
||
UpdateServerState();
|
||
UpdateProtocolState();
|
||
};
|
||
Closing += OnClosing;
|
||
Closed += (_, _) => AppLog.Written -= OnLogWritten;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 被协议拉起 / 另一个实例把参数转过来时调用:<c>yuncang://open?code=xxx</c>
|
||
/// </summary>
|
||
public void HandleLaunchUrl(string url)
|
||
{
|
||
AppLog.Write($"收到启动协议:{url}");
|
||
|
||
var code = ProtocolHelper.ParseCode(url);
|
||
if (code is null)
|
||
{
|
||
AppLog.Write("协议里没带 code,忽略");
|
||
return;
|
||
}
|
||
|
||
QueryBox.Text = code;
|
||
RunQuery();
|
||
}
|
||
|
||
// ---------------- 服务状态 ----------------
|
||
|
||
/// <summary>
|
||
/// 只展示运行状态:本机服务跟随程序生命周期(启动即开、退出即停),
|
||
/// 不再提供「停止服务」按钮 —— 停了网页就叫不醒,属于误操作。
|
||
/// </summary>
|
||
private void UpdateServerState()
|
||
{
|
||
var running = _server.IsRunning;
|
||
|
||
StatusText.Text = running ? $"运行中 · {_server.BaseUrl}" : "未启动";
|
||
StatusDot.Fill = (Brush)FindResource(running ? "OkBrush" : "BadBrush");
|
||
}
|
||
|
||
// ---------------- 启动协议 ----------------
|
||
|
||
/// <summary>
|
||
/// 协议状态只做展示:**注册由安装包完成、卸载时由 MSI 自动删除**;
|
||
/// 运行时启动时会自动校正到当前 exe(App.EnsureProtocolRegistered)。
|
||
/// UI 上不提供手动注册/取消 —— 手动取消会让网页再也叫不醒软件,属于误操作。
|
||
/// </summary>
|
||
private void UpdateProtocolState()
|
||
{
|
||
var registered = ProtocolHelper.IsRegistered();
|
||
var command = ProtocolHelper.RegisteredCommand();
|
||
var exe = Environment.ProcessPath;
|
||
|
||
// 注册表指向的必须就是当前这个 exe(否则网页叫醒拿到的是旧路径/开发目录里的 exe)
|
||
var pointsToMe = command is not null && exe is not null
|
||
&& command.IndexOf(exe, StringComparison.OrdinalIgnoreCase) >= 0;
|
||
|
||
ProtocolText.Text = !registered
|
||
? $"未注册 {ProtocolHelper.Scheme}://(网页无法叫醒本软件,重新运行安装包可修复)"
|
||
: pointsToMe
|
||
? $"已注册 {ProtocolHelper.Scheme}://(网页可叫醒本软件)"
|
||
: $"已注册,但指向旧路径(重启本软件会自动校正)";
|
||
}
|
||
|
||
// ---------------- 查询 ----------------
|
||
|
||
private void Query_Click(object sender, RoutedEventArgs e) => RunQuery();
|
||
|
||
private void QueryBox_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (e.Key == Key.Enter) RunQuery();
|
||
}
|
||
|
||
private void RunQuery()
|
||
{
|
||
var code = QueryBox.Text.Trim();
|
||
AppLog.Write($"查询:{code}");
|
||
|
||
var result = _queryService.Query(code);
|
||
ResultBox.Text = result.Ok
|
||
? JsonUtil.ToPrettyJson(result.Data)
|
||
: $"{(int)result.StatusCode} {result.Message}";
|
||
|
||
ResultBox.ScrollToHome();
|
||
}
|
||
|
||
// ---------------- 退出登录 / 关闭 ----------------
|
||
|
||
private void Logout_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var answer = MessageBox.Show(this,
|
||
$"要退出登录吗?(本机服务会继续运行,重新登录后可继续使用)",
|
||
"退出登录", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
||
if (answer != MessageBoxResult.Yes) return;
|
||
|
||
LogoutRequested?.Invoke();
|
||
}
|
||
|
||
private void OnClosing(object? sender, CancelEventArgs e)
|
||
{
|
||
if (AllowClose) return;
|
||
|
||
// 关窗口 = 缩到托盘,别把本机服务带走
|
||
e.Cancel = true;
|
||
Hide();
|
||
HiddenToTray?.Invoke();
|
||
}
|
||
|
||
// ---------------- 小工具 ----------------
|
||
|
||
private void CopyAddress_Click(object sender, RoutedEventArgs e) => CopyText(_server.BaseUrl);
|
||
|
||
private void OpenInBrowser_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
// 根路径是本机服务的说明页(/api/health 已移除)
|
||
Process.Start(new ProcessStartInfo($"{_server.BaseUrl}/") { UseShellExecute = true });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"打开浏览器失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void CopyText(string text)
|
||
{
|
||
try
|
||
{
|
||
Clipboard.SetText(text);
|
||
AppLog.Write($"已复制:{text}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"复制失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>日志(HTTP 服务线程也会触发,切回 UI 线程再改集合)</summary>
|
||
private void OnLogWritten(string line)
|
||
{
|
||
if (!Dispatcher.CheckAccess())
|
||
{
|
||
Dispatcher.BeginInvoke(new Action(() => Append(line)));
|
||
return;
|
||
}
|
||
|
||
Append(line);
|
||
}
|
||
|
||
private void Append(string line)
|
||
{
|
||
_logs.Add(line);
|
||
|
||
// 只留最近 300 行
|
||
while (_logs.Count > 300) _logs.RemoveAt(0);
|
||
|
||
if (_logs.Count > 0) LogList.ScrollIntoView(_logs[^1]);
|
||
}
|
||
}
|