- 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 良构与括号平衡检查。
85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Text.Encodings.Web;
|
||
using System.Text.Json;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 本机设置:`%LOCALAPPDATA%\YuncangTrace\settings.json`
|
||
///
|
||
/// 目前只有「服务器地址」(登录用的后端)和「记住账号」。
|
||
/// </summary>
|
||
public sealed class AppSettings
|
||
{
|
||
/// <summary>默认后端地址(本地开发)</summary>
|
||
public const string DefaultServer = "http://127.0.0.1:7300";
|
||
|
||
/// <summary>后端 API 根地址(登录、校验令牌都走它)</summary>
|
||
public string ServerBaseUrl { get; set; } = DefaultServer;
|
||
|
||
/// <summary>上次登录的账号(只在「记住账号」勾选时保存)</summary>
|
||
public string LastAccount { get; set; } = string.Empty;
|
||
|
||
/// <summary>是否记住账号</summary>
|
||
public bool RememberAccount { get; set; } = true;
|
||
|
||
private static string Dir => Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YuncangTrace");
|
||
|
||
private static string FilePath => Path.Combine(Dir, "settings.json");
|
||
|
||
private static readonly JsonSerializerOptions Options = new()
|
||
{
|
||
WriteIndented = true,
|
||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||
};
|
||
|
||
/// <summary>读设置(读不到就用默认值,绝不抛)</summary>
|
||
public static AppSettings Load()
|
||
{
|
||
try
|
||
{
|
||
if (!File.Exists(FilePath)) return new AppSettings();
|
||
|
||
var settings = JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(FilePath));
|
||
if (settings is null) return new AppSettings();
|
||
|
||
settings.ServerBaseUrl = Normalize(settings.ServerBaseUrl);
|
||
return settings;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"读取设置失败(用默认值):{ex.Message}");
|
||
return new AppSettings();
|
||
}
|
||
}
|
||
|
||
/// <summary>存设置(失败只记日志)</summary>
|
||
public void Save()
|
||
{
|
||
try
|
||
{
|
||
ServerBaseUrl = Normalize(ServerBaseUrl);
|
||
if (!RememberAccount) LastAccount = string.Empty;
|
||
|
||
Directory.CreateDirectory(Dir);
|
||
File.WriteAllText(FilePath, JsonSerializer.Serialize(this, Options));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"保存设置失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>统一成 `http://host:port` 形式(去掉末尾斜杠,没写协议就补 http://)</summary>
|
||
public static string Normalize(string? url)
|
||
{
|
||
var value = (url ?? string.Empty).Trim();
|
||
if (value.Length == 0) return DefaultServer;
|
||
|
||
if (!value.Contains("://", StringComparison.Ordinal)) value = "http://" + value;
|
||
return value.TrimEnd('/');
|
||
}
|
||
}
|