| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using Admin.NET.Core.Service;
- using Microsoft.AspNetCore.SignalR;
- namespace Admin.NET.Plugin.ApprovalFlow.Service;
- /// <summary>
- /// 流程通知服务 — 按 JSON 配置分渠道调度
- /// </summary>
- public class FlowNotifyService : ITransient
- {
- private readonly IHubContext<OnlineUserHub, IOnlineUserHub> _hubContext;
- private readonly SysCacheService _cacheService;
- public FlowNotifyService(
- IHubContext<OnlineUserHub, IOnlineUserHub> hubContext,
- SysCacheService cacheService)
- {
- _hubContext = hubContext;
- _cacheService = cacheService;
- }
- public async Task NotifyUsers(List<long> userIds, FlowNotification notification)
- {
- if (userIds.Count == 0) return;
- var cfg = App.GetConfig<NotifyChannelConfig>("ApprovalFlow:Notify");
- if (cfg?.SignalR != false)
- await SendSignalR(userIds, notification);
- if (cfg?.DingTalk == true)
- await SendDingTalk(userIds, notification);
- if (cfg?.WorkWeixin == true)
- await SendWorkWeixin(userIds, notification);
- if (cfg?.Email == true)
- await SendEmail(userIds, notification);
- if (cfg?.Sms == true)
- await SendSms(userIds, notification);
- }
- public async Task NotifyUrge(List<long> userIds, long instanceId, string title)
- {
- await NotifyUsers(userIds, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.Urge,
- InstanceId = instanceId,
- Title = $"【催办】{title}",
- Content = "流程发起人催促您尽快审批,请及时处理。",
- });
- }
- public async Task NotifyNewTask(List<long> userIds, long instanceId, string title, string? nodeName)
- {
- await NotifyUsers(userIds, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.NewTask,
- InstanceId = instanceId,
- Title = $"【待审批】{title}",
- Content = $"您有一条新的审批任务({nodeName}),请及时处理。",
- });
- }
- public async Task NotifyFlowCompleted(long initiatorId, long instanceId, string title, FlowInstanceStatusEnum finalStatus)
- {
- var statusText = finalStatus == FlowInstanceStatusEnum.Approved ? "已通过" : "已拒绝";
- await NotifyUsers(new List<long> { initiatorId }, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.FlowCompleted,
- InstanceId = instanceId,
- Title = $"【审批{statusText}】{title}",
- Content = $"您发起的审批流程已{statusText}。",
- });
- }
- /// <summary>
- /// 转办通知 — 通知被转办人
- /// </summary>
- public async Task NotifyTransferred(long targetUserId, long instanceId, string title, string? fromName)
- {
- await NotifyUsers(new List<long> { targetUserId }, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.Transferred,
- InstanceId = instanceId,
- Title = $"【转办】{title}",
- Content = $"{fromName} 将一条审批任务转交给您,请及时处理。",
- });
- }
- /// <summary>
- /// 退回通知 — 通知被退回节点的审批人
- /// </summary>
- public async Task NotifyReturned(List<long> userIds, long instanceId, string title, string? returnedBy)
- {
- await NotifyUsers(userIds, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.Returned,
- InstanceId = instanceId,
- Title = $"【退回】{title}",
- Content = $"{returnedBy} 已退回该审批,请重新审核。",
- });
- }
- /// <summary>
- /// 加签通知 — 通知被加签人
- /// </summary>
- public async Task NotifyAddSign(long targetUserId, long instanceId, string title, string? fromName)
- {
- await NotifyUsers(new List<long> { targetUserId }, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.AddSign,
- InstanceId = instanceId,
- Title = $"【加签】{title}",
- Content = $"{fromName} 邀请您参与审批,请及时处理。",
- });
- }
- /// <summary>
- /// 撤回通知 — 通知被取消的审批人
- /// </summary>
- public async Task NotifyWithdrawn(List<long> userIds, long instanceId, string title, string? initiatorName)
- {
- await NotifyUsers(userIds, new FlowNotification
- {
- Type = FlowNotificationTypeEnum.Withdrawn,
- InstanceId = instanceId,
- Title = $"【已撤回】{title}",
- Content = $"{initiatorName} 已撤回该审批流程。",
- });
- }
- // ═══════════════════════════════════════════
- // 各渠道发送实现
- // ═══════════════════════════════════════════
- private async Task SendSignalR(List<long> userIds, FlowNotification notification)
- {
- var onlineUsers = _cacheService.HashGetAll<SysOnlineUser>(CacheConst.KeyUserOnline);
- var connectionIds = onlineUsers
- .Where(u => userIds.Contains(u.Value.UserId))
- .Select(u => u.Value.ConnectionId)
- .ToList();
- if (connectionIds.Count == 0) return;
- await _hubContext.Clients.Clients(connectionIds).ReceiveMessage(new
- {
- title = notification.Title,
- message = notification.Content,
- type = notification.Type.ToString(),
- instanceId = notification.InstanceId,
- });
- }
- private Task SendDingTalk(List<long> userIds, FlowNotification notification)
- {
- // TODO: 接入 DingTalk 开放平台 API
- return Task.CompletedTask;
- }
- private Task SendWorkWeixin(List<long> userIds, FlowNotification notification)
- {
- // TODO: 接入企业微信消息推送 API
- return Task.CompletedTask;
- }
- private Task SendEmail(List<long> userIds, FlowNotification notification)
- {
- // TODO: 接入 SMTP / 邮件服务
- return Task.CompletedTask;
- }
- private Task SendSms(List<long> userIds, FlowNotification notification)
- {
- // TODO: 接入短信服务
- return Task.CompletedTask;
- }
- }
- public class FlowNotification
- {
- public FlowNotificationTypeEnum Type { get; set; }
- public long InstanceId { get; set; }
- public string Title { get; set; } = "";
- public string Content { get; set; } = "";
- }
- /// <summary>
- /// 通知类型
- /// </summary>
- public enum FlowNotificationTypeEnum
- {
- NewTask,
- Urge,
- FlowCompleted,
- Transferred,
- Returned,
- AddSign,
- Withdrawn,
- }
- /// <summary>
- /// 通知渠道配置(来自 ApprovalFlow:Notify JSON)
- /// </summary>
- public class NotifyChannelConfig
- {
- public bool SignalR { get; set; } = true;
- public bool DingTalk { get; set; }
- public bool WorkWeixin { get; set; }
- public bool Email { get; set; }
- public bool Sms { get; set; }
- }
|