一、UI
- 主界面删除「已注册 yuncang://(网页可叫醒本软件)」整行(`ProtocolText`),
行定义 4→3(标题 / 查询 / 日志);删除 `UpdateProtocolState()` 及其调用。
协议对用户是内部实现,不展示;注册由 MSI 完成、卸载自动删除、启动自动校正。
二、修复(本次测试暴露的真实缺陷)
1. **更新检查提前到登录之前**(`App.OnStartup`):
原来放在 `ShowMainWindow()` 之后,一旦本地令牌失效/未登录,软件停在登录窗,
**永远不会检查更新**(实测 1.0.7 → 1.0.8 卡在登录窗 180s 未更新)。
2. **`ApiClient.MeAsync` 不再把网络错误当成"令牌失效"**:
原来 catch(ApiException) → return null,调用方据此 `SessionStore.Clear()`,
后端启动中/一时连不上就会**误删 session.json**(本次就丢了登录态)。
现在只有 401/403 返回 null;超时/连不上/5xx 抛异常 → 登录窗保留令牌并提示,
下次启动仍可自动登录。
三、验证(标准流程:打包 → 启动旧版 → 自动更新)
- 打包 1.0.9(含修复)并安装(旧版 1.0.7 因缺陷无法自更新,只能手工升级一次);
- 打包 1.0.10 → `test-update.ps1`:
当前安装 1.0.9(**处于登录窗,未登录**)→ 后端目标 1.0.10
[4s] 1.0.9 → [10s] 1.0.10
✔ 自动更新完成:1.0.9 → 1.0.10(耗时 10.1s)
✔ 本机服务 17300 有响应(HTTP 401)
✔ 软件进程已恢复
证明「未登录也能检测并完成更新」。
四、待用户操作:本次测试中 session.json 被旧逻辑误删,需要**重新登录一次**;
之后的版本不会因为网络抖动再丢登录态。
168 lines
4.9 KiB
C#
168 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
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}";
|
||
|
||
// 日志:先灌历史(服务在登录前就起来了),再订阅新的
|
||
foreach (var line in AppLog.History) _logs.Add(line);
|
||
|
||
LogList.ItemsSource = _logs;
|
||
AppLog.Written += OnLogWritten;
|
||
|
||
Loaded += (_, _) =>
|
||
{
|
||
UpdateServerState();
|
||
};
|
||
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");
|
||
}
|
||
|
||
// ---------------- 查询 ----------------
|
||
|
||
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 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]);
|
||
}
|
||
}
|