feat: 添加SignalR登录状态管理功能
- 在Program.cs中注册LoginHub - 在HomeController中传递用户ID以支持SignalR连接 - 在Index.cshtml中实现SignalR连接和心跳机制,处理强制登出和注册成功事件 - 移除旧的登录状态检查逻辑,优化页面加载时的连接管理
This commit is contained in:
1 parent
0e5d0247bf
commit
c38199268f
9 files changed
+242
-49
No files matched your search
@@ -56,7 +56,7 @@
|
||||
initLocationChart();
|
||||
initCurrentTaskChart();
|
||||
});
|
||||
console.log(echarts);
|
||||
//console.log(echarts);
|
||||
// 一周内出入库任务统计柱状图
|
||||
function initTaskHistoryChart() {
|
||||
var taskHistoryChart = echarts.init(document.getElementById('taskHistoryChart'));
|
||||
|
||||
@@ -1,57 +1,144 @@
|
||||
<script>layui.use('index')</script>
|
||||
<script src="/front/signalr.min.js"></script>
|
||||
<script>
|
||||
// 检查登录状态的函数
|
||||
function checkLoginStatus() {
|
||||
$.ajax({
|
||||
url: '/Home/CheckLogin',
|
||||
type: 'GET',
|
||||
success: function(data) {
|
||||
// 如果返回的数据包含Login,说明已被重定向到登录页
|
||||
if (data && data.indexOf('Login') !== -1) {
|
||||
// 清除定时器
|
||||
if (window.loginCheckTimer) {
|
||||
clearInterval(window.loginCheckTimer);
|
||||
}
|
||||
|
||||
// 使用layer弹窗提示
|
||||
layer.alert("您的账号已在其他设备登录,请重新登录", {
|
||||
icon: 0,
|
||||
title: "登录提示",
|
||||
closeBtn: 0,
|
||||
btn: ['确定'],
|
||||
yes: function(index) {
|
||||
layer.close(index);
|
||||
// 重定向到登录页
|
||||
window.location.href = '/Login/Login';
|
||||
}
|
||||
});
|
||||
// SignalR 登录状态管理
|
||||
var loginConnection = null;
|
||||
var heartbeatTimer = null;
|
||||
var userId = '@ViewData["ITCode"]'; // 从后端获取当前用户ID
|
||||
var isForceLogout = false;
|
||||
|
||||
// 初始化 SignalR 连接
|
||||
function initLoginHub() {
|
||||
loginConnection = new signalR.HubConnectionBuilder()
|
||||
.withUrl("/loginHub")
|
||||
.withAutomaticReconnect([0, 2000, 5000, 10000, 30000]) // 自动重连策略
|
||||
.build();
|
||||
|
||||
// 监听强制登出事件
|
||||
loginConnection.on("ForceLogout", function(message) {
|
||||
isForceLogout = true;
|
||||
stopHeartbeat();
|
||||
|
||||
// 使用layer弹窗提示
|
||||
layer.alert(message || "您的账号已在其他设备登录,请重新登录", {
|
||||
icon: 0,
|
||||
title: "登录提示",
|
||||
closeBtn: 0,
|
||||
btn: ['确定'],
|
||||
yes: function(index) {
|
||||
layer.close(index);
|
||||
// 重定向到登录页
|
||||
window.location.href = '/Login/Login';
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
// 如果返回401或其他错误,直接跳转到登录页
|
||||
window.location.href = '/Login/Login';
|
||||
});
|
||||
});
|
||||
|
||||
// 监听注册成功
|
||||
loginConnection.on("RegisterSuccess", function(message) {
|
||||
console.log("SignalR 登录连接成功:", message);
|
||||
startHeartbeat();
|
||||
});
|
||||
|
||||
// 监听心跳响应
|
||||
loginConnection.on("HeartbeatResponse", function(serverTime) {
|
||||
//console.log("心跳响应:", serverTime);
|
||||
});
|
||||
|
||||
// 连接关闭事件
|
||||
loginConnection.onclose(function(error) {
|
||||
console.log("SignalR 连接已关闭", error);
|
||||
stopHeartbeat();
|
||||
|
||||
if (!isForceLogout) {
|
||||
// 非强制登出,尝试重连
|
||||
setTimeout(function() {
|
||||
startConnection();
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
|
||||
// 重连成功事件
|
||||
loginConnection.onreconnected(function(connectionId) {
|
||||
console.log("SignalR 重连成功:", connectionId);
|
||||
// 重新注册
|
||||
if (userId) {
|
||||
loginConnection.invoke("Register", userId).catch(function(err) {
|
||||
console.error("重新注册失败:", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 开始连接
|
||||
startConnection();
|
||||
}
|
||||
|
||||
// 页面加载完成后开始检查
|
||||
$(document).ready(function() {
|
||||
// 存储定时器ID
|
||||
window.loginCheckTimer = setInterval(function() {
|
||||
checkLoginStatus();
|
||||
}, 10000); // 每10秒检查一次
|
||||
// 启动连接
|
||||
function startConnection() {
|
||||
if (isForceLogout) return;
|
||||
|
||||
loginConnection.start()
|
||||
.then(function() {
|
||||
console.log("SignalR 连接已建立");
|
||||
// 注册当前用户
|
||||
if (userId) {
|
||||
loginConnection.invoke("Register", userId).catch(function(err) {
|
||||
console.error("注册失败:", err);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error("SignalR 连接失败:", err);
|
||||
// 连接失败,5秒后重试
|
||||
setTimeout(function() {
|
||||
startConnection();
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
// 页面关闭时清除定时器
|
||||
$(window).on('unload', function() {
|
||||
if (window.loginCheckTimer) {
|
||||
clearInterval(window.loginCheckTimer);
|
||||
// 开始心跳
|
||||
function startHeartbeat() {
|
||||
stopHeartbeat();
|
||||
heartbeatTimer = setInterval(function() {
|
||||
if (loginConnection && loginConnection.state === signalR.HubConnectionState.Connected) {
|
||||
loginConnection.invoke("Heartbeat", userId).catch(function(err) {
|
||||
console.error("心跳发送失败:", err);
|
||||
});
|
||||
}
|
||||
}, 30000); // 每30秒发送一次心跳
|
||||
}
|
||||
|
||||
// 停止心跳
|
||||
function stopHeartbeat() {
|
||||
if (heartbeatTimer) {
|
||||
clearInterval(heartbeatTimer);
|
||||
heartbeatTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载完成后初始化
|
||||
$(document).ready(function() {
|
||||
if (userId) {
|
||||
initLoginHub();
|
||||
}
|
||||
|
||||
// 页面关闭时断开连接
|
||||
$(window).on('beforeunload', function() {
|
||||
if (loginConnection) {
|
||||
loginConnection.stop();
|
||||
}
|
||||
});
|
||||
|
||||
// 页面从隐藏状态变为可见时,立即检查一次
|
||||
// 页面从隐藏状态变为可见时,检查连接状态
|
||||
$(document).on('visibilitychange', function() {
|
||||
if (document.visibilityState === 'visible') {
|
||||
checkLoginStatus();
|
||||
if (document.visibilityState === 'visible' && !isForceLogout) {
|
||||
if (loginConnection && loginConnection.state !== signalR.HubConnectionState.Connected) {
|
||||
startConnection();
|
||||
} else if (loginConnection && userId) {
|
||||
// 页面可见时发送一次心跳
|
||||
loginConnection.invoke("Heartbeat", userId).catch(function(err) {
|
||||
console.error("心跳发送失败:", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user