milimoe 7061996177
修改架构 第二部分 (#16)
* 移除多余实现

* 修改方法名

* 更新连接服务器的逻辑

* 添加异常处理

* 修改架构之Login

* 修复IntoRoom Bug

* 修复ForceLogOut Bug

* 修复LogOut Bug

* 修复CreateRoom QuitRoom IntoRoom

* 断开连接后,应该关闭Socket连接

* 修复Reg Main Bug
2023-09-12 00:23:35 +08:00

192 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Core.Library.SQLScript.Common;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.Model;
using Milimoe.FunGame.Desktop.UI;
namespace Milimoe.FunGame.Desktop.Controller
{
public class LoginController
{
private readonly GeneralForm UIForm;
public LoginController(GeneralForm form)
{
UIForm = form;
}
public async Task<bool> LoginAccountAsync(string username, string password, string autokey = "")
{
bool result = false;
string msg = "";
try
{
password = password.Encrypt(username);
LoginEventArgs args = new(username, password, autokey);
if (OnBeforeLoginEvent(args))
{
DataRequest request = RunTime.NewLongRunningDataRequest(DataRequestType.Login_Login);
request.AddRequestData("username", username);
request.AddRequestData("password", password);
request.AddRequestData("autokey", autokey);
request.AddRequestData("key", Guid.Empty);
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
Guid key = request.GetResult<Guid>("key");
msg = request.GetResult<string>("msg") ?? "";
if (msg != "")
{
UIForm.ShowMessage(ShowMessageType.Error, msg);
}
else if (key != Guid.Empty)
{
request.AddRequestData("key", key);
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
msg = request.GetResult<string>("msg") ?? "";
if (msg != "")
{
UIForm.ShowMessage(ShowMessageType.Error, msg);
}
else
{
User user = request.GetResult<User>("user") ?? Factory.GetUser();
if (user.Id != 0)
{
// 创建User对象并返回到Main
RunTime.Session.LoginKey = key;
RunTime.Main?.UpdateUI(MainInvokeType.SetUser, user);
result = true;
}
}
}
}
}
request.Dispose();
}
if (!result && msg == "")
{
UIForm.ShowMessage(ShowMessageType.Error, "登录失败!");
}
OnAfterLoginEvent(result, args);
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
return result;
}
public static async Task<string> ForgetPassword_CheckVerifyCodeAsync(string username, string email, string verifycode)
{
string msg = "无法找回您的密码,请稍后再试。";
try
{
DataRequest request = RunTime.NewLongRunningDataRequest(DataRequestType.Login_GetFindPasswordVerifyCode);
request.AddRequestData(ForgetVerifyCodes.Column_Username, username);
request.AddRequestData(ForgetVerifyCodes.Column_Email, email);
if (verifycode.Trim() == "")
{
// 未发送verifycode说明需要系统生成一个验证码
request.AddRequestData(ForgetVerifyCodes.Column_ForgetVerifyCode, "");
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
msg = request.GetResult<string>("msg") ?? msg;
}
else RunTime.WritelnSystemInfo(request.Error);
}
else
{
// 发送verifycode需要验证
request.AddRequestData(ForgetVerifyCodes.Column_ForgetVerifyCode, verifycode);
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
msg = request.GetResult<string>("msg") ?? msg;
}
else RunTime.WritelnSystemInfo(request.Error);
}
request.Dispose();
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
return msg;
}
public static async Task<string> ForgetPassword_UpdatePasswordAsync(string username, string password)
{
string msg = "无法更新您的密码,请稍后再试。";
try
{
DataRequest request = RunTime.NewLongRunningDataRequest(DataRequestType.Login_UpdatePassword);
request.AddRequestData(UserQuery.Column_Username, username);
request.AddRequestData(UserQuery.Column_Password, password.Encrypt(username));
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
msg = request.GetResult<string>("msg") ?? msg;
}
else RunTime.WritelnSystemInfo(request.Error);
request.Dispose();
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
return msg;
}
private bool OnBeforeLoginEvent(LoginEventArgs LoginEventArgs)
{
if (UIForm.GetType() == typeof(Login))
{
return ((Login)UIForm).OnBeforeLoginEvent(LoginEventArgs) == EventResult.Success;
}
return true;
}
private void OnAfterLoginEvent(bool result, LoginEventArgs LoginEventArgs)
{
try
{
if (UIForm.GetType() == typeof(Login))
{
Login login = (Login)UIForm;
if (result) login.OnSucceedLoginEvent(LoginEventArgs);
else login.OnFailedLoginEvent(LoginEventArgs);
login.OnAfterLoginEvent(LoginEventArgs);
}
else if (UIForm.GetType() == typeof(Main))
{
if (result) ((Main)UIForm).OnSucceedLoginEvent(LoginEventArgs);
}
}
catch (Exception e)
{
RunTime.WritelnSystemInfo(e.GetErrorInfo());
}
}
}
}