From b9ffd7449eae3cef9cc3da0c7713d397e51e51f1 Mon Sep 17 00:00:00 2001 From: DonPangPang Date: Thu, 24 Mar 2022 19:44:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edevelopment=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=201.=20=E5=BC=80=E5=90=AF=E5=BC=80=E5=8F=91=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E9=AA=8C=E8=AF=81=E7=A0=81=E5=B0=86=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E5=BC=BA=E5=88=B6=E9=AA=8C=E8=AF=81=202.=20=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E5=BC=80=E5=8F=91=E6=A8=A1=E5=BC=8F=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E8=83=8C=E6=99=AF=E8=89=B2=E5=B0=86?= =?UTF-8?q?=E5=8F=98=E4=B8=BA=E7=81=B0=E8=89=B2=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lazy.Captcha.Core/CaptchaOptions.cs | 5 + Lazy.Captcha.Core/DefaultCaptcha.cs | 6 +- .../Image/DefaultCaptchaImageGenerator.cs | 105 ++++++++++-------- .../Generator/Image/ICaptchaImageGenerator.cs | 4 +- Lazy.Captcha.Web/Lazy.Captcha.Web.csproj | 2 + Lazy.Captcha.Web/Program.cs | 2 + Lazy.Captcha.Web/appsettings.json | 1 + LazyCaptcha.sln | 7 +- README.md | 3 + 9 files changed, 86 insertions(+), 49 deletions(-) diff --git a/Lazy.Captcha.Core/CaptchaOptions.cs b/Lazy.Captcha.Core/CaptchaOptions.cs index e970eb0..c2f9ca4 100644 --- a/Lazy.Captcha.Core/CaptchaOptions.cs +++ b/Lazy.Captcha.Core/CaptchaOptions.cs @@ -57,6 +57,11 @@ namespace Lazy.Captcha.Core /// public bool IgnoreCase { get; set; } = true; + /// + /// 是否开启开发者模式 + /// + public bool IsDevelopment { get; set; } = false; + /// /// 存储键前缀 /// diff --git a/Lazy.Captcha.Core/DefaultCaptcha.cs b/Lazy.Captcha.Core/DefaultCaptcha.cs index 942c00c..34b56d6 100644 --- a/Lazy.Captcha.Core/DefaultCaptcha.cs +++ b/Lazy.Captcha.Core/DefaultCaptcha.cs @@ -35,7 +35,7 @@ namespace Lazy.Captcha.Core public CaptchaData Generate(string captchaId) { var (renderText, code) = _captchaCodeGenerator.Generate(_options.CodeLength); - var image = _captchaImageGenerator.Generate(renderText, _options.ImageOption); + var image = _captchaImageGenerator.Generate(renderText, _options.ImageOption, _options.IsDevelopment); _storage.Set(captchaId, code, TimeSpan.FromSeconds(_options.ExpirySeconds)); return new CaptchaData(captchaId, code, image); @@ -54,6 +54,8 @@ namespace Lazy.Captcha.Core /// public bool Validate(string captchaId, string code, TimeSpan? delay = null) { + if (_options.IsDevelopment) return true; + var val = _storage.Get(captchaId); var comparisonType = _options.IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; var result = string.Equals(val, code, comparisonType); @@ -77,6 +79,8 @@ namespace Lazy.Captcha.Core /// public async Task ValidateAsync(string captchaId, string code, TimeSpan? delay = null, CancellationToken token = default) { + if (_options.IsDevelopment) return true; + var val = _storage.Get(captchaId); var comparisonType = _options.IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; var result = string.Equals(val, code, comparisonType); diff --git a/Lazy.Captcha.Core/Generator/Image/DefaultCaptchaImageGenerator.cs b/Lazy.Captcha.Core/Generator/Image/DefaultCaptchaImageGenerator.cs index 0695f0b..a74fca1 100644 --- a/Lazy.Captcha.Core/Generator/Image/DefaultCaptchaImageGenerator.cs +++ b/Lazy.Captcha.Core/Generator/Image/DefaultCaptchaImageGenerator.cs @@ -25,13 +25,13 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 生成气泡图形描述 /// - /// - /// - /// - /// - /// - /// - /// + /// + /// + /// + /// + /// + /// + /// protected virtual List GenerateBubbleGraphicDescriptions(int width, int height, int count, int minRadius, int maxRadius, float thickness) { var result = new List(); @@ -57,8 +57,8 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制多个气泡 /// - /// 上下文 - /// 气泡图形描述 + /// 上下文 + /// 气泡图形描述 protected virtual void DrawBubbles(IImageProcessingContext ctx, List graphicDescriptions) { graphicDescriptions.ForEach(gd => @@ -77,8 +77,8 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制多个气泡 /// - /// 上下文 - /// 选项 + /// 上下文 + /// 选项 protected virtual void DrawBubbles(IImageProcessingContext ctx, CaptchaImageGeneratorOption option) { var graphicDescriptions = GenerateBubbleGraphicDescriptions(option.Width, option.Height, option.BubbleCount, option.BubbleMinRadius, option.BubbleMaxRadius, option.BubbleThickness); @@ -88,10 +88,10 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 生成干扰线图形描述 /// - /// 宽 - /// 高 - /// 数量 - /// 干扰线图形描述 + /// 宽 + /// 高 + /// 数量 + /// 干扰线图形描述 protected virtual List GenerateInterferenceLineGraphicDescriptions(int width, int height, int count) { var result = new List(); @@ -119,9 +119,9 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制干扰线 /// - /// 上下文 - /// 验证码的宽 - /// 验证码的高 + /// 上下文 + /// 验证码的宽 + /// 验证码的高 protected virtual void DrawInterferenceLines(IImageProcessingContext ctx, List graphicDescriptions) { graphicDescriptions.ForEach(gd => @@ -140,8 +140,8 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制干扰线 /// - /// 上下文 - /// option + /// 上下文 + /// option protected virtual void DrawInterferenceLines(IImageProcessingContext ctx, CaptchaImageGeneratorOption option) { var graphicDescriptions = GenerateInterferenceLineGraphicDescriptions(option.Width, option.Height, option.InterferenceLineCount); @@ -151,11 +151,11 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 生成干扰线图形描述 /// - /// 宽 - /// 高 - /// 文本 - /// 字体 - /// 文本图形描述 + /// 宽 + /// 高 + /// 文本 + /// 字体 + /// 文本图形描述 protected virtual List GenerateTextGraphicDescriptions(int width, int height, string text, Font font) { var result = new List(); @@ -179,8 +179,8 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制干扰线 /// - /// 上下文 - /// 图形描述 + /// 上下文 + /// 图形描述 protected virtual void DrawTexts(IImageProcessingContext ctx, List graphicDescriptions) { graphicDescriptions.ForEach(gd => @@ -199,9 +199,9 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 绘制文本 /// - /// 上下文 - /// - /// + /// 上下文 + /// + /// protected virtual void DrawTexts(IImageProcessingContext ctx, string text, CaptchaImageGeneratorOption option) { var graphicDescriptions = GenerateTextGraphicDescriptions(option.Width, option.Height, text, option.Font); @@ -211,11 +211,11 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 测算文本绘制位置 /// - /// 验证码宽度 - /// 验证码高度 - /// 要绘制的文本 - /// 文本所应用的字体 - /// 返回每个字符的位置 + /// 验证码宽度 + /// 验证码高度 + /// 要绘制的文本 + /// 文本所应用的字体 + /// 返回每个字符的位置 public virtual List MeasureTextPositions(int width, int height, string text, Font font) { var result = new List(); @@ -256,9 +256,9 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 生成 /// - /// - /// - /// + /// + /// + /// public virtual byte[] Generate(string text, CaptchaImageGeneratorOption option) { if (option.Animation) @@ -271,6 +271,19 @@ namespace Lazy.Captcha.Core.Generator.Image } } + public byte[] Generate(string text, CaptchaImageGeneratorOption option, bool isDevelopment) + { + if (!isDevelopment) + { + return Generate(text, option); + } + else + { + option.BackgroundColor = Color.Gray; + return Generate(text, option); + } + } + private byte[] GenerateNormal(string text, CaptchaImageGeneratorOption option) { using (Image img = new Image(option.Width, option.Height, option.BackgroundColor)) @@ -296,10 +309,10 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 计算透明度 /// - /// 帧索引 - /// 文字索引 - /// 验证码长度 - /// 文字的透明度 + /// 帧索引 + /// 文字索引 + /// 验证码长度 + /// 文字的透明度 private float GenerateBlendPercentage(int frameIndex, int textIndex, int len) { int num = frameIndex + textIndex; @@ -311,9 +324,9 @@ namespace Lazy.Captcha.Core.Generator.Image /// /// 生成动图 /// - /// - /// - /// + /// + /// + /// private byte[] GenerateAnimation(string text, CaptchaImageGeneratorOption option) { var texGraphicDescriptions = GenerateTextGraphicDescriptions(option.Width, option.Height, text, option.Font); @@ -376,4 +389,4 @@ namespace Lazy.Captcha.Core.Generator.Image } } } -} +} \ No newline at end of file diff --git a/Lazy.Captcha.Core/Generator/Image/ICaptchaImageGenerator.cs b/Lazy.Captcha.Core/Generator/Image/ICaptchaImageGenerator.cs index e8c9104..b96a614 100644 --- a/Lazy.Captcha.Core/Generator/Image/ICaptchaImageGenerator.cs +++ b/Lazy.Captcha.Core/Generator/Image/ICaptchaImageGenerator.cs @@ -13,5 +13,7 @@ namespace Lazy.Captcha.Core.Generator.Image public interface ICaptchaImageGenerator { byte[] Generate(string text, CaptchaImageGeneratorOption option); + + byte[] Generate(string text, CaptchaImageGeneratorOption option, bool isDevelopment); } -} +} \ No newline at end of file diff --git a/Lazy.Captcha.Web/Lazy.Captcha.Web.csproj b/Lazy.Captcha.Web/Lazy.Captcha.Web.csproj index 5dd7319..b246496 100644 --- a/Lazy.Captcha.Web/Lazy.Captcha.Web.csproj +++ b/Lazy.Captcha.Web/Lazy.Captcha.Web.csproj @@ -18,4 +18,6 @@ + + diff --git a/Lazy.Captcha.Web/Program.cs b/Lazy.Captcha.Web/Program.cs index d3bc058..d568f7d 100644 --- a/Lazy.Captcha.Web/Program.cs +++ b/Lazy.Captcha.Web/Program.cs @@ -26,6 +26,8 @@ builder.Services.AddCaptcha(builder.Configuration); // option.IgnoreCase = true; // ȽʱǷԴСд // option.StoreageKeyPrefix = ""; // 洢ǰ׺ +// option.IsDevelopment = false; // Ƿ񿪷ģʽ + // option.ImageOption.Animation = true; // Ƿö // option.ImageOption.FrameDelay = 30; // ÿ֡ӳ,Animation=trueʱЧ, Ĭ30 diff --git a/Lazy.Captcha.Web/appsettings.json b/Lazy.Captcha.Web/appsettings.json index 3ace6bd..ff995f9 100644 --- a/Lazy.Captcha.Web/appsettings.json +++ b/Lazy.Captcha.Web/appsettings.json @@ -6,6 +6,7 @@ "CaptchaType": 5, // 验证码类型 0 - 11 //"CodeLength": 4, // 验证码长度, 要放在CaptchaType设置后。 当类型为算术表达式时,长度代表操作的个数,不设置时默认为2。 "ExpirySeconds": 60, // 验证码过期秒数 + "IsDevelopment": false, // 是否开启开发模式 "IgnoreCase": true, // 比较时是否忽略大小写 "StoreageKeyPrefix": "", // 存储键前缀 "ImageOption": { diff --git a/LazyCaptcha.sln b/LazyCaptcha.sln index c5d601e..d75d243 100644 --- a/LazyCaptcha.sln +++ b/LazyCaptcha.sln @@ -11,7 +11,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lazy.Captcha.Web", "Lazy.Ca EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lazy.Captcha.Redis", "Lazy.Captcha.Redis\Lazy.Captcha.Redis.csproj", "{EC32628E-195E-4277-8026-EFAFA571432A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lazy.Captcha.xUnit", "Lazy.Captcha.xUnit\Lazy.Captcha.xUnit.csproj", "{4E63EE78-CE03-4D89-BF86-7AF2247082F2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lazy.Captcha.xUnit", "Lazy.Captcha.xUnit\Lazy.Captcha.xUnit.csproj", "{4E63EE78-CE03-4D89-BF86-7AF2247082F2}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5CE8949E-41C7-4CC6-B9F4-82693212FC1C}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/README.md b/README.md index 9fc0852..1b753f5 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ builder.Services.AddCaptcha(builder.Configuration); "CaptchaType": 5, // 验证码类型 "CodeLength": 4, // 验证码长度, 要放在CaptchaType设置后 当类型为算术表达式时,长度代表操作的个数 "ExpirySeconds": 60, // 验证码过期秒数 + "IsDevelopment": false, "IgnoreCase": true, // 比较时是否忽略大小写 "StoreageKeyPrefix": "", // 存储键前缀 "ImageOption": { @@ -121,6 +122,8 @@ builder.Services.AddCaptcha(builder.Configuration, option => option.IgnoreCase = true; // 比较时是否忽略大小写 option.StoreageKeyPrefix = ""; // 存储键前缀 + option.IsDevelopment = false; // 是否开发模式 + option.ImageOption.Animation = true; // 是否启用动画 option.ImageOption.FrameDelay = 30; // 每帧延迟,Animation=true时有效, 默认30 -- Gitee