ChatX/chatclient/LoginWindow.xaml.cs

175 lines
5.8 KiB
C#
Raw Normal View History

2025-06-01 02:57:57 +08:00
using System;
using System.Collections.Generic;
2025-06-01 20:33:52 +08:00
using System.ComponentModel;
2025-06-01 02:57:57 +08:00
using System.Linq;
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;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
2025-06-01 20:33:52 +08:00
using System.Net.Sockets;
using System.IO;
using log4net;
using log4net.Config;
2025-06-01 22:34:58 +08:00
using System.Windows.Interop;
using chatapi;
using System.Runtime.CompilerServices;
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; } = "";
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
}
}
private void Login_Click(object sender, RoutedEventArgs e)
{
if (UserName == "")
{
2025-06-01 22:34:58 +08:00
LoginMsg.Text = "用户名不能为空";
2025-06-01 20:33:52 +08:00
}
else if (UserPassword == "")
{
2025-06-01 22:34:58 +08:00
LoginMsg.Text = "密码不能为空";
2025-06-01 20:33:52 +08:00
}
else
{
var LoginData = new
{
type = "login",
username = UserName,
password = UserPassword
};
2025-06-01 22:34:58 +08:00
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("127.0.0.1", 5555);
}
catch (Exception ex)
{
log.Error(ex);
MainWindow.Client.Close();
}
finally
{
LoginMsg.Text = "服务器连接失败或已断开连接";
}
}
}
}
}
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
{
var SignData = new
{
type = "register",
username = SignName,
password = SignPassword1
};
string SignJsonData = JsonSerializer.Serialize(SignData);
byte[] dataBytes = Encoding.UTF8.GetBytes(SignJsonData);
log.Info($"向服务器发送注册请求UserName:{SignName}");
Update("LoginMsg");
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("127.0.0.1", 5555);
}
catch (Exception ex)
{
log.Error(ex);
MainWindow.Client.Close();
}
finally
{
SignMsg = "服务器连接失败或已断开连接";
}
}
}
2025-06-01 20:33:52 +08:00
}
2025-06-01 02:57:57 +08:00
}
}
}