From cfca7a0171b7ee58279244e72a617bded8a909e5 Mon Sep 17 00:00:00 2001 From: cjy Date: Mon, 15 Jul 2024 13:40:47 +0800 Subject: [PATCH] 11 --- .../DS.Module.Core/Helpers/RequestHelper.cs | 44 ++++++- .../DS.Module.EmailModule.csproj | 14 ++ .../DS.Module.EmailModule/DjyEmailSendReq.cs | 124 ++++++++++++++++++ .../EmailModuleInstall.cs | 27 ++++ .../DS.Module.EmailModule/EmailService.cs | 40 ++++++ .../DS.Module.EmailModule/IEmailService.cs | 21 +++ ds-wms-service/ds-wms-service.sln | 9 +- 7 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 ds-wms-service/DS.Module.EmailModule/DS.Module.EmailModule.csproj create mode 100644 ds-wms-service/DS.Module.EmailModule/DjyEmailSendReq.cs create mode 100644 ds-wms-service/DS.Module.EmailModule/EmailModuleInstall.cs create mode 100644 ds-wms-service/DS.Module.EmailModule/EmailService.cs create mode 100644 ds-wms-service/DS.Module.EmailModule/IEmailService.cs diff --git a/ds-wms-service/DS.Module.Core/Helpers/RequestHelper.cs b/ds-wms-service/DS.Module.Core/Helpers/RequestHelper.cs index 5acbb30a..e9a8512e 100644 --- a/ds-wms-service/DS.Module.Core/Helpers/RequestHelper.cs +++ b/ds-wms-service/DS.Module.Core/Helpers/RequestHelper.cs @@ -1,5 +1,9 @@ using System.Net; +using System.Net.Http.Headers; +using System.Net.Http; using System.Text; +using DS.Module.Core.Extensions; +using System.Security.Policy; namespace DS.Module.Core.Helpers; @@ -7,7 +11,7 @@ namespace DS.Module.Core.Helpers; /// 请求帮助类 /// public class RequestHelper -{ +{ public static string Post(string postData, string Url,int timeout = 60000) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url); @@ -72,7 +76,45 @@ public class RequestHelper return result; } + /// + /// http post请求 + /// + /// 地址 + /// 入参 + /// + /// 头信息 + /// + /// + /// , string token ="", Dictionary headers = null + public static async Task PostAsyncNoHeaders(string url, string parameter) + { + using (var client = new HttpClient()) + { + //var data = new StringContent("param1=value1¶m2=value2", Encoding.UTF8, "application/x-www-form-urlencoded"); + var data = new StringContent(parameter, Encoding.UTF8, "application/x-www-form-urlencoded"); + //_logger.LogWarning($"data:{data.ToJsonString()};parameter:{parameter}"); + // 添加header参数 + //client.DefaultRequestHeaders.Add("Authorization", "Bearer your_token_here"); + //foreach (var header in headers) + //{ + // client.DefaultRequestHeaders.Add(header.Key, header.Value); + //} + //_logger.LogWarning($"header:{client.DefaultRequestHeaders.ToJsonString()}"); + + try + { + var response = await client.PostAsync(url, data); + response.EnsureSuccessStatusCode(); + var responseBody = await response.Content.ReadAsStringAsync(); + return responseBody; + } + catch (HttpRequestException e) + { + throw new Exception(e.Message); + } + } + } /// /// http异步请求 /// diff --git a/ds-wms-service/DS.Module.EmailModule/DS.Module.EmailModule.csproj b/ds-wms-service/DS.Module.EmailModule/DS.Module.EmailModule.csproj new file mode 100644 index 00000000..b3971ede --- /dev/null +++ b/ds-wms-service/DS.Module.EmailModule/DS.Module.EmailModule.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/ds-wms-service/DS.Module.EmailModule/DjyEmailSendReq.cs b/ds-wms-service/DS.Module.EmailModule/DjyEmailSendReq.cs new file mode 100644 index 00000000..fe8e3cd7 --- /dev/null +++ b/ds-wms-service/DS.Module.EmailModule/DjyEmailSendReq.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DS.Module.EmailModule +{ + /// + /// 大简云邮箱接口请求 + /// + public class DjyEmailSendReq + { + /// + /// 接收邮箱 + /// + public string SendTo { get; set; } + /// + /// 抄送 + /// + public string CCTo { get; set; } + /// + /// 邮件标题 + /// + public string Title { get; set; } + /// + /// 邮件正文 + /// + public string Body { get; set; } + /// + /// 邮件显示的发送人 不设置则显示发送的邮箱 + /// + public string ShowName { get; set; } + /// + /// 发送邮箱配置(发件邮箱) + /// + public string SmtpConfig { get; set; } + /// + /// 附件列表 + /// + public List Attaches { get; set; } + /// + /// 自定义发送的邮箱账号 发件邮箱的账号,使用自定义邮箱发送邮件时,必须指定 + /// + public string Account { get; set; } + /// + /// 自定义发送的邮箱密码 发件邮箱的密码,使用自定义邮箱发送邮件时,必须指定 + /// + public string Password { get; set; } + /// + /// 自定义发送的邮箱服务器 发件邮箱的服务器地址,使用自定义邮箱发送邮件时,必须指定 + /// + public string Server { get; set; } + /// + /// 自定义发送的发件端口 发件邮箱的端口,使用自定义邮箱发送邮件时,必须指定 + /// + public int Port { get; set; } + /// + /// 自定义发送是否使用ssl 发件邮箱的是否使用SSL,使用自定义邮箱发送邮件时,必须指定 + /// + public bool UseSSL { get; set; } + } + + /// + /// 附件信息 + /// + public class Attaches { + + /// + /// 附件名称 在邮件中显示的附件名称 + /// + public string AttachName { get; set; } = string.Empty; + + /// + /// 附件内容 以base64编码的字符串 + /// + public string AttachContent { get; set; } = string.Empty; + } + /// + /// SmtpConfig选项枚举 + /// + public enum SmtpConfigEnum { + + /// + /// dongshengcangdan@h8j.top + /// + CANGDAN, + /// + /// inv@mail.myshipping.net + /// + INVOICE, + /// + /// noreplay@mail.myshipping.net + /// + NOREPLAY, + /// + /// service@mail.myshipping.net + /// + SERVICE, + /// + /// spot@mail.myshipping.net + /// + SPOT, + } + + /// + /// 大简云邮箱接口返回 + /// + public class DjyEmailSendRes { + + /// + /// 是否成功 + /// + public bool Success { get; set; } + /// + /// 说明文本 + /// + public string Message { get; set; } + /// + /// 代号 + /// + public string Code { get; set; } + } +} diff --git a/ds-wms-service/DS.Module.EmailModule/EmailModuleInstall.cs b/ds-wms-service/DS.Module.EmailModule/EmailModuleInstall.cs new file mode 100644 index 00000000..f185170c --- /dev/null +++ b/ds-wms-service/DS.Module.EmailModule/EmailModuleInstall.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DS.Module.EmailModule +{ + /// + /// 注入Email服务 + /// + public static class EmailModuleInstall + { + /// + /// + /// + /// + /// + public static void AddEmailModuleInstall(this IServiceCollection services) + { + if (services == null) throw new ArgumentNullException(nameof(services)); + + services.AddScoped(); + } + } +} diff --git a/ds-wms-service/DS.Module.EmailModule/EmailService.cs b/ds-wms-service/DS.Module.EmailModule/EmailService.cs new file mode 100644 index 00000000..4023992e --- /dev/null +++ b/ds-wms-service/DS.Module.EmailModule/EmailService.cs @@ -0,0 +1,40 @@ +using DS.Module.Core; +using DS.Module.Core.Helpers; +using Microsoft.Extensions.DependencyInjection; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft; +using Newtonsoft.Json; +using DS.Module.Core.Extensions; + +namespace DS.Module.EmailModule +{ + public class EmailService : IEmailService + { + private readonly IServiceProvider _serviceProvider; + private readonly ISqlSugarClient db; + private readonly string emailServiceUrl; + /// + /// 构造函数 + /// + /// + public EmailService(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + db = _serviceProvider.GetRequiredService(); + } + public async Task SendEmailByDjy(DjyEmailSendReq req) + { + var url = db.Queryable().AS("sys_config").Where("Code=@Code", new { Code = "" }).Select("Value").First()?.ToString(); + if (url.IsNull()) + return await Task.FromResult(DataResult.Failed("未配置")); + + var res =await RequestHelper.PostAsyncNoHeaders(url, JsonConvert.SerializeObject(req)); + throw new NotImplementedException(); + } + } +} diff --git a/ds-wms-service/DS.Module.EmailModule/IEmailService.cs b/ds-wms-service/DS.Module.EmailModule/IEmailService.cs new file mode 100644 index 00000000..8bfff378 --- /dev/null +++ b/ds-wms-service/DS.Module.EmailModule/IEmailService.cs @@ -0,0 +1,21 @@ +using DS.Module.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DS.Module.EmailModule +{ + public interface IEmailService + { + + /// + /// 根据大简云邮箱接口发送邮件 + /// + /// 请求数据 + /// + public Task SendEmailByDjy(DjyEmailSendReq req); + + } +} diff --git a/ds-wms-service/ds-wms-service.sln b/ds-wms-service/ds-wms-service.sln index f539c3a0..13a7fd09 100644 --- a/ds-wms-service/ds-wms-service.sln +++ b/ds-wms-service/ds-wms-service.sln @@ -57,7 +57,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.Module.DjyRulesEngine", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.Module.RedisModule", "DS.Module.RedisModule\DS.Module.RedisModule.csproj", "{CF36AACB-6405-4F5E-9494-72D8BD1FF414}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DS.WMS.FinanceApi", "DS.WMS.FinanceApi\DS.WMS.FinanceApi.csproj", "{32B97A3A-C361-44F3-B417-5D08E9FD9624}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.WMS.FinanceApi", "DS.WMS.FinanceApi\DS.WMS.FinanceApi.csproj", "{32B97A3A-C361-44F3-B417-5D08E9FD9624}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DS.Module.EmailModule", "DS.Module.EmailModule\DS.Module.EmailModule.csproj", "{4B51DCC1-62A5-49C5-978B-798E6B48F3C0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -165,6 +167,10 @@ Global {32B97A3A-C361-44F3-B417-5D08E9FD9624}.Debug|Any CPU.Build.0 = Debug|Any CPU {32B97A3A-C361-44F3-B417-5D08E9FD9624}.Release|Any CPU.ActiveCfg = Release|Any CPU {32B97A3A-C361-44F3-B417-5D08E9FD9624}.Release|Any CPU.Build.0 = Release|Any CPU + {4B51DCC1-62A5-49C5-978B-798E6B48F3C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B51DCC1-62A5-49C5-978B-798E6B48F3C0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B51DCC1-62A5-49C5-978B-798E6B48F3C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B51DCC1-62A5-49C5-978B-798E6B48F3C0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -195,6 +201,7 @@ Global {C3296BF4-9A02-4C7D-90C9-7BBFDAA4F1A1} = {518DB9B5-80A8-4B2C-8570-52BD406458DE} {CF36AACB-6405-4F5E-9494-72D8BD1FF414} = {518DB9B5-80A8-4B2C-8570-52BD406458DE} {32B97A3A-C361-44F3-B417-5D08E9FD9624} = {65D75DB2-12D5-4D1F-893D-9750905CE5E4} + {4B51DCC1-62A5-49C5-978B-798E6B48F3C0} = {518DB9B5-80A8-4B2C-8570-52BD406458DE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {66115F23-94B4-43C0-838E-33B5CF77F788}