diff --git a/chatclient/Data/ChatDataModel.cs b/chatclient/Data/ChatDataModel.cs index 255987a..3e773cf 100644 --- a/chatclient/Data/ChatDataModel.cs +++ b/chatclient/Data/ChatDataModel.cs @@ -52,4 +52,12 @@ namespace chatclient.Data File,//文件 System//系统信息 } + /// + /// 临时账户信息类,包含用户名和密码 + /// + public class Account + { + public string? UserName { get; set; } + public string? UserPassword { get; set; } + } } diff --git a/chatclient/Data/TrayIconManager.cs b/chatclient/Data/TrayIconManager.cs index a0c34ca..f1e04dc 100644 --- a/chatclient/Data/TrayIconManager.cs +++ b/chatclient/Data/TrayIconManager.cs @@ -35,11 +35,6 @@ namespace chatclient.Data // 取消关闭操作,改为最小化到托盘 e.Cancel = true; _mainWindow.Hide(); - - // 显示通知 - //_trayIcon.ShowBalloonTip("Application minimized", - // "The application is running in the system tray", - // BalloonIcon.Info); } private ContextMenu CreateContextMenu() @@ -70,7 +65,6 @@ namespace chatclient.Data Style = (Style)Application.Current.Resources["MaterialTrayMenuItem"], Icon = new PackIcon { Kind = iconKind, Width = 20, Height = 20 } }; - menuItem.Click += clickHandler; return menuItem; } @@ -94,7 +88,15 @@ namespace chatclient.Data private void Settings_Click(object sender, RoutedEventArgs e) { - // 实现设置逻辑 + RestoreApplication(); + Application.Current.Dispatcher.Invoke(() => + { + var mainWindow = Application.Current.Windows.OfType().FirstOrDefault(); + if (mainWindow != null) + { + mainWindow.TabControl.SelectedItem = mainWindow.Settings; + } + }); } private void Updates_Click(object sender, RoutedEventArgs e) diff --git a/chatclient/Data/chatapi.cs b/chatclient/Data/chatapi.cs index 5ec8aae..aef143b 100644 --- a/chatclient/Data/chatapi.cs +++ b/chatclient/Data/chatapi.cs @@ -4,8 +4,8 @@ namespace chatclient.Data { internal class Server { - public const string ServerUrl = "http://127.0.0.1:5001"; - public const string ServerIP = "127.0.0.1"; + public const string ServerUrl = "http://175.24.191.172:5001"; + public const string ServerIP = "175.24.191.172"; public const int ServerPort = 8889; } internal class LoginData diff --git a/chatclient/LoginWindow.xaml b/chatclient/LoginWindow.xaml index 61fab8c..08bb72e 100644 --- a/chatclient/LoginWindow.xaml +++ b/chatclient/LoginWindow.xaml @@ -7,8 +7,8 @@ xmlns:local="clr-namespace:chatclient" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="chatclient.LoginWindow" mc:Ignorable="d" - Title="LoginWindow" Height="590" Width="330" MinHeight="590" MinWidth="330" MaxHeight="590" MaxWidth="330" - ResizeMode="NoResize" Closing="Window_Closing"> + Title="LoginWindow" Height="540" Width="330" MinHeight="540" MinWidth="330" MaxHeight="540" MaxWidth="330" + ResizeMode="NoResize" Closing="Window_Closing" Loaded="Window_Loaded"> @@ -26,8 +26,8 @@ - - + diff --git a/chatclient/LoginWindow.xaml.cs b/chatclient/LoginWindow.xaml.cs index ba1c4b1..f28d3c6 100644 --- a/chatclient/LoginWindow.xaml.cs +++ b/chatclient/LoginWindow.xaml.cs @@ -11,6 +11,8 @@ using Microsoft.Win32; using chatclient.Data; using System.Net; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; +using Microsoft.VisualBasic.ApplicationServices; +using MaterialDesignThemes.Wpf; namespace chatclient @@ -101,21 +103,91 @@ namespace chatclient } else { - log.Info($"向服务器发送注册HttpAPI请求(UserName:{SignName})"); - SignRegistryUser(SignName, SignPassword1).ContinueWith(task => + SignRegistryUser(SignName, SignPassword1).ContinueWith(Task => { - if (task.IsCompletedSuccessfully) + if (Task.IsCompletedSuccessfully) { log.Info("注册请求发送成功"); + Application.Current.Dispatcher.Invoke(() => + { + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + if (loginWindow != null) + { + loginWindow.SignMsg = "注册请求已发送,请等待服务器响应"; + } + }); } else { - log.Error("注册请求发送失败", task.Exception); + log.Error("注册请求发送失败", Task.Exception); + Application.Current.Dispatcher.Invoke(() => + { + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + if (loginWindow != null) + { + loginWindow.SignMsg = "注册请求发送失败,请检查网络连接"; + } + }); } }); + //log.Info($"向服务器发送注册HttpAPI请求(UserName:{SignName})"); + //SignRegistryUser(SignName, SignPassword1).ContinueWith(task => + //{ + // if (task.IsCompletedSuccessfully) + // { + // log.Info("注册请求发送成功"); + // } + // else + // { + // log.Error("注册请求发送失败", task.Exception); + // } + //}); } } - public static async Task SignRegistryUser(string Username, string Userpassword) + public static async Task SignRegistryUser(string Username, string Userpassword) + { + var SignData = new + { + type = "register", + username = Username, + password = Userpassword + }; + string LoginJsonData = JsonSerializer.Serialize(SignData); + byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData); + log.Info($"向服务器发送注册请求(UserName:{Username})"); + // 检查Socket是否可用 + if (MainWindow.Client?.Connected == true) + { + MainWindow.Client.Send(dataBytes); + return; + } + log.Info("未连接服务器,尝试异步连接"); + // 异步连接操作 + await Task.Run(() => + { + try + { + MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + MainWindow.Client?.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort); + MainWindow.StartReceive(); + MainWindow.Client?.Send(dataBytes); + } + catch (Exception ex) + { + log.Error($"连接失败: {ex.Message}"); + MainWindow.Client?.Close(); + Application.Current.Dispatcher.Invoke(() => + { + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + if (loginWindow != null) + { + loginWindow.SignMsg = "服务器连接失败"; + } + }); + } + }); + } + public static async Task HttpSignRegistryUser(string Username, string Userpassword) { try { @@ -207,15 +279,51 @@ namespace chatclient } public void Window_Loaded(object sender, RoutedEventArgs e) { - // 窗口加载时可以进行一些初始化操作 log.Info("登录窗口已加载"); - // 如果需要从配置文件或其他地方加载保存的账号信息,可以在这里实现 - // 例如:UserName = LoadSavedUsername(); - // Update("UserName"); + + try + { + string tempPath = System.IO.Path.GetTempPath(); + string filePath = System.IO.Path.Combine(tempPath, "chatclient_login.tmp"); + if (System.IO.File.Exists(filePath)) + { + string json = System.IO.File.ReadAllText(filePath, Encoding.UTF8); + var loginInfo = JsonSerializer.Deserialize (json); + if (loginInfo != null) + { + + UserName = loginInfo.UserName!; + NameBox.Text = loginInfo.UserName; + UserPassword = loginInfo.UserPassword!; + PasswoedBox.Password = loginInfo.UserPassword; + SaveAccount = true; + + } + } + } + catch (Exception ex) + { + log.Error("读取临时登录信息失败", ex); + } } public void Window_Closing(object sender, CancelEventArgs e) { - if(MainWindow.token == null) Application.Current.Shutdown(); + if (SaveAccount && !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(UserPassword)) + { + try + { + string tempPath = System.IO.Path.GetTempPath(); + string filePath = System.IO.Path.Combine(tempPath, "chatclient_login.tmp"); + var loginInfo = new { UserName, UserPassword }; + string json = JsonSerializer.Serialize(loginInfo); + System.IO.File.WriteAllText(filePath, json, Encoding.UTF8); + } + catch (Exception ex) + { + log.Error("保存登录信息到临时文件失败", ex); + } + } + if (MainWindow.token == null) Application.Current.Shutdown(); } } } diff --git a/chatclient/MainWindow.xaml b/chatclient/MainWindow.xaml index cce7b90..1c1c515 100644 --- a/chatclient/MainWindow.xaml +++ b/chatclient/MainWindow.xaml @@ -11,7 +11,7 @@ Style="{StaticResource MaterialDesignWindow}" Closed="MainWindow_Closed" Loaded="MainWindow_Loaded"> - +