//
//
//
using ERP.Framework.Cache;
using ERP.Framework.Config;
using ERP.Framework.Constants;
using ERP.Framework.Emum;
using ERP.Framework.Security.Core;
using ERP.Framework.Utils;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace ERP.Framework.Security
{
public class LoginHelper
{
///
/// 根据设备进行登录
///
///
///
///
///
///
public static string LoginByDevice(
LoginUser userInfo
, SecurityConfig securityConfig
, DeviceEnum device)
{
string tokenId;
var tokenList = GetTokenList(userInfo.UserId);
var result = TokenHelper.CreateToken(
securityConfig.JwtSecurityKey
, userInfo.UserId
, userInfo.UserName
, securityConfig.TimeOut
, out tokenId);
//Todo 不允许并发登录处理
HandleExceedToken(tokenList, securityConfig.MaxLoginCount);
tokenList.Add(new TokenSign
{
TokenId = tokenId,
Device = device
});
SetTokenList(userInfo.UserId, tokenList);
SetLastActivity(tokenId, securityConfig.ActivityTimeOut, securityConfig.TimeOut);
SetUserInfo(tokenId, userInfo, securityConfig.TimeOut);
return result;
}
///
/// 获取当前用户
///
///
///
public static LoginUser GetLoginUser()
{
var httpContextAccessor = new HttpContextAccessor();
var httpContext = httpContextAccessor.HttpContext;
var loginUser = httpContext!.Items["LoginUser"] as LoginUser;
if (loginUser != null)
{
return loginUser;
}
var tokenId = httpContext!.Items["TokenId"] as string;
var redisData = RedisHelper.Get(AuthConstant.USER_INFO + tokenId);
var result = JsonConvert.DeserializeObject(redisData);
if (result != null)
{
httpContext.Items["LoginUser"] = result;
return result;
}
else
{
// Todo 抛出异常处理
throw new Exception("");
}
}
///
/// 获取当前用户Id
///
///
public static long? GetUserId()
{
var httpContextAccessor = new HttpContextAccessor();
var httpContext = httpContextAccessor.HttpContext;
var result = httpContext!.Items["UserId"] as string;
return result!.ToLong();
}
///
/// 获取TokenList缓存
///
///
///
private static List GetTokenList(long userId)
{
var tokenListData = RedisHelper.Get(AuthConstant.TOKEN_LIST + CryptoUtil.MD5(userId.ToString()));
if (!tokenListData.IsNullOrEmpty())
{
var data = JsonConvert.DeserializeObject>(tokenListData)!;
return data;
}
return new List();
}
///
/// 创建Last Activity
///
///
///
private static void SetLastActivity(string tokenId, int activityTimeOut, int tokenTimeOut)
{
var key = AuthConstant.LAST_ACTIVITY + tokenId;
var val = DateTime.Now.AddMinutes(activityTimeOut).ToDateLongString();
RedisHelper.SaveExpire(key, val, tokenTimeOut);
}
///
/// 处理超出的会话
///
///
///
private static void HandleExceedToken(List list, SecurityConfig config)
{
if (list.Count > config.MaxLoginCount)
{
var num = list.Count - config.MaxLoginCount;
var substituteList = list.Take(num).ToList();
foreach (var item in substituteList)
{
SubstitutedUser(item.TokenId, config.TimeOut);
}
list.RemoveRange(0, num);
}
}
///
/// 顶下用户
///
///
///
private static void SubstitutedUser(string tokenId, int tokenTimeOut)
{
var key = AuthConstant.LAST_ACTIVITY + tokenId;
RedisHelper.SaveExpire(key, AuthConstant.SUBSTITUTED, tokenTimeOut);
}
///
/// 创建Token List缓存
///
///
///
private static void SetTokenList(long userId, List list)
{
var key = AuthConstant.TOKEN_LIST + CryptoUtil.MD5(userId.ToString());
var val = JsonConvert.SerializeObject(list);
RedisHelper.Save(key, val);
}
///
/// 创建UserInfo
///
///
///
private static void SetUserInfo(string tokenId, LoginUser loginUser, int tokenTimeOut)
{
var key = AuthConstant.USER_INFO + tokenId;
var val = JsonConvert.SerializeObject(loginUser);
RedisHelper.SaveExpire(key, val, tokenTimeOut);
}
}
}