😎优化在线用户逻辑

This commit is contained in:
zuohuaijun 2025-04-15 18:41:09 +08:00
parent 7650895334
commit 4344f94df7
3 changed files with 20 additions and 16 deletions

View File

@ -39,6 +39,8 @@ public class OnlineUserHub : Hub<IOnlineUserHub>
var httpContext = Context.GetHttpContext(); var httpContext = Context.GetHttpContext();
var userId = (httpContext.User.FindFirst(ClaimConst.UserId)?.Value).ToLong(); var userId = (httpContext.User.FindFirst(ClaimConst.UserId)?.Value).ToLong();
var account = httpContext.User.FindFirst(ClaimConst.Account)?.Value; var account = httpContext.User.FindFirst(ClaimConst.Account)?.Value;
if (userId < 0 || string.IsNullOrWhiteSpace(account)) return;
var realName = httpContext.User.FindFirst(ClaimConst.RealName)?.Value; var realName = httpContext.User.FindFirst(ClaimConst.RealName)?.Value;
var tenantId = (httpContext.User.FindFirst(ClaimConst.TenantId)?.Value).ToLong(); var tenantId = (httpContext.User.FindFirst(ClaimConst.TenantId)?.Value).ToLong();
var loginMode = (LoginModeEnum)(httpContext.User.FindFirst(ClaimConst.LoginMode)?.Value).ToInt(); var loginMode = (LoginModeEnum)(httpContext.User.FindFirst(ClaimConst.LoginMode)?.Value).ToInt();
@ -46,7 +48,6 @@ public class OnlineUserHub : Hub<IOnlineUserHub>
var device = httpContext.GetClientDeviceInfo().Trim(); var device = httpContext.GetClientDeviceInfo().Trim();
var ipAddress = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault(); var ipAddress = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (userId < 0 || string.IsNullOrWhiteSpace(account)) return;
var user = new SysOnlineUser var user = new SysOnlineUser
{ {
ConnectionId = Context.ConnectionId, ConnectionId = Context.ConnectionId,

View File

@ -1,6 +1,6 @@
import { debounce } from 'lodash-es'; import { debounce } from 'lodash-es';
import { Local, Session } from '/@/utils/storage'; import { Local, Session } from '/@/utils/storage';
import { signalR } from '/@/views/system/onlineUser/signalR'; // import { signalR } from '/@/views/system/onlineUser/signalR';
import { ElMessageBox } from 'element-plus'; import { ElMessageBox } from 'element-plus';
import { accessTokenKey, refreshAccessTokenKey } from '/@/utils/axios-utils'; import { accessTokenKey, refreshAccessTokenKey } from '/@/utils/axios-utils';
import { i18n } from '/@/i18n'; import { i18n } from '/@/i18n';
@ -113,7 +113,7 @@ class IdleTimeoutManager {
const appEl = document.getElementById('app')!; const appEl = document.getElementById('app')!;
appEl?.remove(); appEl?.remove();
// 关闭连接 // 关闭连接
signalR.stop(); // signalR.stop();
// TODO: 如果要改成调用 logout 登出接口,需要调整 clearAccessTokens 会 reload 页面的问题 // TODO: 如果要改成调用 logout 登出接口,需要调整 clearAccessTokens 会 reload 页面的问题

View File

@ -112,7 +112,7 @@ const options = useVxeTable<SysOnlineUser>(
{ field: 'browser', title: t('message.list.browser'), minWidth: 160, showOverflow: 'tooltip' }, { field: 'browser', title: t('message.list.browser'), minWidth: 160, showOverflow: 'tooltip' },
// { field: 'connectionId', title: 'Id', minWidth: 160, showOverflow: 'tooltip', sortable: true }, // { field: 'connectionId', title: 'Id', minWidth: 160, showOverflow: 'tooltip', sortable: true },
{ field: 'time', title: t('message.list.loginTime'), minWidth: 120, showOverflow: 'tooltip' }, { field: 'time', title: t('message.list.loginTime'), minWidth: 120, showOverflow: 'tooltip' },
{ title: t('message.list.operation'), fixed: 'right', width: 100, showOverflow: true, slots: { default: 'row_buttons' } }, { field: 'buttons', title: t('message.list.operation'), fixed: 'right', width: 100, showOverflow: true, slots: { default: 'row_buttons' } },
], ],
}, },
// vxeGrid()vxe-table // vxeGrid()vxe-table
@ -133,13 +133,16 @@ onMounted(async () => {
state.localPageParam = Local.get(localPageParamKey) || state.localPageParam; state.localPageParam = Local.get(localPageParamKey) || state.localPageParam;
// 线 // 线
signalR.off('OnlineUserList'); signalR.off('OnlineUserList');
signalR.on('OnlineUserList', (data: any) => { signalR.on('OnlineUserList', async (data: any) => {
state.onlineUserList = data.userList; state.onlineUserList = data.userList;
state.lastUserState = { state.lastUserState = {
online: data.online, online: data.online,
realName: data.realName, realName: data.realName,
}; };
notificationThrottle(); notificationThrottle();
// //
// await handleQuery();
}); });
// 线 // 线
signalR.off('ForceOffline'); signalR.off('ForceOffline');
@ -155,12 +158,12 @@ onMounted(async () => {
// //
const notificationThrottle = throttle( const notificationThrottle = throttle(
function () { function () {
// ElNotification({ ElNotification({
// title: '', title: '提示',
// message: `${state.lastUserState.online ? `${state.lastUserState.realName}线` : `${state.lastUserState.realName}`}`, message: `${state.lastUserState.online ? `${state.lastUserState.realName}】上线了` : `${state.lastUserState.realName}】离开了`}`,
// type: `${state.lastUserState.online ? 'info' : 'error'}`, type: `${state.lastUserState.online ? 'info' : 'error'}`,
// position: 'bottom-right', position: 'bottom-right',
// }); });
}, },
3000, 3000,
{ {
@ -170,9 +173,9 @@ const notificationThrottle = throttle(
); );
// //
const openDrawer = () => { const openDrawer = async () => {
state.isVisible = true; state.isVisible = true;
handleQuery(); await handleQuery();
}; };
// api // api
@ -189,10 +192,10 @@ const handleQuery = async (reset = false) => {
}; };
// //
const resetQuery = () => { const resetQuery = async () => {
state.queryParams.userName = undefined; state.queryParams.userName = undefined;
state.queryParams.realName = undefined; state.queryParams.realName = undefined;
handleQuery(); await handleQuery();
}; };
// //