使用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, Connected,
Disconnect, Disconnect,
Disconnected, Disconnected,
AutoLogin,
Close Close
} }

View File

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

View File

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

View File

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

View File

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