diff --git a/Infrastructure/Constant/HubsConstant.cs b/Infrastructure/Constant/HubsConstant.cs index d81b7f8..929872c 100644 --- a/Infrastructure/Constant/HubsConstant.cs +++ b/Infrastructure/Constant/HubsConstant.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Infrastructure.Constant +namespace Infrastructure.Constant { public class HubsConstant { @@ -13,5 +7,7 @@ namespace Infrastructure.Constant public static string OnlineNum = "onlineNum"; public static string MoreNotice = "moreNotice"; public static string OnlineUser = "onlineUser"; + public static string LockUser = "lockUser"; + public static string ConnId = "connId"; } } diff --git a/Infrastructure/Helper/DateTimeHelper.cs b/Infrastructure/Helper/DateTimeHelper.cs index 72076c4..bc07775 100644 --- a/Infrastructure/Helper/DateTimeHelper.cs +++ b/Infrastructure/Helper/DateTimeHelper.cs @@ -91,7 +91,7 @@ namespace Infrastructure #region 获取unix时间戳 /// - /// 获取unix时间戳 + /// 获取unix时间戳(毫秒) /// /// /// diff --git a/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs b/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs index 015f877..7501236 100644 --- a/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs +++ b/ZR.Admin.WebApi/Controllers/System/monitor/SysUserOnlineController.cs @@ -1,8 +1,11 @@ -using Microsoft.AspNetCore.Mvc; +using Infrastructure.Constant; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; 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 { @@ -12,9 +15,9 @@ namespace ZR.Admin.WebApi.Controllers.monitor [ApiExplorerSettings(GroupName = "sys")] public class SysUserOnlineController : BaseController { - private IHubContext HubContext; + private readonly IHubContext HubContext; - public SysUserOnlineController(IHubContext hubContext) + public SysUserOnlineController(IHubContext hubContext) { HubContext = hubContext; } @@ -33,5 +36,23 @@ namespace ZR.Admin.WebApi.Controllers.monitor return SUCCESS(new { result, totalNum = MessageHub.clientUsers.Count }); } + + /// + /// 强退 + /// + /// + [HttpDelete("lock")] + [Log(Title = "强退", BusinessType = BusinessType.FORCE)] + public async Task Lock([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 }); + + var expirTime = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now.AddMinutes(dto.Time)); + + CacheService.SetLockUser(dto.ClientId, expirTime, dto.Time); + return SUCCESS(new { expirTime }); + } } } diff --git a/ZR.Admin.WebApi/Hubs/MessageHub.cs b/ZR.Admin.WebApi/Hubs/MessageHub.cs index 356cd98..edef352 100644 --- a/ZR.Admin.WebApi/Hubs/MessageHub.cs +++ b/ZR.Admin.WebApi/Hubs/MessageHub.cs @@ -1,8 +1,7 @@ -using Infrastructure; -using Infrastructure.Constant; -using Infrastructure.Model; +using Infrastructure.Constant; using IPTools.Core; using Microsoft.AspNetCore.SignalR; +using System.Web; using UAParser; using ZR.Admin.WebApi.Extensions; using ZR.Service.System.IService; @@ -45,8 +44,11 @@ namespace ZR.Admin.WebApi.Hubs ClientInfo clientInfo = HttpContextExtension.GetClientInfo(App.HttpContext); string device = clientInfo.ToString(); + var qs = HttpContextExtension.GetQueryString(App.HttpContext); - var userid = HttpContextExtension.GetUId(App.HttpContext); + string from = HttpUtility.ParseQueryString(qs).Get("from") ?? "web"; + + long userid = HttpContextExtension.GetUId(App.HttpContext); string uuid = device + userid + ip; var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId); var user2 = clientUsers.Any(u => u.Uuid == uuid); @@ -54,18 +56,20 @@ namespace ZR.Admin.WebApi.Hubs //判断用户是否存在,否则添加集合!user2 && !user && if (!user2 && !user && Context.User.Identity.IsAuthenticated) { - OnlineUsers users = new(Context.ConnectionId, name, userid, ip, device) + OnlineUsers onlineUser = new(Context.ConnectionId, name, userid, ip, device) { Location = ip_info.City, - Uuid = uuid + Uuid = uuid, + Platform = from }; - clientUsers.Add(users); + clientUsers.Add(onlineUser); Console.WriteLine($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个"); //Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}"); - Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice()); + Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice()); + Clients.Caller.SendAsync(HubsConstant.ConnId, onlineUser.ConnnectionId); } - Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count); + Clients.Caller.SendAsync(HubsConstant.OnlineNum, clientUsers.Count); return base.OnConnectedAsync(); } @@ -90,7 +94,7 @@ namespace ZR.Admin.WebApi.Hubs #endregion /// - /// 注册信息 + /// 发送信息 /// /// /// @@ -103,5 +107,15 @@ namespace ZR.Admin.WebApi.Hubs await Clients.Client(connectId).SendAsync("receiveChat", new { userName, message }); } + + /// + /// 获取链接id + /// + /// + [HubMethodName("getConnId")] + public string GetConnectId() + { + return Context.ConnectionId; + } } } diff --git a/ZR.Admin.WebApi/Hubs/OnlineUsers.cs b/ZR.Admin.WebApi/Hubs/OnlineUsers.cs index cdfce13..81aed2d 100644 --- a/ZR.Admin.WebApi/Hubs/OnlineUsers.cs +++ b/ZR.Admin.WebApi/Hubs/OnlineUsers.cs @@ -26,6 +26,10 @@ /// 浏览器 /// public string Browser { get; set; } + /// + /// 平台 + /// + public string Platform { get; set; } = string.Empty; public OnlineUsers(string clientid, string name, long? userid, string userip, string browser) { diff --git a/ZR.Model/System/Dto/LockUserDto.cs b/ZR.Model/System/Dto/LockUserDto.cs new file mode 100644 index 0000000..c1219f4 --- /dev/null +++ b/ZR.Model/System/Dto/LockUserDto.cs @@ -0,0 +1,10 @@ +namespace ZR.Model.System.Dto +{ + public class LockUserDto + { + public string ClientId { get; set; } + public string ConnnectionId { get; set; } + public string Reason { get; set; } + public int Time { get; set; } + } +} diff --git a/ZR.Model/System/Dto/LoginBodyDto.cs b/ZR.Model/System/Dto/LoginBodyDto.cs index d5f2a66..ba408a4 100644 --- a/ZR.Model/System/Dto/LoginBodyDto.cs +++ b/ZR.Model/System/Dto/LoginBodyDto.cs @@ -26,5 +26,6 @@ namespace ZR.Model.System.Dto /// public string Uuid { get; set; } = ""; public string LoginIP { get; set; } + public string ClientId { get; set; } } }