一、工具链 - 本机没有 .NET,微软域名又被网络阻断,改用 curl 从 builds.dotnet.microsoft.com 拉 dotnet-sdk-8.0.426-win-x64.zip,解压到 %LOCALAPPDATA%\Microsoft\dotnet(免管理员), 并写入用户 PATH / DOTNET_ROOT / DOTNET_CLI_TELEMETRY_OPTOUT。 二、首次编译暴露的两个真实缺陷(都已修复) - ProtocolHelper.cs 缺 using System.Collections.Generic(ImplicitUsings=disable 后 IEnumerable<string> 找不到)。 - App.xaml 少了 BgBrush,LoginWindow/MainWindow 用 StaticResource 引用它 → 编译期查不出, 运行期一弹登录窗就 XamlParseException 直接退出(事件日志里能看到)。 已补 BgBrush=#F5F7FA 并注明「缺一个就会运行时炸」。 - 顺带把 WFAC010(WinForms 建议 DPI 改走 ApplicationHighDpiMode)压掉并写明原因: 本程序宿主是 WPF,走不到 WinForms 的 ApplicationConfiguration,DPI 只能由 manifest 决定。 三、服务地址 = 构建期环境(对齐前端 VITE_BACKEND 的思路) - csproj:Debug → AppEnvironment=Development、ServerBaseUrl=http://127.0.0.1:7300; Release → Production、占位 https://packs.example.com;用 -p:ServerBaseUrl= 覆盖; 两者通过 AssemblyMetadataAttribute 写进程序集,exe 自带「我是哪个环境」。 - 新增 Services/BuildInfo.cs 读元数据(带兜底,绝不抛)。 - AppSettings:默认值改为 BuildInfo.DefaultServer;等于内置默认值时不落盘(换构建能自动跟随), 老版本已把默认值写进 settings.json 的也识别为"没改过";新增 UseServer() 支持运行时临时覆盖。 - App:解析 --server=xxx / -s xxx,其次 YUNCANG_SERVER 环境变量,写在构建默认值之前。 - 登录窗:「服务器地址」改叫「服务地址」,右侧显示"开发环境/生产环境"(只展示,由构建决定), 输入框 ToolTip 说明留空即用当前环境默认地址。 - /api/health 增加 env(Development/Production)与 server(当前后端地址),排障一眼看出装错没有。 四、验证 - Debug / Release / Release+域名覆盖 三种构建均 0 警告 0 错误; - Debug:health 返回 env=Development、server=http://127.0.0.1:7300; - Release:env=Production、server=https://packs.example.com; Release 加 -p:ServerBaseUrl=https://trace.yuncang.cn 后 server 随之变化; - 带 --server=http://10.0.0.9:7300 启动:server 被覆盖,settings.json 未被污染; - health / 未登录 query(401) / activate-login(200,登录窗弹出) / OPTIONS 预检(204+CORS) 全部正常。 - 注意:改 -p: 属性必须 -t:Rebuild,否则 MSBuild 不会重新生成 AssemblyInfo。 五、README - 新增「环境配置(开发 / 生产)」:构建矩阵、发布时覆盖域名、优先级、默认值不落盘的原因、 settings.json/session.json 的行为;补了本机 SDK 装在用户目录、需要 DOTNET_ROOT 的说明; 行为表新增「网页请求唤起登录」一行;health 返回字段更新。
133 lines
5.2 KiB
C#
133 lines
5.2 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`
|
||
///
|
||
/// 目前只有「服务器地址」(登录用的后端)和「记住账号」。
|
||
/// 服务器地址的**默认值**来自构建环境(<see cref="BuildInfo"/>),用户没改过就不落盘,
|
||
/// 这样 Debug / Release 两个构建各自连自己的后端,不会互相污染。
|
||
/// </summary>
|
||
public sealed class AppSettings
|
||
{
|
||
/// <summary>当前构建的默认后端地址(Debug = 本地 7300,Release = 正式站点)</summary>
|
||
public static string DefaultServer => BuildInfo.DefaultServer;
|
||
|
||
/// <summary>后端 API 根地址(登录、校验令牌都走它)</summary>
|
||
public string ServerBaseUrl { get; set; } = DefaultServer;
|
||
|
||
/// <summary>地址来自命令行/环境变量的临时覆盖(不落盘)</summary>
|
||
[System.Text.Json.Serialization.JsonIgnore]
|
||
public bool ServerIsVolatile { get; private set; }
|
||
|
||
/// <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();
|
||
|
||
// 文件里存的是空串 = 用户没改过,用当前构建的默认值;
|
||
// 老版本会把默认地址直接落盘,所以「等于任何已知默认值」也当作没改过,跟随当前构建。
|
||
var stored = Normalize(settings.ServerBaseUrl);
|
||
settings.ServerBaseUrl = IsKnownDefault(stored) ? DefaultServer : stored;
|
||
return settings;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"读取设置失败(用默认值):{ex.Message}");
|
||
return new AppSettings();
|
||
}
|
||
}
|
||
|
||
/// <summary>运行时指定后端地址(<c>--server=</c> / <c>YUNCANG_SERVER</c>),只影响本次运行</summary>
|
||
public void UseServer(string? url)
|
||
{
|
||
var value = (url ?? string.Empty).Trim();
|
||
if (value.Length == 0) return;
|
||
|
||
ServerBaseUrl = Normalize(value);
|
||
ServerIsVolatile = true;
|
||
AppLog.Write($"后端地址已被临时覆盖:{ServerBaseUrl}(不保存)");
|
||
}
|
||
|
||
/// <summary>存设置(失败只记日志)</summary>
|
||
public void Save()
|
||
{
|
||
try
|
||
{
|
||
ServerBaseUrl = Normalize(ServerBaseUrl);
|
||
if (!RememberAccount) LastAccount = string.Empty;
|
||
|
||
Directory.CreateDirectory(Dir);
|
||
|
||
// 等于构建默认值(或只是临时覆盖)就写空串:换环境构建时能自动跟随,只有用户手改的地址才落盘
|
||
var stored = ServerIsVolatile || ServerBaseUrl == DefaultServer ? string.Empty : ServerBaseUrl;
|
||
var payload = new StoredSettings
|
||
{
|
||
ServerBaseUrl = stored,
|
||
LastAccount = LastAccount,
|
||
RememberAccount = RememberAccount,
|
||
};
|
||
|
||
File.WriteAllText(FilePath, JsonSerializer.Serialize(payload, 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('/');
|
||
}
|
||
|
||
/// <summary>是不是"内置默认地址"之一(老版本落盘过这些值,不该当成用户自定义)</summary>
|
||
private static bool IsKnownDefault(string url)
|
||
{
|
||
return url.Equals(DefaultServer, StringComparison.OrdinalIgnoreCase)
|
||
|| url.Equals(Normalize(BuildInfo.DevServer), StringComparison.OrdinalIgnoreCase)
|
||
|| url.Equals(Normalize(BuildInfo.ProdServerPlaceholder), StringComparison.OrdinalIgnoreCase);
|
||
}
|
||
|
||
/// <summary>落盘用的结构(和本类的可读属性一一对应)</summary>
|
||
private sealed class StoredSettings
|
||
{
|
||
public string ServerBaseUrl { get; set; } = string.Empty;
|
||
|
||
public string LastAccount { get; set; } = string.Empty;
|
||
|
||
public bool RememberAccount { get; set; } = true;
|
||
}
|
||
}
|