From f4e2d0be387a02cea5ed2f1b6f72596683b05820 Mon Sep 17 00:00:00 2001
From: XuShanQiXun <3401460572@qq.com>
Date: Sun, 8 Jun 2025 07:34:58 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=A6=E6=88=B7=E7=AE=A1?=
=?UTF-8?q?=E7=90=86=E5=92=8C=E7=95=8C=E9=9D=A2=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
在 `ChatDataModel.cs` 中添加 `Account` 类以存储临时账户信息。
修改 `TrayIconManager.cs` 的窗口关闭事件处理,移除通知显示并优化设置逻辑。
更新 `chatapi.cs` 中的服务器 URL 和 IP 地址。
调整 `LoginWindow.xaml` 的尺寸并添加 `Loaded` 事件处理,确保用户名和密码输入框正确绑定。
在 `LoginWindow.xaml.cs` 中实现读取和保存临时登录信息的功能。
修改 `MainWindow.xaml` 的选项卡定义,确保用户信息和设置选项卡正确显示。
更新 `MainWindow.xaml.cs` 中的登录和注册处理逻辑,确保正确处理服务器返回消息并清理资源。
---
chatclient/Data/ChatDataModel.cs | 8 ++
chatclient/Data/TrayIconManager.cs | 16 ++--
chatclient/Data/chatapi.cs | 4 +-
chatclient/LoginWindow.xaml | 8 +-
chatclient/LoginWindow.xaml.cs | 128 ++++++++++++++++++++++++++---
chatclient/MainWindow.xaml | 6 +-
chatclient/MainWindow.xaml.cs | 35 +++++++-
7 files changed, 176 insertions(+), 29 deletions(-)
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">
-
+
-
+
@@ -133,7 +133,7 @@
-
+
diff --git a/chatclient/MainWindow.xaml.cs b/chatclient/MainWindow.xaml.cs
index 1c252b4..39402fd 100644
--- a/chatclient/MainWindow.xaml.cs
+++ b/chatclient/MainWindow.xaml.cs
@@ -21,6 +21,7 @@ using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Collections.Specialized;
+using Hardcodet.Wpf.TaskbarNotification;
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
namespace chatclient
@@ -132,9 +133,9 @@ namespace chatclient
var Type = JsonSerializer.Deserialize(msg);
if (Type != null)
{
- var LoginResponse = JsonSerializer.Deserialize(msg);
if (Type.type == "login_1")
{
+ var LoginResponse = JsonSerializer.Deserialize(msg);
if (LoginResponse!.status == "success" && LoginResponse != null)
{
token = LoginResponse.token;
@@ -176,9 +177,10 @@ namespace chatclient
});
}
}
- else if (Type.type == "login_0" && LoginResponse != null)
+ else if (Type.type == "login_0")
{
- log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}");
+ var LoginResponse = JsonSerializer.Deserialize(msg);
+ log.Warn($"登录失败: {LoginResponse!.message}\nMsg:{msg}");
Application.Current.Dispatcher.Invoke(() =>
{
var loginWindow = Application.Current.Windows.OfType().FirstOrDefault();
@@ -188,6 +190,29 @@ namespace chatclient
}
});
}
+ else if (Type.type == "register_1")
+ {
+ var SignResponse = JsonSerializer.Deserialize(msg);
+ log.Warn($"注册成功\nMsg:{msg}");
+ Application.Current.Dispatcher.Invoke(async () =>
+ {
+ //var loginWindow = Application.Current.Windows.OfType().FirstOrDefault();
+ await LoginWindow.Login(true,LoginWindow.SignName, LoginWindow.SignPassword1);
+ });
+ }
+ else if (Type.type == "register_0")
+ {
+ var SignResponse = JsonSerializer.Deserialize(msg);
+ log.Warn($"注册失败: {SignResponse!.message}\nMsg:{msg}");
+ Application.Current.Dispatcher.Invoke(() =>
+ {
+ var loginWindow = Application.Current.Windows.OfType().FirstOrDefault();
+ if (loginWindow != null)
+ {
+ loginWindow.SignMsg = "用户名已存在";
+ }
+ });
+ }
else if (Type.type == "chat")
{
var chat = JsonSerializer.Deserialize(msg);
@@ -345,12 +370,16 @@ namespace chatclient
}
private void MainWindow_Closed(object sender, System.EventArgs e)
{
+ log.Info("MainWindow 关闭事件触发,清理资源");
// 清理资源
Client?.Shutdown(SocketShutdown.Both);
Client?.Close();
+ log.Info("关闭Socket连接");
Client?.Dispose();
token = null;
_trayManager?.Dispose();
+ log.Info("托盘图标管理器已释放资源");
+ log.Info("Bye!");
}
}
}
\ No newline at end of file