Skip to content

Navigation Menu

Sign in
Sign up

Commit 400b9a5

Browse files
committed
fix code smell
1 parent 1427ede commit 400b9a5

8 files changed

Lines changed: 61 additions & 85 deletions

File tree

‎src/CheckCodeHelper.Samples/appsettings.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@
111111
},
112112
//以下为业务测试数据,实际不应该走配置
113113
"CurrentSenderKey": "Console",
114-
"BizFlag": "LoginValidError",
114+
"BizFlag": "ForgetAndResetPassword", //LoginValidError
115115
"Receiver": "test"
116116
}

‎src/CheckCodeHelper.Sender.EMail/AttachmentInfo.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CheckCodeHelper.Sender.EMail
1111
/// <summary>
1212
/// 邮件附件信息
1313
/// </summary>
14-
public class AttachmentInfo : IDisposable
14+
public sealedclass AttachmentInfo : IDisposable
1515
{
1616
/// <summary>
1717
/// 释放<see cref="Stream"/>

‎src/CheckCodeHelper.Sender.EMail/EMailHelper.cs‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,41 +99,20 @@ public async Task SendEMailAsync(string subject, string content, IEnumerable<Mai
9999
var message = new MimeMessage();
100100
message.From.AddRange(fromAddress);
101101
message.To.AddRange(toAddress);
102-
if (ccAddress!=null&&ccAddress.Any())
102+
if (!this.IsEmpty(ccAddress))
103103
{
104-
message.Cc.AddRange(ccAddress);
104+
message.Cc.AddRange(ccAddress);
105105
}
106-
if (bccAddress!=null&&bccAddress.Any())
106+
if (!this.IsEmpty(bccAddress))
107107
{
108-
message.Bcc.AddRange(bccAddress);
108+
message.Bcc.AddRange(bccAddress);
109109
}
110110
message.Subject = subject;
111111
var body = new TextPart(textFormat)
112112
{
113113
Text = content
114-
};
115-
MimeEntity entity = body;
116-
if (attachments != null)
117-
{
118-
var mult = new Multipart("mixed")
119-
{
120-
body
121-
};
122-
foreach (var att in attachments)
123-
{
124-
if (att.Stream != null)
125-
{
126-
var attachment = string.IsNullOrWhiteSpace(att.ContentType) ? new MimePart() : new MimePart(att.ContentType);
127-
attachment.Content = new MimeContent(att.Stream);
128-
attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
129-
attachment.ContentTransferEncoding = att.ContentTransferEncoding;
130-
attachment.FileName = ConvertHeaderToBase64(att.FileName, Encoding.UTF8);//解决附件中文名问题
131-
mult.Add(attachment);
132-
}
133-
}
134-
entity = mult;
135-
}
136-
message.Body = entity;
114+
};
115+
message.Body = this.GetMimeEntity(body, attachments);
137116
message.Date = DateTime.Now;
138117
using (var client = new SmtpClient())
139118
{
@@ -173,5 +152,34 @@ private string ConvertHeaderToBase64(string inputStr, Encoding encoding)
173152
}
174153
return inputStr;
175154
}
155+
private bool IsEmpty<T>(IEnumerable<T> source)
156+
{
157+
return source == null || !source.Any();
158+
}
159+
private MimeEntity GetMimeEntity(MimePart body, IEnumerable<AttachmentInfo> attachments)
160+
{
161+
MimeEntity entity = body;
162+
if (!this.IsEmpty(attachments))
163+
{
164+
var mult = new Multipart("mixed")
165+
{
166+
body
167+
};
168+
foreach (var att in attachments)
169+
{
170+
if (att.Stream != null)
171+
{
172+
var attachment = string.IsNullOrWhiteSpace(att.ContentType) ? new MimePart() : new MimePart(att.ContentType);
173+
attachment.Content = new MimeContent(att.Stream);
174+
attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
175+
attachment.ContentTransferEncoding = att.ContentTransferEncoding;
176+
attachment.FileName = ConvertHeaderToBase64(att.FileName, Encoding.UTF8);//解决附件中文名问题
177+
mult.Add(attachment);
178+
}
179+
}
180+
entity = mult;
181+
}
182+
return entity;
183+
}
176184
}
177185
}

‎src/CheckCodeHelper.Storage.DistributedCache/CheckCodeHelper.Storage.DistributedCache.csproj‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/CheckCodeHelper.Storage.DistributedCache/DistributedCacheStorage.cs‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎src/CheckCodeHelper.sln‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CheckCodeHelper.Sender.EMai
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CheckCodeHelper.Storage.Memory", "CheckCodeHelper.Storage.Memory\CheckCodeHelper.Storage.Memory.csproj", "{7DF905AF-A63E-462E-9B22-BF5503912851}"
2323
EndProject
24-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CheckCodeHelper.Storage.DistributedCache", "CheckCodeHelper.Storage.DistributedCache\CheckCodeHelper.Storage.DistributedCache.csproj", "{EC3822B7-D96F-435F-A163-84B0C7CDEA2B}"
25-
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CheckCodeHelper.Sender.AlibabaSms", "CheckCodeHelper.Sender.AlibabaSms\CheckCodeHelper.Sender.AlibabaSms.csproj", "{A75EF12C-4D4D-430F-BE97-BC4C29EA171E}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CheckCodeHelper.Sender.AlibabaSms", "CheckCodeHelper.Sender.AlibabaSms\CheckCodeHelper.Sender.AlibabaSms.csproj", "{A75EF12C-4D4D-430F-BE97-BC4C29EA171E}"
2725
EndProject
2826
Global
2927
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -59,10 +57,6 @@ Global
5957
{7DF905AF-A63E-462E-9B22-BF5503912851}.Debug|Any CPU.Build.0 = Debug|Any CPU
6058
{7DF905AF-A63E-462E-9B22-BF5503912851}.Release|Any CPU.ActiveCfg = Release|Any CPU
6159
{7DF905AF-A63E-462E-9B22-BF5503912851}.Release|Any CPU.Build.0 = Release|Any CPU
62-
{EC3822B7-D96F-435F-A163-84B0C7CDEA2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63-
{EC3822B7-D96F-435F-A163-84B0C7CDEA2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
64-
{EC3822B7-D96F-435F-A163-84B0C7CDEA2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
65-
{EC3822B7-D96F-435F-A163-84B0C7CDEA2B}.Release|Any CPU.Build.0 = Release|Any CPU
6660
{A75EF12C-4D4D-430F-BE97-BC4C29EA171E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6761
{A75EF12C-4D4D-430F-BE97-BC4C29EA171E}.Debug|Any CPU.Build.0 = Debug|Any CPU
6862
{A75EF12C-4D4D-430F-BE97-BC4C29EA171E}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -79,7 +73,6 @@ Global
7973
{CB2BEB92-DB0A-4D28-99B0-9F3D961E6593} = {27744E1C-CEE0-41F7-988D-22734CA8787B}
8074
{D33B0951-75DF-4470-B7C0-7F071149C4CA} = {27744E1C-CEE0-41F7-988D-22734CA8787B}
8175
{7DF905AF-A63E-462E-9B22-BF5503912851} = {27744E1C-CEE0-41F7-988D-22734CA8787B}
82-
{EC3822B7-D96F-435F-A163-84B0C7CDEA2B} = {27744E1C-CEE0-41F7-988D-22734CA8787B}
8376
{A75EF12C-4D4D-430F-BE97-BC4C29EA171E} = {27744E1C-CEE0-41F7-988D-22734CA8787B}
8477
EndGlobalSection
8578
GlobalSection(ExtensibilityGlobals) = postSolution

‎src/CheckCodeHelper/CodeHelper.cs‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,7 @@ public async Task<SendResult> SendCodeAsync(string receiver, string bizFlag, str
6060
if (canSend)
6161
{
6262
//校验发送结果
63-
result = SendResult.FailInSend;
64-
if (await this.Sender.SendAsync(receiver, bizFlag, code, effectiveTime).ConfigureAwait(false)
65-
&& await this.Storage.SetCodeAsync(receiver, bizFlag, code, effectiveTime).ConfigureAwait(false))
66-
{
67-
result = SendResult.Success;
68-
if (periodLimit != null)
69-
{
70-
if (sendCount == 0)
71-
{
72-
await this.Storage.SetPeriodAsync(receiver, bizFlag, periodLimit.Period).ConfigureAwait(false);
73-
}
74-
else
75-
{
76-
await this.Storage.IncreaseSendTimesAsync(receiver, bizFlag).ConfigureAwait(false);
77-
}
78-
}
79-
}
63+
result = await this.SendCodeAfterCheckedAsync(receiver, bizFlag, code, effectiveTime, periodLimit, sendCount);
8064
}
8165
}
8266
return result;
@@ -89,6 +73,27 @@ private async Task<bool> IsSupportAsync(string receiver)
8973
}
9074
return this.Sender.IsSupport(receiver);
9175
}
76+
private async Task<SendResult> SendCodeAfterCheckedAsync(string receiver, string bizFlag, string code, TimeSpan effectiveTime, PeriodLimit periodLimit, int sendCount)
77+
{
78+
var result = SendResult.FailInSend;
79+
if (await this.Sender.SendAsync(receiver, bizFlag, code, effectiveTime).ConfigureAwait(false)
80+
&& await this.Storage.SetCodeAsync(receiver, bizFlag, code, effectiveTime).ConfigureAwait(false))
81+
{
82+
result = SendResult.Success;
83+
if (periodLimit != null)
84+
{
85+
if (sendCount == 0)
86+
{
87+
await this.Storage.SetPeriodAsync(receiver, bizFlag, periodLimit.Period).ConfigureAwait(false);
88+
}
89+
else
90+
{
91+
await this.Storage.IncreaseSendTimesAsync(receiver, bizFlag).ConfigureAwait(false);
92+
}
93+
}
94+
}
95+
return result;
96+
}
9297
/// <summary>
9398
/// 验证校验码是否正确
9499
/// </summary>

‎src/CheckCodeHelper/ComplexContentFormatter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public IContentFormatter RemoveFormatter(string bizFlag, string senderKey)
5454
/// <param name="effectiveTime">校验码有效时间范围</param>
5555
/// <param name="senderKey"><see cref="ICodeSender.Key"/></param>
5656
/// <returns></returns>
57-
public string GetContent(string receiver, string bizFlag, string code, TimeSpan effectiveTime, string senderKey)
57+
public string GetContent(string receiver, string bizFlag, string code, TimeSpan effectiveTime, string senderKey=null)
5858
{
5959
if (string.IsNullOrWhiteSpace(bizFlag))
6060
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /