😎优化在线用户逻辑

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,14 +39,15 @@ public class OnlineUserHub : Hub<IOnlineUserHub>
var httpContext = Context.GetHttpContext();
var userId = (httpContext.User.FindFirst(ClaimConst.UserId)?.Value).ToLong();
var account = httpContext.User.FindFirst(ClaimConst.Account)?.Value;
if (userId < 0 || string.IsNullOrWhiteSpace(account)) return;
var realName = httpContext.User.FindFirst(ClaimConst.RealName)?.Value;
var tenantId = (httpContext.User.FindFirst(ClaimConst.TenantId)?.Value).ToLong();
var loginMode = (LoginModeEnum)(httpContext.User.FindFirst(ClaimConst.LoginMode)?.Value).ToInt();
loginMode = loginMode == 0 ? LoginModeEnum.PC : loginMode; // 默认PC登录模式
var device = httpContext.GetClientDeviceInfo().Trim();
var ipAddress = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (userId < 0 || string.IsNullOrWhiteSpace(account)) return;
var user = new SysOnlineUser
{
ConnectionId = Context.ConnectionId,

View File

@ -1,6 +1,6 @@
import { debounce } from 'lodash-es';
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 { accessTokenKey, refreshAccessTokenKey } from '/@/utils/axios-utils';
import { i18n } from '/@/i18n';
@ -113,7 +113,7 @@ class IdleTimeoutManager {
const appEl = document.getElementById('app')!;
appEl?.remove();
// 关闭连接
signalR.stop();
// signalR.stop();
// 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: 'connectionId', title: 'Id', minWidth: 160, showOverflow: 'tooltip', sortable: true },
{ 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
@ -133,13 +133,16 @@ onMounted(async () => {
state.localPageParam = Local.get(localPageParamKey) || state.localPageParam;
// 线
signalR.off('OnlineUserList');
signalR.on('OnlineUserList', (data: any) => {
signalR.on('OnlineUserList', async (data: any) => {
state.onlineUserList = data.userList;
state.lastUserState = {
online: data.online,
realName: data.realName,
};
notificationThrottle();
// //
// await handleQuery();
});
// 线
signalR.off('ForceOffline');
@ -155,12 +158,12 @@ onMounted(async () => {
//
const notificationThrottle = throttle(
function () {
// ElNotification({
// title: '',
// message: `${state.lastUserState.online ? `${state.lastUserState.realName}线` : `${state.lastUserState.realName}`}`,
// type: `${state.lastUserState.online ? 'info' : 'error'}`,
// position: 'bottom-right',
// });
ElNotification({
title: '提示',
message: `${state.lastUserState.online ? `${state.lastUserState.realName}】上线了` : `${state.lastUserState.realName}】离开了`}`,
type: `${state.lastUserState.online ? 'info' : 'error'}`,
position: 'bottom-right',
});
},
3000,
{
@ -170,9 +173,9 @@ const notificationThrottle = throttle(
);
//
const openDrawer = () => {
const openDrawer = async () => {
state.isVisible = true;
handleQuery();
await handleQuery();
};
// api
@ -189,10 +192,10 @@ const handleQuery = async (reset = false) => {
};
//
const resetQuery = () => {
const resetQuery = async () => {
state.queryParams.userName = undefined;
state.queryParams.realName = undefined;
handleQuery();
await handleQuery();
};
//