From e98bde7ad55e4c9ebc3e681e44c9186164bb4f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Wed, 23 Feb 2022 18:30:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/AppSettings.cs | 78 +++++++++++++++++++ Infrastructure/ConfigUtils.cs | 2 +- .../System/CodeGeneratorController.cs | 4 +- .../Filters/ActionPermissionFilter.cs | 2 +- ZR.Admin.WebApi/Framework/JwtUtil.cs | 5 +- ZR.Admin.WebApi/Startup.cs | 7 +- ZR.CodeGenerator/CodeGeneratorTool.cs | 8 +- ZR.CodeGenerator/DbProvider.cs | 4 +- ZR.Common/AliyunOssHelper.cs | 8 +- ZR.Common/Cache/RedisServer.cs | 4 +- ZR.Common/MailHelper.cs | 2 +- ZR.Service/System/SysFileService.cs | 2 +- ZR.Vue/src/views/tool/file/index.vue | 19 ++++- 13 files changed, 121 insertions(+), 24 deletions(-) create mode 100644 Infrastructure/AppSettings.cs diff --git a/Infrastructure/AppSettings.cs b/Infrastructure/AppSettings.cs new file mode 100644 index 0000000..f2a6d2b --- /dev/null +++ b/Infrastructure/AppSettings.cs @@ -0,0 +1,78 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Infrastructure +{ + public class AppSettings + { + static IConfiguration Configuration { get; set; } + + public AppSettings(IConfiguration configuration) + { + Configuration = configuration; + } + + /// + /// 封装要操作的字符 + /// + /// 节点配置 + /// + public static string App(params string[] sections) + { + try + { + if (sections.Any()) + { + return Configuration[string.Join(":", sections)]; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + + return ""; + } + + /// + /// 递归获取配置信息数组 + /// + /// + /// + /// + public static List App(params string[] sections) + { + List list = new(); + // 引用 Microsoft.Extensions.Configuration.Binder 包 + Configuration.Bind(string.Join(":", sections), list); + return list; + } + public static T Bind(string key, T t) + { + Configuration.Bind(key, t); + + return t; + } + + public static T GetAppConfig(string key, T defaultValue = default) + { + T setting = (T)Convert.ChangeType(Configuration[key], typeof(T)); + var value = setting; + if (setting == null) + value = defaultValue; + return value; + } + + /// + /// 获取配置文件 + /// + /// eg: WeChat:Token + /// + public static string GetConfig(string key) + { + return Configuration[key]; + } + } +} diff --git a/Infrastructure/ConfigUtils.cs b/Infrastructure/ConfigUtils.cs index c733a9e..432604c 100644 --- a/Infrastructure/ConfigUtils.cs +++ b/Infrastructure/ConfigUtils.cs @@ -20,7 +20,7 @@ namespace Infrastructure Instance = new ConfigUtils(); } - public static ConfigUtils Instance { get; private set; } + public static ConfigUtils Instance { get; } #endregion private static IConfiguration Configuration { get; set; } diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index 0e74020..09d397e 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -237,7 +237,7 @@ namespace ZR.Admin.WebApi.Controllers var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); - dto.DbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.ConnBusDbType, 0); + dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0); dto.GenTable = genTableInfo; dto.IsPreview = true; //生成代码 @@ -263,7 +263,7 @@ namespace ZR.Admin.WebApi.Controllers var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); - dto.DbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.ConnBusDbType, 0); + dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0); dto.GenTable = genTableInfo; //自定义路径 if (genTableInfo.GenType == "1") diff --git a/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs b/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs index d895726..94a6c8f 100644 --- a/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs +++ b/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs @@ -57,7 +57,7 @@ namespace ZR.Admin.WebApi.Filters HasPermi = perms.Exists(f => f.ToLower() == Permission.ToLower()); } - bool isDemoMode = ConfigUtils.Instance.GetAppConfig("DemoMode", false); + bool isDemoMode = AppSettings.GetAppConfig("DemoMode", false); //演示公开环境屏蔽权限 string[] denyPerms = new string[] { "update", "add", "remove", "add", "edit", "delete", "import", "run", "start", "stop", "clear", "send", "export", "upload", "common" }; diff --git a/ZR.Admin.WebApi/Framework/JwtUtil.cs b/ZR.Admin.WebApi/Framework/JwtUtil.cs index d22fd94..3af5a6f 100644 --- a/ZR.Admin.WebApi/Framework/JwtUtil.cs +++ b/ZR.Admin.WebApi/Framework/JwtUtil.cs @@ -71,8 +71,9 @@ namespace ZR.Admin.WebApi.Framework /// public static TokenValidationParameters ValidParameters() { - JwtSettings jwtSettings = new JwtSettings(); - ConfigUtils.Instance.Bind("JwtSettings", jwtSettings); + JwtSettings jwtSettings = new(); + AppSettings.Bind("JwtSettings", jwtSettings); + if (jwtSettings == null || jwtSettings.SecretKey.IsEmpty()) { throw new Exception("JwtSettings获取失败"); diff --git a/ZR.Admin.WebApi/Startup.cs b/ZR.Admin.WebApi/Startup.cs index 2ea41f1..1eb0689 100644 --- a/ZR.Admin.WebApi/Startup.cs +++ b/ZR.Admin.WebApi/Startup.cs @@ -44,7 +44,8 @@ namespace ZR.Admin.WebApi }); }); //Error unprotecting the session cookie - services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection")); + services.AddDataProtection() + .PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection")); //֤ͨ services.AddHeiCaptcha(); services.AddSession(); @@ -130,12 +131,12 @@ namespace ZR.Admin.WebApi private void InjectServices(IServiceCollection services, IConfiguration configuration) { services.AddAppService(); - + services.AddSingleton(new AppSettings(configuration)); //ƻ services.AddTaskSchedulers(); //ʼdb DbExtension.AddDb(configuration); - + //עREDIS Task.Run(() => { diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 0b09e65..29805e2 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -78,7 +78,7 @@ namespace ZR.CodeGenerator InitJntTemplate(dto, replaceDto); replaceDto.VueViewListHtml = GenerateVueTableList(); replaceDto.VueQueryFormHtml = GenerateVueQueryForm(); - + GenerateModels(replaceDto, dto); GenerateInputDto(replaceDto, dto); GenerateRepository(replaceDto, dto); @@ -277,8 +277,8 @@ namespace ZR.CodeGenerator /// public static string GetClassName(string tableName) { - bool autoRemovePre = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_autoPre, false); - string tablePrefix = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_tablePrefix); + bool autoRemovePre = AppSettings.GetAppConfig(GenConstants.Gen_autoPre, false); + string tablePrefix = AppSettings.GetAppConfig(GenConstants.Gen_tablePrefix); if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre) { @@ -364,7 +364,7 @@ namespace ZR.CodeGenerator ModuleName = "business",//导入默认模块名 ClassName = GetClassName(tableName).FirstUpperCase(), BusinessName = tableName.UnderScoreToCamelCase().FirstUpperCase(), - FunctionAuthor = ConfigUtils.Instance.GetConfig(GenConstants.Gen_author), + FunctionAuthor = AppSettings.GetConfig(GenConstants.Gen_author), TableName = tableName, TableComment = desc, FunctionName = desc, diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs index 9a6e711..24dddb9 100644 --- a/ZR.CodeGenerator/DbProvider.cs +++ b/ZR.CodeGenerator/DbProvider.cs @@ -23,8 +23,8 @@ namespace ZR.CodeGenerator /// public SqlSugarClient GetSugarDbContext(string dbName = "") { - string connStr = ConfigUtils.Instance.GetConfig(GenConstants.Gen_conn); - int dbType = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_conn_dbType, 0); + string connStr = AppSettings.GetConfig(GenConstants.Gen_conn); + int dbType = AppSettings.GetAppConfig(GenConstants.Gen_conn_dbType, 0); if (!string.IsNullOrEmpty(dbName)) { diff --git a/ZR.Common/AliyunOssHelper.cs b/ZR.Common/AliyunOssHelper.cs index 8510317..55108ef 100644 --- a/ZR.Common/AliyunOssHelper.cs +++ b/ZR.Common/AliyunOssHelper.cs @@ -8,10 +8,10 @@ namespace ZR.Common { public class AliyunOssHelper { - static string accessKeyId = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:KEY"); - static string accessKeySecret = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:SECRET"); - static string endpoint = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:REGIONID"); - static string bucketName1 = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:bucketName"); + static string accessKeyId = AppSettings.GetConfig("ALIYUN_OSS:KEY"); + static string accessKeySecret = AppSettings.GetConfig("ALIYUN_OSS:SECRET"); + static string endpoint = AppSettings.GetConfig("ALIYUN_OSS:REGIONID"); + static string bucketName1 = AppSettings.GetConfig("ALIYUN_OSS:bucketName"); /// /// 上传到阿里云 diff --git a/ZR.Common/Cache/RedisServer.cs b/ZR.Common/Cache/RedisServer.cs index 3e99211..aa3312c 100644 --- a/ZR.Common/Cache/RedisServer.cs +++ b/ZR.Common/Cache/RedisServer.cs @@ -10,8 +10,8 @@ namespace ZR.Common.Cache public static void Initalize() { - Cache = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Cache")); - Session = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Session")); + Cache = new CSRedisClient(AppSettings.GetConfig("RedisServer:Cache")); + Session = new CSRedisClient(AppSettings.GetConfig("RedisServer:Session")); } } } diff --git a/ZR.Common/MailHelper.cs b/ZR.Common/MailHelper.cs index d05ef85..3f52ce9 100644 --- a/ZR.Common/MailHelper.cs +++ b/ZR.Common/MailHelper.cs @@ -35,7 +35,7 @@ namespace ZR.Common public MailHelper() { - ConfigUtils.Instance.Bind("MailOptions", mailOptions); + AppSettings.Bind("MailOptions", mailOptions); FromEmail = mailOptions.From; Smtp = mailOptions.Smtp; FromPwd = mailOptions.Password; diff --git a/ZR.Service/System/SysFileService.cs b/ZR.Service/System/SysFileService.cs index dce4638..1ff9b66 100644 --- a/ZR.Service/System/SysFileService.cs +++ b/ZR.Service/System/SysFileService.cs @@ -20,7 +20,7 @@ namespace ZR.Service.System [AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)] public class SysFileService : BaseService, ISysFileService { - private string domainUrl = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:domainUrl"); + private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl"); private readonly SysFileRepository SysFileRepository; public SysFileService(SysFileRepository repository) : base(repository) diff --git a/ZR.Vue/src/views/tool/file/index.vue b/ZR.Vue/src/views/tool/file/index.vue index 463b26c..7a2a071 100644 --- a/ZR.Vue/src/views/tool/file/index.vue +++ b/ZR.Vue/src/views/tool/file/index.vue @@ -340,4 +340,21 @@ export default { }, }, }; - \ No newline at end of file + + \ No newline at end of file