packServer/desktop/MainWindow.xaml.cs
liufumingren 21dfc7f651 feat(desktop): 日志改为落文件(软件目录 logs\log_日期.txt),界面移除日志模块
- AppLog:不再做内存缓冲/事件,直接按天追加到 <软件目录>\logs\log_yyyy-MM-dd.txt
  行格式 [yyyy-MM-dd HH:mm:ss] 内容;写失败一律吞掉,不影响主流程
  装到只读目录(如 Program Files)时自动退回 %LOCALAPPDATA%\YuncangTrace\logs
- 新增 AppLog.Clear():删除目录下全部 log_*.txt(当天文件也能删,之后重建)
- MainWindow:删掉「运行日志」ListBox 与整套集合/订阅代码,Grid 行 4→3
  查询卡改成 Grid(结果框自适应高度),窗口 620→460
- 菜单「工具」增加「打开日志目录」「清理日志」
- build.ps1 补 -p:AppEnvironment:原来 Release 发布时环境恒为 Production,
  开发包日志/关于里也显示「生产环境」(现在与 -Environment 一致)
- test-update.ps1:更新完成后轮询 60s 等服务与进程起来,避免误报「无响应」
2026-10-17 18:10:28 +08:00

219 lines
7.2 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.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using YuncangTrace.LocalService.Services;
namespace YuncangTrace.LocalService;
/// <summary>
/// 主窗口:本机服务状态、测试查询、当前登录用户
///
/// 日志不进界面:只写文件(<c>软件目录\logs\log_yyyy-MM-dd.txt</c>),
/// 需要看的时候用菜单「工具 → 打开日志目录」。
///
/// 关窗口**不是退出**:只是隐藏到托盘(本机服务得继续跑)。
/// 真正退出:托盘菜单「退出」,或点「退出登录」后再取消登录。
/// </summary>
public partial class MainWindow : Window
{
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;
/// <summary>菜单「帮助 → 检查更新」(交给 App 处理,复用启动时的检查逻辑)</summary>
public event Action? CheckUpdateRequested;
/// <summary>菜单「服务 → 重启软件」(交给 App 处理:先退出再延迟拉起)</summary>
public event Action? RestartRequested;
public MainWindow(LocalHttpServer server, AppSettings settings, Session session)
{
InitializeComponent();
_server = server;
UserText.Text = $"已登录:{session.DisplayName}";
UserText.ToolTip = $"服务器:{settings.ServerBaseUrl}";
Loaded += (_, _) =>
{
UpdateServerState();
};
Closing += OnClosing;
}
/// <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");
MenuServiceStatus.Header = running ? "服务状态:运行中" : "服务状态:未启动";
}
// ---------------- 菜单 ----------------
private void FocusQuery_Click(object sender, RoutedEventArgs e)
{
QueryBox.Focus();
QueryBox.SelectAll();
}
private void ClearUpdateCache_Click(object sender, RoutedEventArgs e)
{
var dir = Path.Combine(Path.GetTempPath(), "YuncangTraceUpdate");
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true);
AppLog.Write($"已清理更新缓存:{dir}");
}
catch (Exception ex)
{
AppLog.Write($"清理更新缓存失败:{ex.Message}");
}
}
/// <summary>用资源管理器打开日志目录(按天一个 log_yyyy-MM-dd.txt)</summary>
private void OpenLogFolder_Click(object sender, RoutedEventArgs e)
{
try
{
Directory.CreateDirectory(AppLog.LogDirectory);
Process.Start(new ProcessStartInfo("explorer.exe", $"\"{AppLog.LogDirectory}\"") { UseShellExecute = true });
}
catch (Exception ex)
{
MessageBox.Show(this, $"打开日志目录失败:{ex.Message}\n\n{AppLog.LogDirectory}",
"打开日志目录", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void ClearLogs_Click(object sender, RoutedEventArgs e)
{
var answer = MessageBox.Show(this,
$"确定删除全部日志文件吗?\n\n{AppLog.LogDirectory}",
"清理日志", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer != MessageBoxResult.Yes) return;
var removed = AppLog.Clear();
AppLog.Write($"已清理日志:删除 {removed} 个文件");
MessageBox.Show(this,
removed > 0 ? $"已删除 {removed} 个日志文件。" : "没有可清理的日志文件。",
"清理日志", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void Restart_Click(object sender, RoutedEventArgs e) => RestartRequested?.Invoke();
private void CheckUpdate_Click(object sender, RoutedEventArgs e) => CheckUpdateRequested?.Invoke();
private void About_Click(object sender, RoutedEventArgs e)
{
var version = GetType().Assembly.GetName().Version?.ToString() ?? "1.0.0";
MessageBox.Show(this,
$"云仓视频溯源软件(本机服务)\n\n" +
$"版本:{version}\n" +
$"环境:{BuildInfo.EnvironmentLabel}\n" +
$"服务:{(_server.IsRunning ? "运行中" : "未启动")}",
"关于",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
// ---------------- 查询 ----------------
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}");
}
}
}