using Admin.NET.Core.Service; using Microsoft.AspNetCore.SignalR; namespace Admin.NET.Plugin.ApprovalFlow.Service; /// /// 流程通知服务 — 按 JSON 配置分渠道调度 /// public class FlowNotifyService : ITransient { private readonly IHubContext _hubContext; private readonly SysCacheService _cacheService; public FlowNotifyService( IHubContext hubContext, SysCacheService cacheService) { _hubContext = hubContext; _cacheService = cacheService; } public async Task NotifyUsers(List userIds, FlowNotification notification) { if (userIds.Count == 0) return; var cfg = App.GetConfig("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 userIds, long instanceId, string title) { await NotifyUsers(userIds, new FlowNotification { Type = FlowNotificationTypeEnum.Urge, InstanceId = instanceId, Title = $"【催办】{title}", Content = "流程发起人催促您尽快审批,请及时处理。", }); } public async Task NotifyNewTask(List 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 { initiatorId }, new FlowNotification { Type = FlowNotificationTypeEnum.FlowCompleted, InstanceId = instanceId, Title = $"【审批{statusText}】{title}", Content = $"您发起的审批流程已{statusText}。", }); } /// /// 转办通知 — 通知被转办人 /// public async Task NotifyTransferred(long targetUserId, long instanceId, string title, string? fromName) { await NotifyUsers(new List { targetUserId }, new FlowNotification { Type = FlowNotificationTypeEnum.Transferred, InstanceId = instanceId, Title = $"【转办】{title}", Content = $"{fromName} 将一条审批任务转交给您,请及时处理。", }); } /// /// 退回通知 — 通知被退回节点的审批人 /// public async Task NotifyReturned(List userIds, long instanceId, string title, string? returnedBy) { await NotifyUsers(userIds, new FlowNotification { Type = FlowNotificationTypeEnum.Returned, InstanceId = instanceId, Title = $"【退回】{title}", Content = $"{returnedBy} 已退回该审批,请重新审核。", }); } /// /// 加签通知 — 通知被加签人 /// public async Task NotifyAddSign(long targetUserId, long instanceId, string title, string? fromName) { await NotifyUsers(new List { targetUserId }, new FlowNotification { Type = FlowNotificationTypeEnum.AddSign, InstanceId = instanceId, Title = $"【加签】{title}", Content = $"{fromName} 邀请您参与审批,请及时处理。", }); } /// /// 撤回通知 — 通知被取消的审批人 /// public async Task NotifyWithdrawn(List 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 userIds, FlowNotification notification) { 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; await _hubContext.Clients.Clients(connectionIds).ReceiveMessage(new { title = notification.Title, message = notification.Content, type = notification.Type.ToString(), instanceId = notification.InstanceId, }); } private Task SendDingTalk(List userIds, FlowNotification notification) { // TODO: 接入 DingTalk 开放平台 API return Task.CompletedTask; } private Task SendWorkWeixin(List userIds, FlowNotification notification) { // TODO: 接入企业微信消息推送 API return Task.CompletedTask; } private Task SendEmail(List userIds, FlowNotification notification) { // TODO: 接入 SMTP / 邮件服务 return Task.CompletedTask; } private Task SendSms(List 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; } = ""; } /// /// 通知类型 /// public enum FlowNotificationTypeEnum { NewTask, Urge, FlowCompleted, Transferred, Returned, AddSign, Withdrawn, } /// /// 通知渠道配置(来自 ApprovalFlow:Notify JSON) /// 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; } }