| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Admin.NET.Core.Service;
- namespace Admin.NET.Plugin.ApprovalFlow.Service;
- /// <summary>
- /// 短信推送(P4-16)
- /// 由于 Admin.NET 内置的 SysSmsService.SendSms 依赖运营商预注册模板(阿里云/腾讯云/自定义),
- /// 动态内容不可塞入;本渠道作用限于通知触达(类似"您有新审批待办,请登录系统处理"),
- /// 依赖 NotifyChannelConfig.SmsTemplateId 对应的模板在运营商侧已发布。
- /// 未配置 SmsTemplateId / 用户无手机号时静默跳过。
- /// </summary>
- public class SmsNotifyPusher : INotifyPusher, ITransient
- {
- public string Channel => "Sms";
- private readonly SysSmsService _smsService;
- private readonly SqlSugarRepository<SysUser> _userRep;
- public SmsNotifyPusher(SysSmsService smsService, SqlSugarRepository<SysUser> userRep)
- {
- _smsService = smsService;
- _userRep = userRep;
- }
- public bool IsEnabled(NotifyChannelConfig cfg) => cfg?.Sms == true && !string.IsNullOrWhiteSpace(cfg.SmsTemplateId);
- public async Task<FlowNotifyPushResult> PushAsync(List<long> userIds, FlowNotification notification)
- {
- if (userIds.Count == 0) return FlowNotifyPushResult.Ok(0);
- var cfg = App.GetConfig<NotifyChannelConfig>("ApprovalFlow:Notify");
- var tplId = cfg?.SmsTemplateId;
- if (string.IsNullOrWhiteSpace(tplId)) return FlowNotifyPushResult.Ok(0);
- var users = await _userRep.AsQueryable()
- .Where(u => userIds.Contains(u.Id) && !string.IsNullOrEmpty(u.Phone))
- .Select(u => new { u.Id, u.Phone })
- .ToListAsync();
- if (users.Count == 0) return FlowNotifyPushResult.Ok(0);
- var errors = new List<string>();
- var successCount = 0;
- foreach (var user in users)
- {
- try
- {
- await _smsService.SendSms(user.Phone, tplId);
- successCount++;
- }
- catch (Exception ex)
- {
- errors.Add($"[{user.Id}]{ex.Message}");
- }
- }
- if (successCount > 0 && errors.Count == 0)
- return FlowNotifyPushResult.Ok(successCount);
- if (successCount > 0)
- return new FlowNotifyPushResult
- {
- Success = true,
- ActualTargetCount = successCount,
- ErrorMessage = $"部分失败: {string.Join("; ", errors)}"
- };
- return FlowNotifyPushResult.Fail(string.Join("; ", errors));
- }
- }
|