From ab98cf30c1c56b71c03e4e758a025a06ad9f78c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Wed, 30 Aug 2023 08:24:32 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=E5=BC=BA=E9=80=80=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/System/SysLoginController.cs | 21 ++++------------ .../System/monitor/SysUserOnlineController.cs | 24 +++++++++---------- ZR.Admin.WebApi/Hubs/HubsConstant.cs | 1 + .../System/IService/ISysLoginService.cs | 8 +++++-- ZR.Service/System/SysLoginService.cs | 15 +++++++++++- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs index d6511a3..5321374 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs @@ -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) { diff --git a/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs b/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs index 1f704b9..80b3222 100644 --- a/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs +++ b/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs @@ -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 } /// - /// 单条强退 + /// 单个强退 /// /// - [HttpDelete("lock")] + [HttpDelete("force")] [Log(Title = "强退", BusinessType = BusinessType.FORCE)] [ActionPermissionFilter(Permission = "monitor:online:forceLogout")] - public async Task Lock([FromBody] LockUserDto dto) + public async Task 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); } /// /// 批量强退 /// /// - [HttpDelete("batchlock")] + [HttpDelete("batchForce")] [Log(Title = "强退", BusinessType = BusinessType.FORCE)] [ActionPermissionFilter(Permission = "monitor:online:batchLogout")] - public async Task BatchLock([FromBody] LockUserDto dto) + public async Task 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); } diff --git a/ZR.Admin.WebApi/Hubs/HubsConstant.cs b/ZR.Admin.WebApi/Hubs/HubsConstant.cs index 8142dc3..35f9539 100644 --- a/ZR.Admin.WebApi/Hubs/HubsConstant.cs +++ b/ZR.Admin.WebApi/Hubs/HubsConstant.cs @@ -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"; } } diff --git a/ZR.Service/System/IService/ISysLoginService.cs b/ZR.Service/System/IService/ISysLoginService.cs index aa1c4d2..15d6ba8 100644 --- a/ZR.Service/System/IService/ISysLoginService.cs +++ b/ZR.Service/System/IService/ISysLoginService.cs @@ -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 + public interface ISysLoginService : IBaseService { /// /// 登录 @@ -40,5 +42,7 @@ namespace ZR.Service.System.IService /// /// public int DeleteLogininforByIds(long[] ids); + + void CheckLockUser(string userName); } } diff --git a/ZR.Service/System/SysLoginService.cs b/ZR.Service/System/SysLoginService.cs index bd25669..ec95094 100644 --- a/ZR.Service/System/SysLoginService.cs +++ b/ZR.Service/System/SysLoginService.cs @@ -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)}分钟"); + } + } + } }