2025-06-01 02:57:57 +08:00
|
|
|
|
using System;
|
2025-06-01 20:33:52 +08:00
|
|
|
|
using System.ComponentModel;
|
2025-06-01 02:57:57 +08:00
|
|
|
|
using System.Text;
|
2025-06-01 20:33:52 +08:00
|
|
|
|
using System.Text.Json;
|
2025-06-01 02:57:57 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2025-06-01 20:33:52 +08:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using log4net;
|
2025-06-02 16:40:36 +08:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Microsoft.Win32;
|
2025-06-06 20:48:03 +08:00
|
|
|
|
using chatclient.Data;
|
2025-06-07 11:01:45 +08:00
|
|
|
|
using System.Net;
|
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
|
2025-06-01 20:33:52 +08:00
|
|
|
|
|
2025-06-01 02:57:57 +08:00
|
|
|
|
|
|
|
|
|
namespace chatclient
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// LoginWindow.xaml 的交互逻辑
|
|
|
|
|
/// </summary>
|
2025-06-01 22:34:58 +08:00
|
|
|
|
public partial class LoginWindow : Window, INotifyPropertyChanged
|
2025-06-01 02:57:57 +08:00
|
|
|
|
{
|
2025-06-01 20:33:52 +08:00
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(LoginWindow));
|
2025-06-01 02:57:57 +08:00
|
|
|
|
public LoginWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2025-06-01 20:33:52 +08:00
|
|
|
|
this.DataContext = this;
|
|
|
|
|
}
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
2025-06-01 22:34:58 +08:00
|
|
|
|
private void Update(string UpdateName)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(UpdateName));
|
|
|
|
|
}
|
|
|
|
|
public static string UserName { get; set; } = "";
|
|
|
|
|
public static string UserPassword { get; set; } = "";
|
|
|
|
|
public static string SignName { get; set; } = "";
|
|
|
|
|
public static string SignPassword1 { get; set; } = "";
|
|
|
|
|
public static string SignPassword2 { get; set; } = "";
|
2025-06-02 16:40:36 +08:00
|
|
|
|
private string? _LoginMsg;
|
|
|
|
|
public string? LoginMsg
|
|
|
|
|
{
|
|
|
|
|
get { return _LoginMsg; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_LoginMsg = value;
|
|
|
|
|
Update("LoginMsg");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-01 22:34:58 +08:00
|
|
|
|
private string? _SignMsg;
|
|
|
|
|
public string? SignMsg
|
2025-06-01 20:33:52 +08:00
|
|
|
|
{
|
2025-06-01 22:34:58 +08:00
|
|
|
|
get { return _SignMsg; }
|
|
|
|
|
set
|
2025-06-01 20:33:52 +08:00
|
|
|
|
{
|
2025-06-01 22:34:58 +08:00
|
|
|
|
_SignMsg = value;
|
|
|
|
|
Update("SignMsg");
|
2025-06-01 20:33:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-01 22:34:58 +08:00
|
|
|
|
private bool _SaveAccount;
|
|
|
|
|
public bool SaveAccount
|
2025-06-01 20:33:52 +08:00
|
|
|
|
{
|
2025-06-01 22:34:58 +08:00
|
|
|
|
get { return _SaveAccount; }
|
2025-06-01 20:33:52 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2025-06-01 22:34:58 +08:00
|
|
|
|
_SaveAccount = value;
|
|
|
|
|
Update("SaveAccount");
|
2025-06-01 20:33:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-07 11:01:45 +08:00
|
|
|
|
private async void Login_Click(object sender, RoutedEventArgs e)
|
2025-06-01 20:33:52 +08:00
|
|
|
|
{
|
|
|
|
|
if (UserName == "")
|
|
|
|
|
{
|
2025-06-02 16:40:36 +08:00
|
|
|
|
LoginMsg = "用户名不能为空";
|
2025-06-01 20:33:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (UserPassword == "")
|
|
|
|
|
{
|
2025-06-02 16:40:36 +08:00
|
|
|
|
LoginMsg = "密码不能为空";
|
2025-06-01 20:33:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2025-06-02 16:40:36 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
await Login(false, UserName, UserPassword);
|
2025-06-02 16:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void Sign_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (SignName == "")
|
|
|
|
|
{
|
|
|
|
|
SignMsg = "用户名不能为空";
|
|
|
|
|
}
|
|
|
|
|
else if (SignPassword1 == "")
|
|
|
|
|
{
|
|
|
|
|
SignMsg = "密码不能为空";
|
|
|
|
|
}
|
|
|
|
|
else if (SignPassword2 == "")
|
|
|
|
|
{
|
|
|
|
|
SignMsg = "请再次确认密码";
|
|
|
|
|
}
|
|
|
|
|
else if (SignPassword1 != SignPassword2)
|
|
|
|
|
{
|
|
|
|
|
SignMsg = "两次密码输入不一致";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var SignData = new
|
|
|
|
|
{
|
|
|
|
|
type = "register",
|
|
|
|
|
username = Username,
|
|
|
|
|
password = Userpassword
|
|
|
|
|
};
|
|
|
|
|
string SignJsonData = JsonSerializer.Serialize(SignData);
|
|
|
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes(SignJsonData);
|
|
|
|
|
var content = new StringContent(SignJsonData, Encoding.UTF8, "application/json");
|
2025-06-06 20:48:03 +08:00
|
|
|
|
var response = await MainWindow.HttpClient.PostAsync($"{Server.ServerUrl}/api/register", content);
|
2025-06-02 16:40:36 +08:00
|
|
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
|
|
log.Info($"注册请求已发送,响应内容: {responseBody}");
|
|
|
|
|
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
|
|
|
|
if (signresponse!.success)
|
|
|
|
|
{
|
|
|
|
|
log.Info($"注册成功: {signresponse.message}");
|
2025-06-07 11:01:45 +08:00
|
|
|
|
await Login(true, Username, Userpassword);
|
2025-06-02 16:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Error($"注册失败: {signresponse.message}");
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
|
|
|
|
loginWindow!.SignMsg = signresponse.message;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("注册请求发送失败", ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
log.Info("注册请求已完成");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-07 11:01:45 +08:00
|
|
|
|
public static async Task Login(bool Sign, string Username, string Userpassword)
|
2025-06-02 16:40:36 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
// 公共的登录数据准备
|
|
|
|
|
var LoginData = new
|
|
|
|
|
{
|
|
|
|
|
type = "login",
|
|
|
|
|
username = Username,
|
|
|
|
|
password = Userpassword
|
|
|
|
|
};
|
|
|
|
|
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
|
|
|
|
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
|
2025-06-01 22:34:58 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
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);
|
2025-06-01 22:34:58 +08:00
|
|
|
|
}
|
2025-06-07 11:01:45 +08:00
|
|
|
|
catch (Exception ex)
|
2025-06-01 22:34:58 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
log.Error($"连接失败: {ex.Message}");
|
|
|
|
|
MainWindow.Client?.Close();
|
|
|
|
|
// 根据Sign类型更新UI
|
|
|
|
|
string errorMsg = Sign ?
|
|
|
|
|
"在完成注册后与服务器断开连接\n请尝试在登录窗口重新登录" :
|
|
|
|
|
"服务器连接失败";
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
2025-06-01 22:34:58 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
|
|
|
|
if (loginWindow != null)
|
2025-06-01 22:34:58 +08:00
|
|
|
|
{
|
2025-06-07 11:01:45 +08:00
|
|
|
|
if (Sign) loginWindow.SignMsg = errorMsg;
|
|
|
|
|
else loginWindow.LoginMsg = errorMsg;
|
2025-06-01 22:34:58 +08:00
|
|
|
|
}
|
2025-06-07 11:01:45 +08:00
|
|
|
|
});
|
2025-06-01 22:34:58 +08:00
|
|
|
|
}
|
2025-06-07 11:01:45 +08:00
|
|
|
|
});
|
2025-06-01 02:57:57 +08:00
|
|
|
|
}
|
2025-06-02 16:40:36 +08:00
|
|
|
|
public void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// 窗口加载时可以进行一些初始化操作
|
|
|
|
|
log.Info("登录窗口已加载");
|
|
|
|
|
// 如果需要从配置文件或其他地方加载保存的账号信息,可以在这里实现
|
|
|
|
|
// 例如:UserName = LoadSavedUsername();
|
|
|
|
|
// Update("UserName");
|
|
|
|
|
}
|
2025-06-07 16:17:28 +08:00
|
|
|
|
public void Window_Closing(object sender, CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(MainWindow.token == null) Application.Current.Shutdown();
|
|
|
|
|
}
|
2025-06-01 02:57:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-02 16:40:36 +08:00
|
|
|
|
|