From 4f429d09a4573ffcd7626626b91115405ac11e0d Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Thu, 15 Aug 2024 22:55:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E4=BB=BB=E5=8A=A1=E4=B8=8D=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bunny.Service/IService/Service/JobService.cs | 6 +- .../Job/JobService/CronJobService.cs | 34 +++++++++++ .../Job/JobService/SimpleJobService.cs | 36 +++++++++++ Bunny.Service/Job/{SimJob.cs => SimpleJob.cs} | 2 +- Bunny.WebApi/Config/BaseConfig.cs | 28 ++------- Bunny.WebApi/Config/FilterConfig.cs | 21 ------- Bunny.WebApi/Config/PluginConfig.cs | 34 ----------- Bunny.WebApi/Config/ServiceRegistration.cs | 56 +++++++++++++++++- Bunny.WebApi/bunny.db | Bin 0 -> 102400 bytes 9 files changed, 134 insertions(+), 83 deletions(-) create mode 100644 Bunny.Service/Job/JobService/CronJobService.cs create mode 100644 Bunny.Service/Job/JobService/SimpleJobService.cs rename Bunny.Service/Job/{SimJob.cs => SimpleJob.cs} (93%) delete mode 100644 Bunny.WebApi/Config/FilterConfig.cs delete mode 100644 Bunny.WebApi/Config/PluginConfig.cs create mode 100644 Bunny.WebApi/bunny.db diff --git a/Bunny.Service/IService/Service/JobService.cs b/Bunny.Service/IService/Service/JobService.cs index 2b2dd41..0924f09 100644 --- a/Bunny.Service/IService/Service/JobService.cs +++ b/Bunny.Service/IService/Service/JobService.cs @@ -17,7 +17,7 @@ public class JobService : IJobService // var scheduler = schedulerFactory.GetScheduler(); var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult(); - var jobDetail = JobBuilder.Create() + var jobDetail = JobBuilder.Create() .UsingJobData("username", "用户名") .UsingJobData("password", "密码") .WithIdentity("simpleJob", "简单的JOB") @@ -46,7 +46,7 @@ public class JobService : IJobService var stdSchedulerFactory = new StdSchedulerFactory(); var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult(); - var jobDetail = JobBuilder.Create() + var jobDetail = JobBuilder.Create() .UsingJobData("username", "用户名") .UsingJobData("password", "密码") // TODO 如果不设置持久的会报错; @@ -81,7 +81,7 @@ public class JobService : IJobService var stdSchedulerFactory = new StdSchedulerFactory(); var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult(); - var jobDetail = JobBuilder.Create() + var jobDetail = JobBuilder.Create() .UsingJobData("username", "用户名") .UsingJobData("password", "密码") .StoreDurably() // 设置作业为持久的 diff --git a/Bunny.Service/Job/JobService/CronJobService.cs b/Bunny.Service/Job/JobService/CronJobService.cs new file mode 100644 index 0000000..5a67003 --- /dev/null +++ b/Bunny.Service/Job/JobService/CronJobService.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Builder; +using Quartz; +using Quartz.Impl; + +namespace Bunny.Service.Job.JobService; + +public static class CronJobService +{ + public static void AddCronJobService(this WebApplicationBuilder builder) + { + var stdSchedulerFactory = new StdSchedulerFactory(); + var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult(); + + var jobDetail = JobBuilder.Create() + .UsingJobData("username", "用户名") + .UsingJobData("password", "密码") + .StoreDurably() // 设置作业为持久的 + .WithIdentity("simpleJob", "简单的JOB") + .Build(); + + scheduler.AddJob(jobDetail, true).GetAwaiter().GetResult(); + + var trigger = TriggerBuilder.Create() + .ForJob(jobDetail) + .UsingJobData("trigger", "trigger值") + .WithIdentity("testTrigger", "测试发出器") + .StartNow() + .WithCronSchedule("0/1 * * * * ?") + .Build(); + + scheduler.ScheduleJob(trigger).GetAwaiter().GetResult(); + scheduler.Start().GetAwaiter().GetResult(); + } +} \ No newline at end of file diff --git a/Bunny.Service/Job/JobService/SimpleJobService.cs b/Bunny.Service/Job/JobService/SimpleJobService.cs new file mode 100644 index 0000000..9ba9561 --- /dev/null +++ b/Bunny.Service/Job/JobService/SimpleJobService.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Builder; +using Quartz; +using Quartz.Impl; + +namespace Bunny.Service.Job.JobService; + +public static class SimpleJobService +{ + public static void AddCreateSimpleJobService(this WebApplicationBuilder builder) + { + var schedulerFactory = new StdSchedulerFactory(); + // TODO 如果使用异步,必须使用await 和 async + // var scheduler = schedulerFactory.GetScheduler(); + var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult(); + + var jobDetail = JobBuilder.Create() + .UsingJobData("username", "用户名") + .UsingJobData("password", "密码") + .WithIdentity("simpleJob", "简单的JOB") + .Build(); + var trigger = TriggerBuilder.Create() + .WithIdentity("testTrigger", "测试发出器") + .StartNow() + // 每隔5秒执行一次,一共重复10次 + .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(10)) + .Build(); + + // 使用同步方法 + scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult(); + scheduler.Start().GetAwaiter().GetResult(); + + // TODO 使用异步 + // await _scheduler.ScheduleJob(jobDetail, trigger); + // await _scheduler.Start(); + } +} \ No newline at end of file diff --git a/Bunny.Service/Job/SimJob.cs b/Bunny.Service/Job/SimpleJob.cs similarity index 93% rename from Bunny.Service/Job/SimJob.cs rename to Bunny.Service/Job/SimpleJob.cs index 13925e0..8649433 100644 --- a/Bunny.Service/Job/SimJob.cs +++ b/Bunny.Service/Job/SimpleJob.cs @@ -2,7 +2,7 @@ namespace Bunny.Service.Job; -public class SimJob : IJob +public class SimpleJob : IJob { public Task Execute(IJobExecutionContext context) { diff --git a/Bunny.WebApi/Config/BaseConfig.cs b/Bunny.WebApi/Config/BaseConfig.cs index 18d19ea..5bf3015 100644 --- a/Bunny.WebApi/Config/BaseConfig.cs +++ b/Bunny.WebApi/Config/BaseConfig.cs @@ -11,40 +11,22 @@ public class BaseConfig(WebApplicationBuilder builder) /// public void Initialize() { - // 注入服务依赖、Host服务、插件等 - AddApplicationService(); - // 使用扩展,第三方如Redis、Minio、SQLSugar - UseExtend(); // 配置跨域 UseCors(); - } - - /// - /// 注入控制器、服务依赖、Host服务等 - /// - private void AddApplicationService() - { // 配置日志相关 // builder.Logging.AddLog4Net("Config/log4net.config"); - // 添加Service服务 - builder.AddApplicationServices(); - // 添加后台服务 - builder.AddApplicationBackendServices(); // 添加使用自定义配置文件 builder.Services.AddSingleton(new AppSettings(builder.Configuration)); // 添加 SignalR builder.Services.AddSignalR(); + // 添加Service服务 + builder.AddApplicationServices(); + // 添加后台服务 + builder.AddApplicationBackendServices(); // 添加验证码 builder.AddCaptcha(); - } - - /// - /// 扩展服务 - /// - private void UseExtend() - { // 设置过滤器 - builder.FilterConfigInitialize(); + builder.AddFilterConfigInitialize(); // 初始化Redis builder.AddRedisContext(); // 初始化Minio diff --git a/Bunny.WebApi/Config/FilterConfig.cs b/Bunny.WebApi/Config/FilterConfig.cs deleted file mode 100644 index 1a93188..0000000 --- a/Bunny.WebApi/Config/FilterConfig.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Bunny.Common.Filter; -using Bunny.Service.Filter; - -namespace Bunny.WebApi.Config; - -public static class FilterConfig -{ - /// - /// 配置过滤器 - /// - /// - public static void FilterConfigInitialize(this WebApplicationBuilder builder) - { - // 添加自定义过滤器---全局异常捕获 - builder.Services.AddControllers(option => - { - option.Filters.Add(); - option.Filters.Add(); - }); - } -} \ No newline at end of file diff --git a/Bunny.WebApi/Config/PluginConfig.cs b/Bunny.WebApi/Config/PluginConfig.cs deleted file mode 100644 index 93c5f3e..0000000 --- a/Bunny.WebApi/Config/PluginConfig.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Lazy.Captcha.Core; -using Lazy.Captcha.Core.Generator; -using SkiaSharp; - -namespace Bunny.WebApi.Config; - -public static class PluginConfig -{ - public static void AddCaptcha(this WebApplicationBuilder builder) - { - var service = builder.Services; - // 使用图形验证码 - service.AddDistributedMemoryCache(); - // 验证码相关配置内容 - service.AddCaptcha(options => - { - options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型 - options.CodeLength = 4; // 验证码长度 - options.ExpirySeconds = 60; // 过期时间(单位/秒) - options.IgnoreCase = true; // 比较忽略大小写 - options.ImageOption.Animation = true; // 是否启用动画 - options.ImageOption.Width = 130; // 验证码宽度 - options.ImageOption.Height = 48; // 验证码高度 - options.ImageOption.BackgroundColor = SKColors.White; - options.ImageOption.BubbleCount = 6; // 气泡数量 - options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径 - options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径 - options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度 - options.ImageOption.InterferenceLineCount = 2; // 干扰线数量 - options.ImageOption.FontSize = 36; // 字体大小 - options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置 - }); - } -} \ No newline at end of file diff --git a/Bunny.WebApi/Config/ServiceRegistration.cs b/Bunny.WebApi/Config/ServiceRegistration.cs index b2e22a8..9398b0c 100644 --- a/Bunny.WebApi/Config/ServiceRegistration.cs +++ b/Bunny.WebApi/Config/ServiceRegistration.cs @@ -1,6 +1,12 @@ using Bunny.Common.Connect; +using Bunny.Common.Filter; +using Bunny.Service.Filter; using Bunny.Service.IService; using Bunny.Service.IService.Service; +using Bunny.Service.Job.JobService; +using Lazy.Captcha.Core; +using Lazy.Captcha.Core.Generator; +using SkiaSharp; namespace Bunny.WebApi.Config; @@ -20,11 +26,59 @@ public static class ServiceRegistration /// /// 注入后台服务相关 + /// 用于添加所以Job服务,之后在BaseConfig中进行调用 /// /// public static void AddApplicationBackendServices(this WebApplicationBuilder builder) { // 测试注入后台服务 - // builder.Services.AddHostedService(); + // builder.Services.AddHostedService().Clear(); + // https://github.com/quartznet/quartznet/tree/main/database/tables + // builder.AddCreateSimpleJobService(); + builder.AddCronJobService(); + } + + /// + /// 配置过滤器 + /// + /// + public static void AddFilterConfigInitialize(this WebApplicationBuilder builder) + { + // 添加自定义过滤器---全局异常捕获 + builder.Services.AddControllers(option => + { + option.Filters.Add(); + option.Filters.Add(); + }); + } + + /// + /// 添加验证码配置内容 + /// + /// + public static void AddCaptcha(this WebApplicationBuilder builder) + { + var service = builder.Services; + // 使用图形验证码 + service.AddDistributedMemoryCache(); + // 验证码相关配置内容 + service.AddCaptcha(options => + { + options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型 + options.CodeLength = 4; // 验证码长度 + options.ExpirySeconds = 60; // 过期时间(单位/秒) + options.IgnoreCase = true; // 比较忽略大小写 + options.ImageOption.Animation = true; // 是否启用动画 + options.ImageOption.Width = 130; // 验证码宽度 + options.ImageOption.Height = 48; // 验证码高度 + options.ImageOption.BackgroundColor = SKColors.White; + options.ImageOption.BubbleCount = 6; // 气泡数量 + options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径 + options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径 + options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度 + options.ImageOption.InterferenceLineCount = 2; // 干扰线数量 + options.ImageOption.FontSize = 36; // 字体大小 + options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置 + }); } } \ No newline at end of file diff --git a/Bunny.WebApi/bunny.db b/Bunny.WebApi/bunny.db new file mode 100644 index 0000000000000000000000000000000000000000..6bcda78a43c0321d5f310266d97f1098f6edc606 GIT binary patch literal 102400 zcmeI3&u`qu6~}k=V^>;PmgD?jTq6O49Hd3RxuX?hF1a3ZWy?W8IJIgB zbrRTCilj&nmJFqh|A8X+9((8|z2#5nrRb%9Mo&R=>2Sy)XGrc!Yq$zPd>4|vGw%&& z<}>fTdBcIcoA%!RuKt_P9({6mUtg=7t5mC%U+H?KQdwfZ7uc`(gyk7gVEg8)1CMAYXbl>RML@$u9G<5dy%q`Ll9HUQkXWO9WmO)oq?bW94 zxW4WTZTk{ei~Gqcu8&mgQFl0y9+}tV<0rQVlz5)yI;n0uuj^ZV65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