using Admin.NET.Core.Service;
using Microsoft.AspNetCore.SignalR;
namespace Admin.NET.Plugin.ApprovalFlow.Service;
///
/// SignalR 站内实时推送(P4-16 - 已有实现重构为独立 Pusher)
///
public class SignalRNotifyPusher : INotifyPusher, ITransient
{
public string Channel => "SignalR";
private readonly IHubContext _hubContext;
private readonly SysCacheService _cacheService;
public SignalRNotifyPusher(
IHubContext hubContext,
SysCacheService cacheService)
{
_hubContext = hubContext;
_cacheService = cacheService;
}
public bool IsEnabled(NotifyChannelConfig cfg) => cfg?.SignalR != false;
public async Task PushAsync(List userIds, FlowNotification notification)
{
try
{
var onlineUsers = _cacheService.HashGetAll(CacheConst.KeyUserOnline);
var connectionIds = onlineUsers
.Where(u => userIds.Contains(u.Value.UserId))
.Select(u => u.Value.ConnectionId)
.ToList();
if (connectionIds.Count == 0)
return FlowNotifyPushResult.Ok(0);
await _hubContext.Clients.Clients(connectionIds).ReceiveMessage(new
{
title = notification.Title,
message = notification.Content,
type = notification.Type.ToString(),
instanceId = notification.InstanceId,
});
return FlowNotifyPushResult.Ok(connectionIds.Count);
}
catch (Exception ex)
{
return FlowNotifyPushResult.Fail(ex.Message);
}
}
}