优化配置文件查询

This commit is contained in:
不做码农 2022-02-23 18:30:17 +08:00
parent 3981fa2303
commit e98bde7ad5
13 changed files with 121 additions and 24 deletions

View File

@ -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;
}
/// <summary>
/// 封装要操作的字符
/// </summary>
/// <param name="sections">节点配置</param>
/// <returns></returns>
public static string App(params string[] sections)
{
try
{
if (sections.Any())
{
return Configuration[string.Join(":", sections)];
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "";
}
/// <summary>
/// 递归获取配置信息数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sections"></param>
/// <returns></returns>
public static List<T> App<T>(params string[] sections)
{
List<T> list = new();
// 引用 Microsoft.Extensions.Configuration.Binder 包
Configuration.Bind(string.Join(":", sections), list);
return list;
}
public static T Bind<T>(string key, T t)
{
Configuration.Bind(key, t);
return t;
}
public static T GetAppConfig<T>(string key, T defaultValue = default)
{
T setting = (T)Convert.ChangeType(Configuration[key], typeof(T));
var value = setting;
if (setting == null)
value = defaultValue;
return value;
}
/// <summary>
/// 获取配置文件
/// </summary>
/// <param name="key">eg: WeChat:Token</param>
/// <returns></returns>
public static string GetConfig(string key)
{
return Configuration[key];
}
}
}

View File

@ -20,7 +20,7 @@ namespace Infrastructure
Instance = new ConfigUtils(); Instance = new ConfigUtils();
} }
public static ConfigUtils Instance { get; private set; } public static ConfigUtils Instance { get; }
#endregion #endregion
private static IConfiguration Configuration { get; set; } private static IConfiguration Configuration { get; set; }

View File

@ -237,7 +237,7 @@ namespace ZR.Admin.WebApi.Controllers
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(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.GenTable = genTableInfo;
dto.IsPreview = true; dto.IsPreview = true;
//生成代码 //生成代码
@ -263,7 +263,7 @@ namespace ZR.Admin.WebApi.Controllers
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(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.GenTable = genTableInfo;
//自定义路径 //自定义路径
if (genTableInfo.GenType == "1") if (genTableInfo.GenType == "1")

View File

@ -57,7 +57,7 @@ namespace ZR.Admin.WebApi.Filters
HasPermi = perms.Exists(f => f.ToLower() == Permission.ToLower()); 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" }; string[] denyPerms = new string[] { "update", "add", "remove", "add", "edit", "delete", "import", "run", "start", "stop", "clear", "send", "export", "upload", "common" };

View File

@ -71,8 +71,9 @@ namespace ZR.Admin.WebApi.Framework
/// <returns></returns> /// <returns></returns>
public static TokenValidationParameters ValidParameters() public static TokenValidationParameters ValidParameters()
{ {
JwtSettings jwtSettings = new JwtSettings(); JwtSettings jwtSettings = new();
ConfigUtils.Instance.Bind("JwtSettings", jwtSettings); AppSettings.Bind("JwtSettings", jwtSettings);
if (jwtSettings == null || jwtSettings.SecretKey.IsEmpty()) if (jwtSettings == null || jwtSettings.SecretKey.IsEmpty())
{ {
throw new Exception("JwtSettings获取失败"); throw new Exception("JwtSettings获取失败");

View File

@ -44,7 +44,8 @@ namespace ZR.Admin.WebApi
}); });
}); });
//Ïû³ýError unprotecting the session cookie¾¯¸æ //Ïû³ý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.AddHeiCaptcha();
services.AddSession(); services.AddSession();
@ -130,7 +131,7 @@ namespace ZR.Admin.WebApi
private void InjectServices(IServiceCollection services, IConfiguration configuration) private void InjectServices(IServiceCollection services, IConfiguration configuration)
{ {
services.AddAppService(); services.AddAppService();
services.AddSingleton(new AppSettings(configuration));
//¿ªÆô¼Æ»®ÈÎÎñ //¿ªÆô¼Æ»®ÈÎÎñ
services.AddTaskSchedulers(); services.AddTaskSchedulers();
//³õʼ»¯db //³õʼ»¯db

View File

@ -277,8 +277,8 @@ namespace ZR.CodeGenerator
/// <returns></returns> /// <returns></returns>
public static string GetClassName(string tableName) public static string GetClassName(string tableName)
{ {
bool autoRemovePre = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_autoPre, false); bool autoRemovePre = AppSettings.GetAppConfig(GenConstants.Gen_autoPre, false);
string tablePrefix = ConfigUtils.Instance.GetAppConfig<string>(GenConstants.Gen_tablePrefix); string tablePrefix = AppSettings.GetAppConfig<string>(GenConstants.Gen_tablePrefix);
if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre) if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre)
{ {
@ -364,7 +364,7 @@ namespace ZR.CodeGenerator
ModuleName = "business",//导入默认模块名 ModuleName = "business",//导入默认模块名
ClassName = GetClassName(tableName).FirstUpperCase(), ClassName = GetClassName(tableName).FirstUpperCase(),
BusinessName = tableName.UnderScoreToCamelCase().FirstUpperCase(), BusinessName = tableName.UnderScoreToCamelCase().FirstUpperCase(),
FunctionAuthor = ConfigUtils.Instance.GetConfig(GenConstants.Gen_author), FunctionAuthor = AppSettings.GetConfig(GenConstants.Gen_author),
TableName = tableName, TableName = tableName,
TableComment = desc, TableComment = desc,
FunctionName = desc, FunctionName = desc,

View File

@ -23,8 +23,8 @@ namespace ZR.CodeGenerator
/// <returns></returns> /// <returns></returns>
public SqlSugarClient GetSugarDbContext(string dbName = "") public SqlSugarClient GetSugarDbContext(string dbName = "")
{ {
string connStr = ConfigUtils.Instance.GetConfig(GenConstants.Gen_conn); string connStr = AppSettings.GetConfig(GenConstants.Gen_conn);
int dbType = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_conn_dbType, 0); int dbType = AppSettings.GetAppConfig(GenConstants.Gen_conn_dbType, 0);
if (!string.IsNullOrEmpty(dbName)) if (!string.IsNullOrEmpty(dbName))
{ {

View File

@ -8,10 +8,10 @@ namespace ZR.Common
{ {
public class AliyunOssHelper public class AliyunOssHelper
{ {
static string accessKeyId = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:KEY"); static string accessKeyId = AppSettings.GetConfig("ALIYUN_OSS:KEY");
static string accessKeySecret = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:SECRET"); static string accessKeySecret = AppSettings.GetConfig("ALIYUN_OSS:SECRET");
static string endpoint = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:REGIONID"); static string endpoint = AppSettings.GetConfig("ALIYUN_OSS:REGIONID");
static string bucketName1 = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:bucketName"); static string bucketName1 = AppSettings.GetConfig("ALIYUN_OSS:bucketName");
/// <summary> /// <summary>
/// 上传到阿里云 /// 上传到阿里云

View File

@ -10,8 +10,8 @@ namespace ZR.Common.Cache
public static void Initalize() public static void Initalize()
{ {
Cache = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Cache")); Cache = new CSRedisClient(AppSettings.GetConfig("RedisServer:Cache"));
Session = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Session")); Session = new CSRedisClient(AppSettings.GetConfig("RedisServer:Session"));
} }
} }
} }

View File

@ -35,7 +35,7 @@ namespace ZR.Common
public MailHelper() public MailHelper()
{ {
ConfigUtils.Instance.Bind("MailOptions", mailOptions); AppSettings.Bind("MailOptions", mailOptions);
FromEmail = mailOptions.From; FromEmail = mailOptions.From;
Smtp = mailOptions.Smtp; Smtp = mailOptions.Smtp;
FromPwd = mailOptions.Password; FromPwd = mailOptions.Password;

View File

@ -20,7 +20,7 @@ namespace ZR.Service.System
[AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)] [AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)]
public class SysFileService : BaseService<SysFile>, ISysFileService public class SysFileService : BaseService<SysFile>, ISysFileService
{ {
private string domainUrl = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:domainUrl"); private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
private readonly SysFileRepository SysFileRepository; private readonly SysFileRepository SysFileRepository;
public SysFileService(SysFileRepository repository) : base(repository) public SysFileService(SysFileRepository repository) : base(repository)

View File

@ -341,3 +341,20 @@ export default {
}, },
}; };
</script> </script>
<style scoped>
.el-avatar {
display: inline-block;
text-align: center;
background: #ccc;
color: #fff;
white-space: nowrap;
position: relative;
overflow: hidden;
vertical-align: middle;
width: 32px;
height: 32px;
line-height: 32px;
border-radius: 16px;
}
</style>