强退功能优化

This commit is contained in:
不做码农 2023-08-30 08:24:32 +08:00
parent 4d914ead0e
commit ab98cf30c1
5 changed files with 37 additions and 32 deletions

View File

@ -16,7 +16,7 @@ namespace ZR.Admin.WebApi.Controllers.System
[ApiExplorerSettings(GroupName = "sys")]
public class SysLoginController : BaseController
{
static readonly NLog.Logger logger = NLog.LogManager.GetLogger("LoginController");
//static readonly NLog.Logger logger = NLog.LogManager.GetLogger("LoginController");
private readonly IHttpContextAccessor httpContextAccessor;
private readonly ISysUserService sysUserService;
private readonly ISysMenuService sysMenuService;
@ -68,14 +68,7 @@ namespace ZR.Admin.WebApi.Controllers.System
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
}
var lockTimeStamp = CacheService.GetLockUser(loginBody.ClientId + loginBody.Username);
var lockTime = DateTimeHelper.ToLocalTimeDateBySeconds(lockTimeStamp);
var ts = lockTime - DateTime.Now;
if (lockTimeStamp > 0 && ts.TotalSeconds > 0)
{
return ToResponse(ResultCode.LOGIN_ERROR, $"你的账号已被锁,剩余{Math.Round(ts.TotalMinutes, 0)}分钟");
}
sysLoginService.CheckLockUser(loginBody.Username);
string location = HttpContextExtension.GetIpInfo(loginBody.LoginIP);
var user = sysLoginService.Login(loginBody, new SysLogininfor() { LoginLocation = location });
@ -252,15 +245,9 @@ namespace ZR.Admin.WebApi.Controllers.System
{
if (dto == null) { return ToResponse(ResultCode.CUSTOM_ERROR, "扫码失败"); }
var name = App.HttpContext.GetName();
var lockTimeStamp = CacheService.GetLockUser(dto.DeviceId + name);
var lockTime = DateTimeHelper.ToLocalTimeDateBySeconds(lockTimeStamp);
var ts = lockTime - DateTime.Now;
sysLoginService.CheckLockUser(name);
if (lockTimeStamp > 0 && ts.TotalSeconds > 0)
{
return ToResponse(ResultCode.LOGIN_ERROR, $"当前设备已被锁,剩余{Math.Round(ts.TotalMinutes, 0)}分钟");
}
//var token = HttpContextExtension.GetToken(HttpContext);
TokenModel tokenModel = JwtUtil.GetLoginUser(HttpContext);
if (CacheService.GetScanLogin(dto.Uuid) is not null)
{

View File

@ -4,7 +4,6 @@ using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Hubs;
using ZR.Model;
using ZR.Model.System.Dto;
using ZR.Service.System;
namespace ZR.Admin.WebApi.Controllers.monitor
{
@ -37,36 +36,37 @@ namespace ZR.Admin.WebApi.Controllers.monitor
}
/// <summary>
/// 单强退
/// 单强退
/// </summary>
/// <returns></returns>
[HttpDelete("lock")]
[HttpDelete("force")]
[Log(Title = "强退", BusinessType = BusinessType.FORCE)]
[ActionPermissionFilter(Permission = "monitor:online:forceLogout")]
public async Task<IActionResult> Lock([FromBody] LockUserDto dto)
public async Task<IActionResult> Force([FromBody] LockUserDto dto)
{
if (dto == null) { return ToResponse(ResultCode.PARAM_ERROR); }
await HubContext.Clients.Client(dto.ConnnectionId).SendAsync(HubsConstant.LockUser, new { dto.Reason, dto.Time });
await HubContext.Clients.Client(dto.ConnnectionId)
.SendAsync(HubsConstant.ForceUser, new { dto.Reason, dto.Time });
var expirTime = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now.AddMinutes(dto.Time));
//PC 端采用设备 + 用户名的方式进行封锁
CacheService.SetLockUser(dto.ClientId + dto.Name, expirTime, dto.Time);
return SUCCESS(new { expirTime });
//var expirTime = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now.AddMinutes(dto.Time));
////PC 端采用设备 + 用户名的方式进行封锁
//CacheService.SetLockUser(dto.ClientId + dto.Name, expirTime, dto.Time);
return SUCCESS(1);
}
/// <summary>
/// 批量强退
/// </summary>
/// <returns></returns>
[HttpDelete("batchlock")]
[HttpDelete("batchForce")]
[Log(Title = "强退", BusinessType = BusinessType.FORCE)]
[ActionPermissionFilter(Permission = "monitor:online:batchLogout")]
public async Task<IActionResult> BatchLock([FromBody] LockUserDto dto)
public async Task<IActionResult> BatchforceLogout([FromBody] LockUserDto dto)
{
if (dto == null) { return ToResponse(ResultCode.PARAM_ERROR); }
await HubContext.Clients.All.SendAsync(HubsConstant.LockUser, new { dto.Reason, dto.Time });
await HubContext.Clients.All.SendAsync(HubsConstant.ForceUser, new { dto.Reason });
return SUCCESS(1);
}

View File

@ -8,6 +8,7 @@
public static string MoreNotice = "moreNotice";
public static string OnlineUser = "onlineUser";
public static string LockUser = "lockUser";
public static string ForceUser = "forceUser";
public static string ConnId = "connId";
}
}

View File

@ -1,10 +1,12 @@
using ZR.Model;
using Infrastructure;
using System;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
namespace ZR.Service.System.IService
{
public interface ISysLoginService: IBaseService<SysLogininfor>
public interface ISysLoginService : IBaseService<SysLogininfor>
{
/// <summary>
/// 登录
@ -40,5 +42,7 @@ namespace ZR.Service.System.IService
/// <param name="ids"></param>
/// <returns></returns>
public int DeleteLogininforByIds(long[] ids);
void CheckLockUser(string userName);
}
}

View File

@ -49,7 +49,7 @@ namespace ZR.Service.System
ClientInfo clientInfo = httpContextAccessor.HttpContext.GetClientInfo();
logininfor.Browser = clientInfo.ToString();
logininfor.Os = clientInfo.OS.ToString();
if (user == null || user.UserId <= 0)
{
logininfor.Msg = "用户名或密码错误";
@ -121,5 +121,18 @@ namespace ZR.Service.System
{
return Delete(ids);
}
public void CheckLockUser(string userName)
{
var lockTimeStamp = CacheService.GetLockUser(userName);
var lockTime = DateTimeHelper.ToLocalTimeDateBySeconds(lockTimeStamp);
var ts = lockTime - DateTime.Now;
if (lockTimeStamp > 0 && ts.TotalSeconds > 0)
{
throw new CustomException(ResultCode.LOGIN_ERROR, $"你的账号已被锁,剩余{Math.Round(ts.TotalMinutes, 0)}分钟");
}
}
}
}