From d1627b5b37044028ceceb9533f9e501bc0df3b6c Mon Sep 17 00:00:00 2001 From: AndyWu Date: Sun, 2 Nov 2025 01:07:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0=E5=A2=9E=E5=8A=A0IServiceP?= =?UTF-8?q?rovider=EF=BC=8C=E4=BB=A5=E4=BE=BF=E4=BB=8E=E4=BB=BB=E6=84=8F?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/NoticeController.cs | 19 +++++++------- .../Extensions/NoticeExtensions.cs | 26 +++++++++---------- .../Extensions/DingtalkExtension.cs | 2 +- .../Extensions/DingtalkOptionsExtension.cs | 4 +-- .../Provider/DingtalkProvider.cs | 2 +- .../Extensions/EmailExtension.cs | 2 +- .../Extensions/EmailOptionsExtension.cs | 4 +-- .../Extensions/FeishuExtension.cs | 2 +- .../Extensions/FeishuOptionsExtension.cs | 4 +-- .../Extensions/WeixinExtension.cs | 2 +- .../Extensions/WeixinOptionsExtension.cs | 4 +-- .../DingTalkNoticeTest.cs | 10 ++++++- test/EasyNotice.UnitTests/EmailNoticeTest.cs | 2 +- test/EasyNotice.UnitTests/FeishuNoticeTest.cs | 2 +- test/EasyNotice.UnitTests/WeixinNoticeTest.cs | 2 +- 15 files changed, 48 insertions(+), 39 deletions(-) diff --git a/sample/EasyNotice.Demo.WebApi/Controller/NoticeController.cs b/sample/EasyNotice.Demo.WebApi/Controller/NoticeController.cs index e9e4b8a..bfd5596 100644 --- a/sample/EasyNotice.Demo.WebApi/Controller/NoticeController.cs +++ b/sample/EasyNotice.Demo.WebApi/Controller/NoticeController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using EasyNotice.Core; +using Microsoft.AspNetCore.Mvc; namespace EasyNotice.Demo.WebApi.Controller { @@ -26,36 +27,36 @@ public NoticeController(IEmailProvider provider, /// 邮件发送 /// [HttpGet] - public async Task SendMail([FromQuery] string str) + public async Task SendMail([FromQuery] string str) { - await _mailProvider.SendAsync(str, new Exception(str)); + return await _mailProvider.SendAsync(str, new Exception(str)); } /// /// 钉钉发送 /// [HttpGet] - public async Task SendDingTalk([FromQuery] string str) + public async Task SendDingTalk([FromQuery] string str) { - await _dingtalkProvider.SendAsync(str, new Exception(str)); + return await _dingtalkProvider.SendAsync(str, new Exception(str)); } /// /// 飞书发送 /// [HttpGet] - public async Task SendFeishu([FromQuery] string str) + public async Task SendFeishu([FromQuery] string str) { - await _feishuProvider.SendAsync(str, new Exception(str)); + return await _feishuProvider.SendAsync(str, new Exception(str)); } /// /// 企业微信发送 /// [HttpGet] - public async Task SendWexin([FromQuery] string str) + public async Task SendWexin([FromQuery] string str) { - await _weixinProvider.SendAsync(str, new Exception(str)); + return await _weixinProvider.SendAsync(str, new Exception(str)); } } } diff --git a/sample/EasyNotice.Demo.WebApi/Extensions/NoticeExtensions.cs b/sample/EasyNotice.Demo.WebApi/Extensions/NoticeExtensions.cs index 480e27e..fd79beb 100644 --- a/sample/EasyNotice.Demo.WebApi/Extensions/NoticeExtensions.cs +++ b/sample/EasyNotice.Demo.WebApi/Extensions/NoticeExtensions.cs @@ -20,21 +20,21 @@ public static IServiceCollection AddNotice(this IServiceCollection services, ICo var mailOptions = baseConfiguration.GetSection(EmailOptions.SectionName).Get(); if (mailOptions != null) { - config.UseEmail(x => + config.UseEmail((option, serviceProvider) => { - x.Password = mailOptions.Password; - x.Host = mailOptions.Host; - x.FromAddress = mailOptions.FromAddress; - x.FromName = mailOptions.FromName; - x.Port = mailOptions.Port; - x.ToAddress = mailOptions.ToAddress; + option.Password = mailOptions.Password; + option.Host = mailOptions.Host; + option.FromAddress = mailOptions.FromAddress; + option.FromName = mailOptions.FromName; + option.Port = mailOptions.Port; + option.ToAddress = mailOptions.ToAddress; }); } var dingtalkOptions = baseConfiguration.GetSection(DingtalkOptions.SectionName).Get(); if (dingtalkOptions != null) { - config.UseDingTalk(x => + config.UseDingTalk((x, serviceProvider) => { x.Secret = dingtalkOptions.Secret; x.WebHook = dingtalkOptions.WebHook; @@ -44,19 +44,19 @@ public static IServiceCollection AddNotice(this IServiceCollection services, ICo var feishuOptions = baseConfiguration.GetSection(FeishuOptions.SectionName).Get(); if (feishuOptions != null) { - config.UseFeishu(x => + config.UseFeishu((option, serviceProvider) => { - x.Secret = feishuOptions.Secret; - x.WebHook = feishuOptions.WebHook; + option.Secret = feishuOptions.Secret; + option.WebHook = feishuOptions.WebHook; }); } var weixinOptions = baseConfiguration.GetSection(WeixinOptions.SectionName).Get(); if (weixinOptions != null) { - config.UseWeixin(x => + config.UseWeixin((option, serviceProvider) => { - x.WebHook = weixinOptions.WebHook; + option.WebHook = weixinOptions.WebHook; }); } }); diff --git a/src/EasyNotice.Dingtalk/Extensions/DingtalkExtension.cs b/src/EasyNotice.Dingtalk/Extensions/DingtalkExtension.cs index dfa304a..12b6044 100644 --- a/src/EasyNotice.Dingtalk/Extensions/DingtalkExtension.cs +++ b/src/EasyNotice.Dingtalk/Extensions/DingtalkExtension.cs @@ -8,7 +8,7 @@ public static class DingtalkExtension { public static EasyNoticeOptions UseDingTalk( this EasyNoticeOptions options, - Action configure + Action configure ) { if (configure == null) diff --git a/src/EasyNotice.Dingtalk/Extensions/DingtalkOptionsExtension.cs b/src/EasyNotice.Dingtalk/Extensions/DingtalkOptionsExtension.cs index 901ff29..dcf2549 100644 --- a/src/EasyNotice.Dingtalk/Extensions/DingtalkOptionsExtension.cs +++ b/src/EasyNotice.Dingtalk/Extensions/DingtalkOptionsExtension.cs @@ -5,9 +5,9 @@ namespace EasyNotice.Dingtalk { public class DingtalkOptionsExtension : IEasyNoticeOptionsExtension { - private readonly Action configure; + private readonly Action configure; - public DingtalkOptionsExtension(Action configure) + public DingtalkOptionsExtension(Action configure) { this.configure = configure; } diff --git a/src/EasyNotice.Dingtalk/Provider/DingtalkProvider.cs b/src/EasyNotice.Dingtalk/Provider/DingtalkProvider.cs index ee95451..289b3c8 100644 --- a/src/EasyNotice.Dingtalk/Provider/DingtalkProvider.cs +++ b/src/EasyNotice.Dingtalk/Provider/DingtalkProvider.cs @@ -124,7 +124,7 @@ private async Task SendBaseAsync(MessageBase message) } else { - return new EasyNoticeSendResponse() { ErrCode = dingtalkResponse.ErrCode, ErrMsg = !string.IsNullOrEmpty(dingtalkResponse.Description) ? $"{dingtalkResponse.Description},{dingtalkResponse.Solution}" : dingtalkResponse.ErrMsg }; + return new EasyNoticeSendResponse() { ErrCode = dingtalkResponse.ErrCode, ErrMsg = !string.IsNullOrEmpty(dingtalkResponse.Description) ? $"{dingtalkResponse.Description},{dingtalkResponse.Solution}".TrimEnd(',') : dingtalkResponse.ErrMsg }; } }, message.title, _noticeOptions.IntervalSeconds); } diff --git a/src/EasyNotice.Email/Extensions/EmailExtension.cs b/src/EasyNotice.Email/Extensions/EmailExtension.cs index 3476e2a..8c0a04b 100644 --- a/src/EasyNotice.Email/Extensions/EmailExtension.cs +++ b/src/EasyNotice.Email/Extensions/EmailExtension.cs @@ -8,7 +8,7 @@ public static class EmailExtension { public static EasyNoticeOptions UseEmail( this EasyNoticeOptions options, - Action configure) + Action configure) { if (configure == null) { diff --git a/src/EasyNotice.Email/Extensions/EmailOptionsExtension.cs b/src/EasyNotice.Email/Extensions/EmailOptionsExtension.cs index 6e5dcc7..7ec9322 100644 --- a/src/EasyNotice.Email/Extensions/EmailOptionsExtension.cs +++ b/src/EasyNotice.Email/Extensions/EmailOptionsExtension.cs @@ -5,9 +5,9 @@ namespace EasyNotice.Email { internal class EmailOptionsExtension : IEasyNoticeOptionsExtension { - private readonly Action configure; + private readonly Action configure; - public EmailOptionsExtension(Action configure) + public EmailOptionsExtension(Action configure) { this.configure = configure; } diff --git a/src/EasyNotice.Feishu/Extensions/FeishuExtension.cs b/src/EasyNotice.Feishu/Extensions/FeishuExtension.cs index da8f053..b0fdd07 100644 --- a/src/EasyNotice.Feishu/Extensions/FeishuExtension.cs +++ b/src/EasyNotice.Feishu/Extensions/FeishuExtension.cs @@ -8,7 +8,7 @@ public static class FeishuExtension { public static EasyNoticeOptions UseFeishu( this EasyNoticeOptions options, - Action configure + Action configure ) { if (configure == null) diff --git a/src/EasyNotice.Feishu/Extensions/FeishuOptionsExtension.cs b/src/EasyNotice.Feishu/Extensions/FeishuOptionsExtension.cs index 547ff9f..ab4fb0f 100644 --- a/src/EasyNotice.Feishu/Extensions/FeishuOptionsExtension.cs +++ b/src/EasyNotice.Feishu/Extensions/FeishuOptionsExtension.cs @@ -5,9 +5,9 @@ namespace EasyNotice.Feishu { public class FeishuOptionsExtension : IEasyNoticeOptionsExtension { - private readonly Action configure; + private readonly Action configure; - public FeishuOptionsExtension(Action configure) + public FeishuOptionsExtension(Action configure) { this.configure = configure; } diff --git a/src/EasyNotice.Weixin/Extensions/WeixinExtension.cs b/src/EasyNotice.Weixin/Extensions/WeixinExtension.cs index 6db1bc2..f9f40c5 100644 --- a/src/EasyNotice.Weixin/Extensions/WeixinExtension.cs +++ b/src/EasyNotice.Weixin/Extensions/WeixinExtension.cs @@ -8,7 +8,7 @@ public static class WeixinExtension { public static EasyNoticeOptions UseWeixin( this EasyNoticeOptions options, - Action configure + Action configure ) { if (configure == null) diff --git a/src/EasyNotice.Weixin/Extensions/WeixinOptionsExtension.cs b/src/EasyNotice.Weixin/Extensions/WeixinOptionsExtension.cs index a5a8252..42015fe 100644 --- a/src/EasyNotice.Weixin/Extensions/WeixinOptionsExtension.cs +++ b/src/EasyNotice.Weixin/Extensions/WeixinOptionsExtension.cs @@ -5,9 +5,9 @@ namespace EasyNotice.Weixin { public class WeixinOptionsExtension : IEasyNoticeOptionsExtension { - private readonly Action configure; + private readonly Action configure; - public WeixinOptionsExtension(Action configure) + public WeixinOptionsExtension(Action configure) { this.configure = configure; } diff --git a/test/EasyNotice.UnitTests/DingTalkNoticeTest.cs b/test/EasyNotice.UnitTests/DingTalkNoticeTest.cs index 82e4a3d..965fae0 100644 --- a/test/EasyNotice.UnitTests/DingTalkNoticeTest.cs +++ b/test/EasyNotice.UnitTests/DingTalkNoticeTest.cs @@ -20,7 +20,7 @@ public DingTalkNoticeTest() services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseDingTalk(option => + config.UseDingTalk((option, serviceProvider) => { option.WebHook = "https://oapi.dingtalk.com/robot/send?access_token=xxx";//通知地址 option.Secret = "secret";//签名校验 @@ -44,5 +44,13 @@ public async Task DingTalk_Send_AtUser_Should_Be_Succeed() var response = await _dingtalkProvider.SendAsync("通知标题", new Exception("custom exception"), new EasyNoticeAtUser() { IsAtAll = false, Mobile = new[] { "138xxxxxxxx" } }); Assert.True(response.IsSuccess); } + + [Fact] + public async Task DingTalk_Send_Markdown_Should_Be_Succeed() + { + var response = await _dingtalkProvider.SendMarkdownAsync("通知标题", "#### 订单审核通过 信息如下:\r\n订单号:11111111111\r\n商品名:手机"); + Assert.True(response.IsSuccess); + } + } } diff --git a/test/EasyNotice.UnitTests/EmailNoticeTest.cs b/test/EasyNotice.UnitTests/EmailNoticeTest.cs index 1e05144..b5d7c0f 100644 --- a/test/EasyNotice.UnitTests/EmailNoticeTest.cs +++ b/test/EasyNotice.UnitTests/EmailNoticeTest.cs @@ -17,7 +17,7 @@ public EmailNoticeTest() services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseEmail(option => + config.UseEmail((option, serviceProvider) => { option.Host = "smtp.qq.com"; option.Port = 465; diff --git a/test/EasyNotice.UnitTests/FeishuNoticeTest.cs b/test/EasyNotice.UnitTests/FeishuNoticeTest.cs index 388c75f..3569b5c 100644 --- a/test/EasyNotice.UnitTests/FeishuNoticeTest.cs +++ b/test/EasyNotice.UnitTests/FeishuNoticeTest.cs @@ -20,7 +20,7 @@ public FeishuNoticeTest() services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseFeishu(option => + config.UseFeishu((option, serviceProvider) => { option.WebHook = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx";//通知地址 option.Secret = "secret";//签名校验 diff --git a/test/EasyNotice.UnitTests/WeixinNoticeTest.cs b/test/EasyNotice.UnitTests/WeixinNoticeTest.cs index 7de3141..8f58687 100644 --- a/test/EasyNotice.UnitTests/WeixinNoticeTest.cs +++ b/test/EasyNotice.UnitTests/WeixinNoticeTest.cs @@ -20,7 +20,7 @@ public WeixinNoticeTest() services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseWeixin(option => + config.UseWeixin((option, serviceProvider) => { option.WebHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx";//通知地址 }); From e2d8068aea5c511d4f5eead825d232261cffe268 Mon Sep 17 00:00:00 2001 From: AndyWu Date: Sun, 2 Nov 2025 01:19:11 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 99a49a3..565fe11 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ public class Startup services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseEmail(option => + config.UseEmail((option, serviceProvider) => { option.Host = "smtp.qq.com";//SMTP地址 option.Port = 465;//SMTP端口 @@ -138,7 +138,7 @@ public class Startup services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseDingTalk(option => + config.UseDingTalk((option, serviceProvider) => { option.WebHook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx";//通知地址 option.Secret = "secret";//签名校验 @@ -194,7 +194,7 @@ public class Startup services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseFeishu(option => + config.UseFeishu((option, serviceProvider) => { option.WebHook = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx";//通知地址 option.Secret = "secret";//签名校验 @@ -250,7 +250,7 @@ public class Startup services.AddEasyNotice(config => { config.IntervalSeconds = 10;//同一标题的消息,10秒内只能发一条,避免短时间内大量发送重复消息 - config.UseWeixin(option => + config.UseWeixin((option, serviceProvider) => { option.WebHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx";//通知地址 });