404 lines
13 KiB
C#
404 lines
13 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Text.Json;
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
>>>>>>> ui编辑
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
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;
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.IO;
|
|||
|
using log4net;
|
|||
|
using log4net.Config;
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
using System.Windows.Interop;
|
|||
|
using chatapi;
|
|||
|
using System.Runtime.CompilerServices;
|
|||
|
|
|||
|
=======
|
|||
|
>>>>>>> ui编辑
|
|||
|
=======
|
|||
|
=======
|
|||
|
using System.Windows.Interop;
|
|||
|
using chatapi;
|
|||
|
using System.Runtime.CompilerServices;
|
|||
|
>>>>>>> 添加登录ui以及部分交互逻辑
|
|||
|
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
|
|||
|
namespace chatclient
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// LoginWindow.xaml 的交互逻辑
|
|||
|
/// </summary>
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
public partial class LoginWindow : Window, INotifyPropertyChanged
|
|||
|
{
|
|||
|
private static readonly ILog log = LogManager.GetLogger(typeof(LoginWindow));
|
|||
|
static string? receive;
|
|||
|
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? _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.Text = "用户名不能为空";
|
|||
|
}
|
|||
|
else if (UserPassword == "")
|
|||
|
{
|
|||
|
LoginMsg.Text = "密码不能为空";
|
|||
|
}
|
|||
|
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("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)
|
|||
|
{
|
|||
|
Thread th = new Thread(LoginReceive);
|
|||
|
th.Start();
|
|||
|
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 = "服务器连接失败或已断开连接";
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
static void LoginReceive()
|
|||
|
{
|
|||
|
byte[] buffer = new byte[1024];
|
|||
|
try
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
int num = MainWindow.Client!.Receive(buffer);
|
|||
|
if (num == 0) break;
|
|||
|
if (MainWindow.Client.Poll(100, SelectMode.SelectRead) && MainWindow.Client.Available == 0 || !MainWindow.Client.Connected)
|
|||
|
{
|
|||
|
log.Error("连接已断开");
|
|||
|
break;
|
|||
|
}
|
|||
|
receive = Encoding.UTF8.GetString(buffer, 0, num);
|
|||
|
response(receive);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
log.Error(ex);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
MainWindow.Client?.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
static void response(string msg)
|
|||
|
{
|
|||
|
LoginData data = JsonSerializer.Deserialize<LoginData>(msg)!;
|
|||
|
if (data.success)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//LoginWindow.Instance.LoginMsg.Text = msg;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
=======
|
|||
|
public partial class LoginWindow : Window
|
|||
|
=======
|
|||
|
public partial class LoginWindow : Window,INotifyPropertyChanged
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
=======
|
|||
|
public partial class LoginWindow : Window, INotifyPropertyChanged
|
|||
|
>>>>>>> 添加登录ui以及部分交互逻辑
|
|||
|
{
|
|||
|
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? _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.Text = "用户名不能为空";
|
|||
|
}
|
|||
|
else if (UserPassword == "")
|
|||
|
{
|
|||
|
LoginMsg.Text = "密码不能为空";
|
|||
|
}
|
|||
|
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("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 = "服务器连接失败或已断开连接";
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
<<<<<<< HEAD
|
|||
|
|
|||
|
>>>>>>> ui编辑
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
}
|
|||
|
}
|