document/公司/工作/云游戏/Untitled-4.ini
2024-12-04 14:23:41 +08:00

162 lines
6.2 KiB
INI
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.

/// <summary>
///
/// </summary>
/// <returns></returns>
public async Task<AccountLogInResponse> Login()
{
var json = await new StreamReader(this.HttpContextAccessor.HttpContext.Request.Body).ReadToEndAsync();
if (string.IsNullOrEmpty(json))
{
throw MessageBox.Show(ResonseCode.NullOrEmpty, "登录方式不合格");
}
var account = AccountExtend.GetUserAccount(json, this);
if (account == null)
{
throw MessageBox.Show(ResonseCode.NullOrEmpty, "未找到登录方式");
}
var userId = await account.LoginAsync();
T_User? user = null;
if (userId > 0)
{
user = await Dao.DaoUser.Context.T_User.FirstOrDefaultAsync(it => it.Id == userId);
}
var ip = this.HttpContextAccessor.HttpContext.GetClientIpAddress();
if (user == null)
{
//注册用户
user = new T_User()
{
CreatedAt = DateTime.Now,
LastLoginAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsTest = false,
LastLoginType = account.LastLoginType,
RegisterType = account.LastLoginType,
State = 0,
UserIconUrl = AppConfig.UserConfig.UserIconUrl,
NickName = $"{AppConfig.UserConfig.NickName}{new Random().Next(1000, 9999)}",
Ip = ip
};
await Dao.DaoUser.Context.T_User.AddAsync(user);
}
if (user == null)
{
throw MessageBox.Show(ResonseCode.Error, "用户注册失败");
}
user.LastLoginAt = DateTime.Now;
user.UpdatedAt = DateTime.Now;
user.Ip = ip;
await Dao.DaoUser.Context.SaveChangesAsync();
var userData = await Dao.DaoUser.Context.T_User_Data.FirstOrDefaultAsync(it => it.UserId == user.Id);
if (userData == null)
{
userData = new T_User_Data()
{
CreateAt = DateTime.Now,
UpdateAt = DateTime.Now,
PhoneNum = account.GetUserDataProperty(UserDataPropertyEnum.PhoneNum),
UserId = user.Id,
Email = account.GetUserDataProperty(UserDataPropertyEnum.Email)
};
await Dao.DaoUser.Context.T_User_Data.AddAsync(userData);
await Dao.DaoUser.Context.SaveChangesAsync();
}
var claims = new[]
{
new Claim("NickName",user.NickName),
new Claim("UserId",user.Id.ToString()),
};
var jwt = JwtAuthManager.GenerateTokens(user.NickName, claims, DateTime.Now);
var accountLogIn = new AccountLogInResponse()
{
NickName = user.NickName,
Token = jwt.AccessToken,
UserId = user.Id,
};
//获取用户登录的设备
var userLoginList = await Dao.DaoUser.Context.T_User_Token.Where(it => it.UserId == user.Id).ToListAsync();
if (userLoginList == null)
{
//用户没有登录过
userLoginList = new List<T_User_Token>();
}
var dev = account.DeviceNumber;
//如果设备号为空则使用用户Id+登录方式
if (string.IsNullOrEmpty(dev))
{
dev = MD5Encryption.ComputeMD5Hash($"{user.Id}:{account.LastLoginType}");
}
// 获取当前时间用于后续多次调用
var currentTime = DateTime.Now;
// 获取当前设备号,若为空则生成一个唯一的设备号
var dev = string.IsNullOrEmpty(account.DeviceNumber)
? MD5Encryption.ComputeMD5Hash($"{user.Id}:{account.LastLoginType}")
: account.DeviceNumber;
// 获取用户的设备列表
var userLoginList = await Dao.DaoUser.Context.T_User_Token
.Where(it => it.UserId == user.Id)
.OrderBy(it => it.LastLoginAt)
.ToListAsync();
// 如果当前登录设备数超过配置的最大设备数
if (userLoginList.Count > AppConfig.UserConfig.MaxDeviceCount)
{
// 删除多余设备记录,保留最新的设备
var excessDevices = userLoginList.Take(userLoginList.Count - AppConfig.UserConfig.MaxDeviceCount).ToList();
Dao.DaoUser.Context.T_User_Token.RemoveRange(excessDevices);
await Dao.DaoUser.Context.SaveChangesAsync();
}
// 查找当前设备记录
var existingDevice = userLoginList.FirstOrDefault(it => it.DeviceNumber == dev);
if (existingDevice == null)
{
// 情况1当前设备不存在且设备数已达最大值
if (userLoginList.Count == AppConfig.UserConfig.MaxDeviceCount)
{
// 踢掉最早登录的设备,将其替换为当前设备
var oldestDevice = userLoginList.First();
oldestDevice.DeviceNumber = dev;
oldestDevice.TokenMd5 = MD5Encryption.ComputeMD5Hash(jwt.AccessToken);
oldestDevice.LastLoginAt = currentTime;
oldestDevice.ExpiresAt = currentTime.AddDays(5);
oldestDevice.Token = jwt.AccessToken;
}
else
{
// 情况2当前设备不存在且设备数小于最大值新增一条设备记录
var newDevice = new T_User_Token
{
CreateAt = currentTime,
ExpiresAt = currentTime.AddDays(5),
LastLoginAt = currentTime,
Token = jwt.AccessToken,
UserId = user.Id,
DeviceNumber = dev,
TokenMd5 = MD5Encryption.ComputeMD5Hash(jwt.AccessToken)
};
await Dao.DaoUser.Context.T_User_Token.AddAsync(newDevice);
}
}
else
{
// 情况3当前设备已存在更新登录时间等参数
existingDevice.LastLoginAt = currentTime;
existingDevice.ExpiresAt = currentTime.AddDays(5);
existingDevice.Token = jwt.AccessToken;
existingDevice.TokenMd5 = MD5Encryption.ComputeMD5Hash(jwt.AccessToken);
}
// 保存更改
await Dao.DaoUser.Context.SaveChangesAsync();
return accountLogIn;
}
{"phoneNumber":"17521010998","verificationCode":"1645"}