From c38199268fadc25a717032248bce4cd76e595452 Mon Sep 17 00:00:00 2001 From: zhougf Date: Fri, 5 Dec 2025 21:30:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0SignalR=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在Program.cs中注册LoginHub - 在HomeController中传递用户ID以支持SignalR连接 - 在Index.cshtml中实现SignalR连接和心跳机制,处理强制登出和注册成功事件 - 移除旧的登录状态检查逻辑,优化页面加载时的连接管理 --- HHWMS.Web/Areas/Api/IWCSController.cs | 4 +- .../Views/LocationMonitor/Index.cshtml | 2 +- HHWMS.Web/Areas/Hub/LoginHub.cs | 104 +++++++++++ .../Controllers/InventoryController.cs | 2 +- HHWMS.Web/Controllers/HomeController.cs | 1 + HHWMS.Web/Program.cs | 1 + HHWMS.Web/Views/Home/FrontPage.cshtml | 2 +- HHWMS.Web/Views/Home/Index.cshtml | 171 +++++++++++++----- HHWMS.Web/Views/Login/Login.cshtml | 4 +- 9 files changed, 242 insertions(+), 49 deletions(-) create mode 100644 HHWMS.Web/Areas/Hub/LoginHub.cs diff --git a/HHWMS.Web/Areas/Api/IWCSController.cs b/HHWMS.Web/Areas/Api/IWCSController.cs index 1b253c7..52b01e1 100644 --- a/HHWMS.Web/Areas/Api/IWCSController.cs +++ b/HHWMS.Web/Areas/Api/IWCSController.cs @@ -476,10 +476,10 @@ namespace HHWMS.Web container.LocationCode = task.TaskType == TaskTypeEnum.出库 ? "" : task.ToLocation; container.IsLock = ContainerLockEnum.未锁; container.UpdateTime = DateTime.Now; + + _unitWork.Update(container); } - _unitWork.Update(container); - // 更新库位状态(区分入库、出库、移库) switch (task.TaskType) { diff --git a/HHWMS.Web/Areas/Configs/Views/LocationMonitor/Index.cshtml b/HHWMS.Web/Areas/Configs/Views/LocationMonitor/Index.cshtml index 8c0ae94..cf608ce 100644 --- a/HHWMS.Web/Areas/Configs/Views/LocationMonitor/Index.cshtml +++ b/HHWMS.Web/Areas/Configs/Views/LocationMonitor/Index.cshtml @@ -137,7 +137,7 @@ // 根据库区加载排号 function loadRows(area) { - console.log(area); + //console.log(area); if (!area) return; $.get('/configs/LocationMonitor/GetRows', { area: area }, function(data) { var rowSelect = $('#rowSelect'); diff --git a/HHWMS.Web/Areas/Hub/LoginHub.cs b/HHWMS.Web/Areas/Hub/LoginHub.cs new file mode 100644 index 0000000..8be026b --- /dev/null +++ b/HHWMS.Web/Areas/Hub/LoginHub.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Concurrent; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; + +namespace HHWMS.Web +{ + /// + /// 登录状态 Hub - 用于保活和异地登录检测 + /// + public class LoginHub : Hub + { + // 存储用户连接信息:用户ID -> 连接ID + private static readonly ConcurrentDictionary _userConnections = new(); + + // 存储连接对应的用户:连接ID -> 用户ID + private static readonly ConcurrentDictionary _connectionUsers = new(); + + /// + /// 用户登录注册连接 + /// + /// 用户ID + public async Task Register(string userId) + { + if (string.IsNullOrEmpty(userId)) + return; + + var connectionId = Context.ConnectionId; + + // 检查该用户是否已有其他连接 + if (_userConnections.TryGetValue(userId, out var oldConnectionId)) + { + if (oldConnectionId != connectionId) + { + // 通知旧连接被踢出 + await Clients.Client(oldConnectionId).SendAsync("ForceLogout", "您的账号已在其他设备登录"); + + // 移除旧连接记录 + _connectionUsers.TryRemove(oldConnectionId, out _); + } + } + + // 更新连接映射 + _userConnections[userId] = connectionId; + _connectionUsers[connectionId] = userId; + + // 通知当前客户端注册成功 + await Clients.Caller.SendAsync("RegisterSuccess", "连接成功"); + } + + /// + /// 心跳保活 + /// + public async Task Heartbeat(string userId) + { + if (string.IsNullOrEmpty(userId)) + return; + + var connectionId = Context.ConnectionId; + + // 检查是否是当前有效连接 + if (_userConnections.TryGetValue(userId, out var currentConnectionId)) + { + if (currentConnectionId != connectionId) + { + // 当前连接不是最新的,通知被踢出 + await Clients.Caller.SendAsync("ForceLogout", "您的账号已在其他设备登录"); + return; + } + } + + // 回复心跳 + await Clients.Caller.SendAsync("HeartbeatResponse", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + } + + /// + /// 连接断开时清理 + /// + public override Task OnDisconnectedAsync(Exception exception) + { + var connectionId = Context.ConnectionId; + + // 移除连接记录 + if (_connectionUsers.TryRemove(connectionId, out var userId)) + { + // 只有当前连接是该用户的有效连接时才移除用户映射 + if (_userConnections.TryGetValue(userId, out var currentConnectionId) && currentConnectionId == connectionId) + { + _userConnections.TryRemove(userId, out _); + } + } + + return base.OnDisconnectedAsync(exception); + } + + /// + /// 获取在线用户数量 + /// + public int GetOnlineCount() + { + return _userConnections.Count; + } + } +} diff --git a/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs b/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs index 48c015f..f772f91 100644 --- a/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs +++ b/HHWMS.Web/Areas/Inventory/Controllers/InventoryController.cs @@ -576,7 +576,7 @@ namespace HHWMS.Web.Controllers }); // 调用ERP接口 - var apiResult = await Wtm.CallAPI("ERP", "Api/FinishIn", HttpMethodEnum.POST, request); + var apiResult = await Wtm.CallAPI("ERP", "FinishIn", HttpMethodEnum.POST, request); if (apiResult.Data != null) { diff --git a/HHWMS.Web/Controllers/HomeController.cs b/HHWMS.Web/Controllers/HomeController.cs index f65d395..8193b83 100644 --- a/HHWMS.Web/Controllers/HomeController.cs +++ b/HHWMS.Web/Controllers/HomeController.cs @@ -27,6 +27,7 @@ namespace HHWMS.Web.Controllers public IActionResult Index(string redirect) { ViewData["title"] = "WMS"; + ViewData["ITCode"] = Wtm.LoginUserInfo?.ITCode; // 传递用户ID用于SignalR连接 return View(); } diff --git a/HHWMS.Web/Program.cs b/HHWMS.Web/Program.cs index 91b8985..9db063c 100644 --- a/HHWMS.Web/Program.cs +++ b/HHWMS.Web/Program.cs @@ -103,6 +103,7 @@ app.UseEndpoints(endpoints => name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapHub("/stocks"); + endpoints.MapHub("/loginHub"); }); // 添加以下代码以确保响应是UTF-8编码 app.Use(async (context, next) => diff --git a/HHWMS.Web/Views/Home/FrontPage.cshtml b/HHWMS.Web/Views/Home/FrontPage.cshtml index ace6602..5d507a1 100644 --- a/HHWMS.Web/Views/Home/FrontPage.cshtml +++ b/HHWMS.Web/Views/Home/FrontPage.cshtml @@ -56,7 +56,7 @@ initLocationChart(); initCurrentTaskChart(); }); - console.log(echarts); + //console.log(echarts); // 一周内出入库任务统计柱状图 function initTaskHistoryChart() { var taskHistoryChart = echarts.init(document.getElementById('taskHistoryChart')); diff --git a/HHWMS.Web/Views/Home/Index.cshtml b/HHWMS.Web/Views/Home/Index.cshtml index 68bffcc..686cee4 100644 --- a/HHWMS.Web/Views/Home/Index.cshtml +++ b/HHWMS.Web/Views/Home/Index.cshtml @@ -1,57 +1,144 @@ +