269 lines
9.9 KiB
C#
269 lines
9.9 KiB
C#
using System;
|
||
using System.ComponentModel;
|
||
using System.Text;
|
||
using System.Text.Json;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Net.Sockets;
|
||
using log4net;
|
||
using System.Net.Http;
|
||
using Microsoft.Win32;
|
||
using chatclient.Data;
|
||
|
||
|
||
namespace chatclient
|
||
{
|
||
/// <summary>
|
||
/// LoginWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class LoginWindow : Window, INotifyPropertyChanged
|
||
{
|
||
private static readonly ILog log = LogManager.GetLogger(typeof(LoginWindow));
|
||
public LoginWindow()
|
||
{
|
||
InitializeComponent();
|
||
this.DataContext = this;
|
||
}
|
||
public event PropertyChangedEventHandler? PropertyChanged;
|
||
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; } = "";
|
||
private string? _LoginMsg;
|
||
public string? LoginMsg
|
||
{
|
||
get { return _LoginMsg; }
|
||
set
|
||
{
|
||
_LoginMsg = value;
|
||
Update("LoginMsg");
|
||
}
|
||
}
|
||
private string? _SignMsg;
|
||
public string? SignMsg
|
||
{
|
||
get { return _SignMsg; }
|
||
set
|
||
{
|
||
_SignMsg = value;
|
||
Update("SignMsg");
|
||
}
|
||
}
|
||
private bool _SaveAccount;
|
||
public bool SaveAccount
|
||
{
|
||
get { return _SaveAccount; }
|
||
set
|
||
{
|
||
_SaveAccount = value;
|
||
Update("SaveAccount");
|
||
}
|
||
}
|
||
private void Login_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (UserName == "")
|
||
{
|
||
LoginMsg = "用户名不能为空";
|
||
}
|
||
else if (UserPassword == "")
|
||
{
|
||
LoginMsg = "密码不能为空";
|
||
}
|
||
else
|
||
{
|
||
Login(false,UserName,UserPassword);
|
||
}
|
||
}
|
||
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");
|
||
var response = await MainWindow.HttpClient.PostAsync($"{Server.ServerUrl}/api/register", content);
|
||
var responseBody = await response.Content.ReadAsStringAsync();
|
||
log.Info($"注册请求已发送,响应内容: {responseBody}");
|
||
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
||
if (signresponse!.success)
|
||
{
|
||
// 显示 MainWindow
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
var mainWindow = Application.Current.Windows
|
||
.OfType<MainWindow>()
|
||
.FirstOrDefault();
|
||
});
|
||
log.Info($"注册成功: {signresponse.message}");
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
Login(true, Username, Userpassword);
|
||
});
|
||
}
|
||
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("注册请求已完成");
|
||
}
|
||
}
|
||
//var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||
//loginWindow?.Close();
|
||
public static void Login(bool Sign,string Username, string Userpassword)
|
||
{
|
||
if (Sign)
|
||
{
|
||
var LoginData = new
|
||
{
|
||
type = "login",
|
||
username = Username,
|
||
password = Userpassword
|
||
};
|
||
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
||
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
|
||
log.Info($"向服务器发送登录请求(UserName:{UserName})");
|
||
if (MainWindow.Client is not null)
|
||
{
|
||
if (MainWindow.Client.Connected)
|
||
{
|
||
MainWindow.Client.Send(dataBytes);
|
||
}
|
||
else
|
||
{
|
||
try
|
||
{
|
||
log.Info("未连接服务器,尝试连接");
|
||
MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
MainWindow.Client.Connect(System.Net.IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
log.Error(ex);
|
||
MainWindow.Client.Close();
|
||
}
|
||
finally
|
||
{
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||
loginWindow!.SignMsg = "在完成注册后与服务器断开连接\n请尝试在登录窗口重新登录";
|
||
});
|
||
//"服务器连接失败";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var LoginData = new
|
||
{
|
||
type = "login",
|
||
username = Username,
|
||
password = Userpassword
|
||
};
|
||
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
||
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
|
||
log.Info($"向服务器发送登录请求(UserName:{UserName})");
|
||
if (MainWindow.Client is not null)
|
||
{
|
||
if (MainWindow.Client.Connected)
|
||
{
|
||
MainWindow.Client.Send(dataBytes);
|
||
}
|
||
else
|
||
{
|
||
try
|
||
{
|
||
log.Info("未连接服务器,尝试连接");
|
||
MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
MainWindow.Client.Connect(System.Net.IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
log.Error(ex);
|
||
MainWindow.Client.Close();
|
||
}
|
||
finally
|
||
{
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||
if (loginWindow != null)
|
||
{
|
||
loginWindow.LoginMsg = "服务器连接失败";
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
public void Window_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
// 窗口加载时可以进行一些初始化操作
|
||
log.Info("登录窗口已加载");
|
||
// 如果需要从配置文件或其他地方加载保存的账号信息,可以在这里实现
|
||
// 例如:UserName = LoadSavedUsername();
|
||
// Update("UserName");
|
||
}
|
||
}
|
||
}
|
||
|