操控平台后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.5 KiB

5 months ago
// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Furion.InstantMessaging;
using Microsoft.AspNetCore.SignalR;
namespace Admin.NET.Core;
/// <summary>
/// 在线用户集线器
/// </summary>
[MapHub("/hubs/onlineUser")]
public class OnlineUserHub : Hub<IOnlineUserHub>
{
private const string GROUP_ONLINE = "GROUP_ONLINE_"; // 租户分组前缀
private readonly SqlSugarRepository<SysOnlineUser> _sysOnlineUerRep;
private readonly SysMessageService _sysMessageService;
private readonly IHubContext<OnlineUserHub, IOnlineUserHub> _onlineUserHubContext;
private readonly SysCacheService _sysCacheService;
public OnlineUserHub(SqlSugarRepository<SysOnlineUser> sysOnlineUerRep,
SysMessageService sysMessageService,
IHubContext<OnlineUserHub, IOnlineUserHub> onlineUserHubContext,
SysCacheService sysCacheService)
{
_sysOnlineUerRep = sysOnlineUerRep;
_sysMessageService = sysMessageService;
_onlineUserHubContext = onlineUserHubContext;
_sysCacheService = sysCacheService;
}
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public override async Task OnConnectedAsync()
{
var httpContext = Context.GetHttpContext();
var token = httpContext.Request.Query["access_token"];
var claims = JWTEncryption.ReadJwtToken(token)?.Claims;
var client = Parser.GetDefault().Parse(httpContext.Request.Headers["User-Agent"]);
var userId = claims.FirstOrDefault(u => u.Type == ClaimConst.UserId)?.Value;
var tenantId = claims.FirstOrDefault(u => u.Type == ClaimConst.TenantId)?.Value;
var user = new SysOnlineUser
{
ConnectionId = Context.ConnectionId,
UserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
UserName = claims.FirstOrDefault(u => u.Type == ClaimConst.Account)?.Value,
RealName = claims.FirstOrDefault(u => u.Type == ClaimConst.RealName)?.Value,
Time = DateTime.Now,
//Ip = App.HttpContext.GetRemoteIpAddressToIPv4(),
Ip = httpContext.Connection.RemoteIpAddress.ToString(),
Browser = client.UA.Family + client.UA.Major,
Os = client.OS.Family + client.OS.Major,
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : Convert.ToInt64(tenantId),
};
await _sysOnlineUerRep.InsertAsync(user);
_sysCacheService.Set(CacheConst.KeyUserOnline + user.UserId, user);
// 以租户Id进行分组
var groupName = $"{GROUP_ONLINE}{user.TenantId}";
await _onlineUserHubContext.Groups.AddToGroupAsync(Context.ConnectionId, groupName);
var userList = await _sysOnlineUerRep.AsQueryable().Filter("", true)
.Where(u => u.TenantId == user.TenantId).Take(10).ToListAsync();
await _onlineUserHubContext.Clients.Groups(groupName).OnlineUserList(new OnlineUserList
{
RealName = user.RealName,
Online = true,
UserList = userList
});
}
/// <summary>
/// 断开
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public override async Task OnDisconnectedAsync(Exception exception)
{
if (string.IsNullOrEmpty(Context.ConnectionId)) return;
var user = await _sysOnlineUerRep.AsQueryable().Filter("", true).FirstAsync(u => u.ConnectionId == Context.ConnectionId);
if (user == null) return;
await _sysOnlineUerRep.DeleteAsync(u => u.Id == user.Id);
_sysCacheService.Remove(CacheConst.KeyUserOnline + user.UserId);
// 通知当前组用户变动
var userList = await _sysOnlineUerRep.AsQueryable().Filter("", true)
.Where(u => u.TenantId == user.TenantId).Take(10).ToListAsync();
await _onlineUserHubContext.Clients.Groups($"{GROUP_ONLINE}{user.TenantId}").OnlineUserList(new OnlineUserList
{
RealName = user.RealName,
Online = false,
UserList = userList
});
}
/// <summary>
/// 强制下线
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task ForceOffline(OnlineUserHubInput input)
{
await _onlineUserHubContext.Clients.Client(input.ConnectionId).ForceOffline("强制下线");
}
/// <summary>
/// 发送信息给某个人
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ClientsSendMessage(MessageInput message)
{
await _sysMessageService.SendUser(message);
}
/// <summary>
/// 发送信息给所有人
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ClientsSendMessagetoAll(MessageInput message)
{
await _sysMessageService.SendAllUser(message);
}
/// <summary>
/// 发送消息给某些人(除了本人)
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ClientsSendMessagetoOther(MessageInput message)
{
await _sysMessageService.SendOtherUser(message);
}
/// <summary>
/// 发送消息给某些人
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ClientsSendMessagetoUsers(MessageInput message)
{
await _sysMessageService.SendUsers(message);
}
}