using System.Text.Json; using OMS.NET.DbClass; namespace OMS.NET.Instructs { /// /// 登录指令 /// public class LoginInstruct : Instruct { public LoginInstruct() { Type = "login"; } public override Task Handler(string wsid) { return Task.Run(() => { if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 try { string email = Data.GetProperty("email").GetString(); string password = Data.GetProperty("password").GetString(); if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("登录信息不能为空"); } //能够获取到则说明客户端有发送数据过来 Instruct res1 = new() { IsResponse = true, Type = "loginStatus", Data = false };//默认为false //Console.WriteLine($"已获取到{email}:{password},解密测试{GlobalArea.DecryptFromBase64String(password)}"); AccountData accountData = AccountData.Get(email) ?? throw new Exception($"数据库中不包含用户{email}"); GlobalArea.AddLoginAccountData(accountData); ResponseOrBroadcastInstructs.Add(res1); //只能原文比较,密文每次都不一样,涉及随机性填充 if (accountData.Password == GlobalArea.DecryptFromBase64String(password)) { res1.Data = true;//登录成功则修改为true GlobalArea.Log.Info($"{accountData.UserEmail}:登录成功"); GlobalArea.Login(wsid, accountData.UserEmail); GlobalArea.Log.Info($"当前登录用户数量: {GlobalArea.LoginUserCount}"); ResponseOrBroadcastInstructs.Add(new Instruct { Type = "broadcast", Class = "logIn", Data = new { userEmail = accountData.UserEmail, userName = accountData.UserName, headColor = accountData.HeadColor, userQq = accountData.UserQQ, }, IsBroadcast = true }); } } catch (Exception ex) { GlobalArea.Log.Warn($"处理{Type}广播指令出错:" + ex.Message); } }); } } }