// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Microsoft.AspNetCore.SignalR;
namespace Admin.NET.Core.Service;
///
/// 系统在线用户服务
///
[ApiDescriptionSettings(Order = 300)]
public class SysOnlineUserService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysOnlineUerRep;
private readonly SysConfigService _sysConfigService;
private readonly IHubContext _onlineUserHubContext;
public SysOnlineUserService(SqlSugarRepository sysOnlineUerRep,
SysConfigService sysConfigService,
IHubContext onlineUserHubContext)
{
_sysOnlineUerRep = sysOnlineUerRep;
_sysConfigService = sysConfigService;
_onlineUserHubContext = onlineUserHubContext;
}
///
/// 获取在线用户分页列表
///
///
[DisplayName("获取在线用户分页列表")]
public async Task> Page(PageOnlineUserInput input)
{
return await _sysOnlineUerRep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.UserName), u => u.UserName.Contains(input.UserName))
.WhereIF(!string.IsNullOrWhiteSpace(input.RealName), u => u.RealName.Contains(input.RealName))
.ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 强制下线
///
///
///
[NonValidation]
[DisplayName("强制下线")]
public async Task ForceOffline(SysOnlineUser user)
{
await _onlineUserHubContext.Clients.Client(user.ConnectionId).ForceOffline("强制下线");
await _sysOnlineUerRep.DeleteAsync(user);
}
///
/// 发布站内消息
///
///
///
///
[NonAction]
public async Task PublicNotice(SysNotice notice, List userIds)
{
var userList = await _sysOnlineUerRep.GetListAsync(u => userIds.Contains(u.UserId));
if (!userList.Any()) return;
foreach (var item in userList)
{
await _onlineUserHubContext.Clients.Client(item.ConnectionId).PublicNotice(notice);
}
}
///
/// 单用户登录
///
///
[NonAction]
public async Task SingleLogin(long userId)
{
if (await _sysConfigService.GetConfigValue(CommonConst.SysSingleLogin))
{
var users = await _sysOnlineUerRep.GetListAsync(u => u.UserId == userId);
foreach (var user in users)
{
await ForceOffline(user);
}
}
}
///
/// 通过用户ID踢掉在线用户
///
///
///
[NonAction]
public async Task ForceOffline(long userId)
{
var users = await _sysOnlineUerRep.GetListAsync(u => u.UserId == userId);
foreach (var user in users)
{
await ForceOffline(user);
}
}
}