使用Async/Await登录

This commit is contained in:
Mili 2023-03-29 22:32:49 +08:00
parent 5a8a34a68e
commit 255e76386c
7 changed files with 92 additions and 92 deletions

View File

@ -237,6 +237,7 @@
Connected,
Disconnect,
Disconnected,
AutoLogin,
Close
}

View File

@ -2,6 +2,7 @@
using Milimoe.FunGame.Core.Library.Common.Architecture;
using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Model;
using Milimoe.FunGame.Core.Library.Exception;
namespace Milimoe.FunGame.Desktop.Controller
{
@ -19,14 +20,26 @@ namespace Milimoe.FunGame.Desktop.Controller
LoginModel.Dispose();
}
public static bool LoginAccount(params object[]? objs)
public static async Task<bool> LoginAccount(params object[]? objs)
{
LoginEventArgs LoginEventArgs = new(objs);
if (RunTime.Login?.OnBeforeLoginEvent(LoginEventArgs) == Core.Library.Constant.EventResult.Fail) return false;
bool result = LoginModel.LoginAccountAsync(objs).Result;
if (result) RunTime.Login?.OnSucceedLoginEvent(LoginEventArgs);
else RunTime.Login?.OnFailedLoginEvent(LoginEventArgs);
RunTime.Login?.OnAfterLoginEvent(LoginEventArgs);
bool result = false;
try
{
LoginEventArgs LoginEventArgs = new(objs);
if (RunTime.Login?.OnBeforeLoginEvent(LoginEventArgs) == Core.Library.Constant.EventResult.Fail) return false;
result = await LoginModel.LoginAccountAsync(objs);
if (result) RunTime.Login?.OnSucceedLoginEvent(LoginEventArgs);
else RunTime.Login?.OnFailedLoginEvent(LoginEventArgs);
RunTime.Login?.OnAfterLoginEvent(LoginEventArgs);
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
return result;
}
}

View File

@ -1,5 +1,6 @@
using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Model;
using Milimoe.FunGame.Desktop.UI;
@ -51,6 +52,9 @@ namespace Milimoe.FunGame.Desktop.Controller
case RunTimeInvokeType.Disconnected:
break;
case RunTimeInvokeType.AutoLogin:
break;
case RunTimeInvokeType.Close:
if (args != null && args.Length > 0)
{
@ -96,5 +100,13 @@ namespace Milimoe.FunGame.Desktop.Controller
{
return Do<bool>(RunTimeInvokeType.Close, e);
}
public async void AutoLogin(params object[] objs)
{
Do<object>(RunTimeInvokeType.AutoLogin);
LoginController LoginController = new();
await LoginController.LoginAccount(objs);
LoginController.Dispose();
}
}
}

View File

@ -1,4 +1,5 @@
using System.Data;
using System.Windows.Forms;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Architecture;
@ -55,17 +56,25 @@ namespace Milimoe.FunGame.Desktop.Model
if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success)
{
SetWorking();
await Task.Factory.StartNew(async() =>
string ErrorMsg = "";
Guid CheckLoginKey = Guid.Empty;
(CheckLoginKey, ErrorMsg) = await Task.Factory.StartNew(GetCheckLoginKeyAsync);
if (ErrorMsg != null && ErrorMsg.Trim() != "")
{
while (true)
ShowMessage.ErrorMessage(ErrorMsg, "登录失败");
return false;
}
if (Socket.Send(SocketMessageType.CheckLogin, CheckLoginKey) == SocketResult.Success)
{
SetWorking();
DataSet ds = await Task.Factory.StartNew(GetLoginUserAsync);
if (ds != null)
{
if (!Working) break;
// 创建User对象并返回到Main
RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance<User>(ds) });
return true;
}
if (Success)
{
await CheckLoginAsync(Work);
}
});
}
}
}
}
@ -77,86 +86,56 @@ namespace Milimoe.FunGame.Desktop.Model
return false;
}
public static bool CheckLogin(params object[]? objs)
{
try
{
Socket? Socket = RunTime.Socket;
if (Socket != null && objs != null)
{
Guid key = Guid.Empty;
if (objs.Length > 0) key = (Guid)objs[0];
if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success)
{
return true;
}
}
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
return false;
}
private static async Task<bool> CheckLoginAsync(SocketObject SocketObject)
public static (Guid, string) GetCheckLoginKeyAsync()
{
Guid key = Guid.Empty;
string? msg = "";
// 返回一个Key再发回去给服务器就行了
if (SocketObject.Length > 0) key = SocketObject.GetParam<Guid>(0);
if (SocketObject.Length > 1) msg = SocketObject.GetParam<string>(1);
// 如果返回了msg说明验证错误。
if (msg != null && msg.Trim() != "")
try
{
ShowMessage.ErrorMessage(msg, "登录失败");
return false;
}
else
{
if (key != Guid.Empty)
while (true)
{
Config.Guid_LoginKey = key;
Socket Socket = RunTime.Socket!;
if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success)
if (!Working) break;
}
if (Success)
{
// 返回一个确认登录的Key
if (Work.Length > 0) key = Work.GetParam<Guid>(0);
if (Work.Length > 1) msg = Work.GetParam<string>(1);
if (key != Guid.Empty)
{
SetWorking();
await Task.Factory.StartNew(() =>
{
while (true)
{
if (!Working) break;
}
if (Success)
{
SocketHandler_CheckLogin(Work);
}
});
return true;
Config.Guid_LoginKey = key;
}
}
else
{
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
return false;
}
}
return true;
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
msg ??= "";
return (key, msg);
}
private static bool SocketHandler_CheckLogin(SocketObject SocketObject)
private static DataSet GetLoginUserAsync()
{
// 返回构造User对象的DataTable
object[] objs = SocketObject.Parameters;
if (objs != null && objs.Length > 0)
DataSet? ds = new();
try
{
DataSet? DataSet = SocketObject.GetParam<DataSet>(0);
// 创建User对象并返回到Main
RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance<User>(DataSet) });
return true;
while (true)
{
if (!Working) break;
}
if (Success)
{
// 返回构造User对象的DataTable
if (Work.Length > 0) ds = Work.GetParam<DataSet>(0);
}
}
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
return false;
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
ds ??= new();
return ds;
}
private static void SetWorking()

View File

@ -34,7 +34,7 @@ namespace Milimoe.FunGame.Desktop.UI
LoginController.Dispose();
}
private bool Login_Handler()
private async Task<bool> Login_Handler()
{
try
{
@ -46,18 +46,13 @@ namespace Milimoe.FunGame.Desktop.UI
UsernameText.Focus();
return false;
}
if (!LoginController.LoginAccount(username, password))
{
ShowMessage.Message("登录失败!!", "登录失败");
return false;
}
return await LoginController.LoginAccount(username, password);
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
return false;
}
return true;
}
/// <summary>
@ -75,10 +70,10 @@ namespace Milimoe.FunGame.Desktop.UI
ShowMessage.TipMessage("与No.16对话即可获得快速登录秘钥,快去试试吧!");
}
private void GoToLogin_Click(object sender, EventArgs e)
private async void GoToLogin_Click(object sender, EventArgs e)
{
GoToLogin.Enabled = false;
if (!Login_Handler()) GoToLogin.Enabled = true;
if (await Login_Handler() == false) GoToLogin.Enabled = true;
}
private void ForgetPassword_Click(object sender, EventArgs e)

View File

@ -1204,7 +1204,7 @@ namespace Milimoe.FunGame.Desktop.UI
if (MainController != null && Config.FunGame_isAutoLogin && Config.FunGame_AutoLoginUser != "" && Config.FunGame_AutoLoginPassword != "" && Config.FunGame_AutoLoginKey != "")
{
// 自动登录
LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey);
RunTime.Connector?.AutoLogin(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey);
}
return EventResult.Success;
}

View File

@ -165,7 +165,7 @@ namespace Milimoe.FunGame.Desktop.UI
{
string username = ((RegisterEventArgs)e).Username;
string password = ((RegisterEventArgs)e).Password;
LoginController.LoginAccount(username, password);
RunTime.Connector?.AutoLogin(username, password);
return EventResult.Success;
}