- 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 良构与括号平衡检查。
177 lines
4.8 KiB
C#
177 lines
4.8 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Input;
|
||
using YuncangTrace.LocalService.Services;
|
||
|
||
namespace YuncangTrace.LocalService;
|
||
|
||
/// <summary>
|
||
/// 登录窗
|
||
///
|
||
/// - 服务器地址 / 账号 / 密码 → `POST /api/web/auth/login` 拿 user JWT;
|
||
/// - 本地已有令牌时,先调 `GET /api/web/me` 验证,还有效就直接跳过登录(窗口一闪而过);
|
||
/// - 成功后把令牌存到 `%LOCALAPPDATA%\YuncangTrace\session.json`。
|
||
/// </summary>
|
||
public partial class LoginWindow : Window
|
||
{
|
||
private readonly AppSettings _settings;
|
||
private readonly Session? _saved;
|
||
private bool _busy;
|
||
|
||
/// <summary>登录成功后的会话(ShowDialog 返回 true 时可用)</summary>
|
||
public Session? Session { get; private set; }
|
||
|
||
/// <summary>登录用户</summary>
|
||
public ApiUser? User { get; private set; }
|
||
|
||
public LoginWindow(AppSettings settings, Session? saved)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_settings = settings;
|
||
_saved = saved;
|
||
|
||
ServerBox.Text = settings.ServerBaseUrl;
|
||
AccountBox.Text = settings.LastAccount;
|
||
RememberBox.IsChecked = settings.RememberAccount;
|
||
|
||
Loaded += OnLoaded;
|
||
}
|
||
|
||
private async void OnLoaded(object sender, RoutedEventArgs e)
|
||
{
|
||
if (_saved is null)
|
||
{
|
||
AccountBox.Focus();
|
||
return;
|
||
}
|
||
|
||
await ResumeAsync();
|
||
}
|
||
|
||
/// <summary>本地有令牌:问一下服务端还在不在,有效就直接进主界面</summary>
|
||
private async Task ResumeAsync()
|
||
{
|
||
SetBusy(true, "正在恢复登录…");
|
||
|
||
try
|
||
{
|
||
var api = new ApiClient(_saved!.ServerBaseUrl);
|
||
var me = await api.MeAsync(_saved.Token);
|
||
|
||
if (me is null)
|
||
{
|
||
AppLog.Write("本地登录态已失效,请重新登录");
|
||
SessionStore.Clear();
|
||
SetBusy(false);
|
||
AccountBox.Focus();
|
||
return;
|
||
}
|
||
|
||
Session = _saved;
|
||
User = me;
|
||
AppLog.Write($"已恢复登录:{me.DisplayName}");
|
||
DialogResult = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"恢复登录失败:{ex.Message}");
|
||
SetBusy(false);
|
||
ShowError($"恢复登录失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void Login_Click(object sender, RoutedEventArgs e) => _ = DoLoginAsync();
|
||
|
||
private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (e.Key == Key.Enter) _ = DoLoginAsync();
|
||
}
|
||
|
||
private async Task DoLoginAsync()
|
||
{
|
||
if (_busy) return;
|
||
|
||
var server = AppSettings.Normalize(ServerBox.Text);
|
||
var account = AccountBox.Text.Trim();
|
||
var password = PasswordBox.Password;
|
||
|
||
if (account.Length == 0)
|
||
{
|
||
ShowError("请填写账号");
|
||
AccountBox.Focus();
|
||
return;
|
||
}
|
||
if (password.Length == 0)
|
||
{
|
||
ShowError("请填写密码");
|
||
PasswordBox.Focus();
|
||
return;
|
||
}
|
||
|
||
ClearError();
|
||
SetBusy(true, "正在登录…");
|
||
|
||
try
|
||
{
|
||
var api = new ApiClient(server);
|
||
var (session, user) = await api.LoginAsync(account, password);
|
||
|
||
session.DisplayName = user.DisplayName;
|
||
SessionStore.Save(session);
|
||
|
||
_settings.ServerBaseUrl = server;
|
||
_settings.RememberAccount = RememberBox.IsChecked == true;
|
||
_settings.LastAccount = _settings.RememberAccount ? account : string.Empty;
|
||
_settings.Save();
|
||
|
||
Session = session;
|
||
User = user;
|
||
|
||
AppLog.Write($"登录成功:{user.DisplayName}({server})");
|
||
DialogResult = true;
|
||
}
|
||
catch (ApiException ex)
|
||
{
|
||
ShowError(ex.Message);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowError($"登录失败:{ex.Message}");
|
||
}
|
||
finally
|
||
{
|
||
SetBusy(false);
|
||
}
|
||
}
|
||
|
||
private void SetBusy(bool busy, string? text = null)
|
||
{
|
||
_busy = busy;
|
||
|
||
LoginButton.IsEnabled = !busy;
|
||
ExitButton.IsEnabled = !busy;
|
||
ServerBox.IsEnabled = !busy;
|
||
AccountBox.IsEnabled = !busy;
|
||
PasswordBox.IsEnabled = !busy;
|
||
RememberBox.IsEnabled = !busy;
|
||
|
||
LoginButton.Content = busy ? (text ?? "请稍候…") : "登录";
|
||
}
|
||
|
||
private void ShowError(string message)
|
||
{
|
||
ErrorText.Text = message;
|
||
ErrorText.Visibility = Visibility.Visible;
|
||
}
|
||
|
||
private void ClearError()
|
||
{
|
||
ErrorText.Text = string.Empty;
|
||
ErrorText.Visibility = Visibility.Collapsed;
|
||
}
|
||
|
||
private void Exit_Click(object sender, RoutedEventArgs e) => DialogResult = false;
|
||
}
|