From d7587dac5c72304c81298d91fb9ecdc4d9170b84 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sat, 11 May 2024 14:31:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=BE=E7=BD=AE=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=9A=E8=BF=87=E5=89=8D=E7=BC=80=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=97=E8=A1=A8=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ISettingManager.cs | 8 ++-- .../ISettingRepository.cs | 6 +++ .../ConfigurationSettingManagementProvider.cs | 5 ++ .../DefaultValueSettingManagementProvider.cs | 6 +++ .../Provider/ISettingManagementProvider.cs | 14 ++++-- .../SettingManagementProvider.cs | 11 +++-- .../SettingManager.cs | 46 ++++++++++++++++++- .../Store/ISettingManagementStore.cs | 2 + .../Store/SettingManagementStore.cs | 11 ++++- .../SqlSugarCoreSettingRepository.cs | 9 ++++ src/NPin.Web/.gitignore | 1 + 11 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/NPin.Web/.gitignore diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingManager.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingManager.cs index d527b3d..1a5fadd 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingManager.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingManager.cs @@ -8,9 +8,11 @@ namespace NPin.Framework.SettingManagement.Domain; /// public interface ISettingManager { - Task GetOrNullAsync([NotNull] string name, [NotNull] string providerName, string? providerKey, bool fallback = true); + Task GetOrNullAsync(string name, string providerName, string? providerKey, bool fallback = true); - Task> GetAllAsync([NotNull] string providerName, string? providerKey, bool fallback = true); + Task> GetAllAsync(string providerName, string? providerKey, bool fallback = true); + + Task> GetAllByPrefixAsync(string namePrefix, string providerName, string? providerKey, bool fallback = true); - Task SetAsync([NotNull] string name, string? value, [NotNull] string providerName, string? providerKey, bool forceToSet = false); + Task SetAsync(string name, string? value, string providerName, string? providerKey, bool forceToSet = false); } \ No newline at end of file diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingRepository.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingRepository.cs index 768c36a..ebbf991 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingRepository.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/ISettingRepository.cs @@ -15,6 +15,12 @@ public interface ISettingRepository: IBasicRepository string providerName, string providerKey, CancellationToken cancellationToken = default); + + Task> GetListByPrefixAsync( + string prefix, + string providerName, + string providerKey, + CancellationToken cancellationToken = default); Task> GetListAsync( string[] names, diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ConfigurationSettingManagementProvider.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ConfigurationSettingManagementProvider.cs index 5cfe203..399675c 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ConfigurationSettingManagementProvider.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ConfigurationSettingManagementProvider.cs @@ -20,6 +20,11 @@ public class ConfigurationSettingManagementProvider: ISettingManagementProvider, return Task.FromResult(Configuration[ConfigurationSettingValueProvider.ConfigurationNamePrefix + setting.Name]); } + public Task> GetByPrefixAsync(string prefix, string? providerKey) + { + throw new AbpException($"Can not get a setting value by prefix to the application configuration."); + } + public Task SetAsync(SettingDefinition setting, string value, string providerKey) { throw new AbpException($"Can not set a setting value to the application configuration."); diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/DefaultValueSettingManagementProvider.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/DefaultValueSettingManagementProvider.cs index cd3212e..5ca499d 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/DefaultValueSettingManagementProvider.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/DefaultValueSettingManagementProvider.cs @@ -12,6 +12,12 @@ public class DefaultValueSettingManagementProvider : ISettingManagementProvider, return Task.FromResult(setting.DefaultValue); } + public Task> GetByPrefixAsync(string prefix, string? providerKey) + { + throw new AbpException( + $"Can not get a value by prefix of a setting."); + } + public Task SetAsync(SettingDefinition setting, string value, string providerKey) { throw new AbpException( diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ISettingManagementProvider.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ISettingManagementProvider.cs index cf28bfc..0d43dc5 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ISettingManagementProvider.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/Provider/ISettingManagementProvider.cs @@ -13,7 +13,15 @@ public interface ISettingManagementProvider /// /// /// - Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); + Task GetOrNullAsync(SettingDefinition setting, string? providerKey); + + /// + /// 通过前缀获取配置列表 + /// + /// + /// + /// + Task> GetByPrefixAsync(string prefix, string? providerKey); /// /// 设置配置数据 @@ -22,7 +30,7 @@ public interface ISettingManagementProvider /// /// /// - Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string providerKey); + Task SetAsync(SettingDefinition setting, string value, string? providerKey); /// /// 清除配置项 @@ -30,5 +38,5 @@ public interface ISettingManagementProvider /// /// /// - Task ClearAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); + Task ClearAsync(SettingDefinition setting, string? providerKey); } \ No newline at end of file diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManagementProvider.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManagementProvider.cs index 6387891..96c2fc7 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManagementProvider.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManagementProvider.cs @@ -18,17 +18,22 @@ public abstract class SettingManagementProvider : ISettingManagementProvider SettingManagementStore = settingManagementStore; } - public virtual async Task GetOrNullAsync(SettingDefinition setting, string providerKey) + public virtual async Task GetOrNullAsync(SettingDefinition setting, string? providerKey) { return await SettingManagementStore.GetOrNullAsync(setting.Name, Name, NormalizeProviderKey(providerKey)); } - public virtual async Task SetAsync(SettingDefinition setting, string value, string providerKey) + public virtual async Task> GetByPrefixAsync(string prefix, string? providerKey) + { + return await SettingManagementStore.GetListByPrefixAsync(prefix, Name, NormalizeProviderKey(providerKey)); + } + + public virtual async Task SetAsync(SettingDefinition setting, string value, string? providerKey) { await SettingManagementStore.SetAsync(setting.Name, value, Name, NormalizeProviderKey(providerKey)); } - public virtual async Task ClearAsync(SettingDefinition setting, string providerKey) + public virtual async Task ClearAsync(SettingDefinition setting, string? providerKey) { await SettingManagementStore.DeleteAsync(setting.Name, Name, NormalizeProviderKey(providerKey)); } diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManager.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManager.cs index 9d067c0..62906a9 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManager.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/SettingManager.cs @@ -34,7 +34,7 @@ public class SettingManager : ISettingManager, ISingletonDependency protected virtual List CreateSettingManagementProviders() { using var scope = ServiceProvider.CreateScope(); - + return Options.Providers .Select(p => (scope.ServiceProvider.GetRequiredService(p) as ISettingManagementProvider)!) .ToList(); @@ -112,6 +112,50 @@ public class SettingManager : ISettingManager, ISingletonDependency return settingValues.Values.ToList(); } + public async Task> GetAllByPrefixAsync(string namePrefix, string providerName, + string? providerKey, bool fallback = false) + { + Check.NotNull(providerName, nameof(providerName)); + + var setting = await SettingDefinitionManager.GetAsync(namePrefix); + var providers = Enumerable.Reverse(Providers) + .SkipWhile(c => c.Name != providerName); + + if (!fallback) + { + providers = providers.TakeWhile(c => c.Name == providerName); + } + + var providerList = providers.Reverse().ToList(); + if (!providerList.Any()) + { + return new Dictionary(); + } + + var settingValues = new Dictionary(); + foreach (var provider in providerList) + { + var list = await provider.GetByPrefixAsync( + setting.Name, + provider.Name == providerName ? providerKey : null + ); + foreach (var sv in list) + { + if (setting.IsEncrypted) + { + sv.Value = SettingEncryptionService.Decrypt(setting, sv.Value); + } + + if (sv.Value != null) + { + settingValues[sv.Name] = sv; + } + } + } + + return settingValues; + } + public virtual async Task SetAsync(string name, string? value, string providerName, string? providerKey, bool forceToSet = false) { diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/ISettingManagementStore.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/ISettingManagementStore.cs index ee98b77..faeaca8 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/ISettingManagementStore.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/ISettingManagementStore.cs @@ -12,6 +12,8 @@ public interface ISettingManagementStore Task> GetListAsync(string providerName, string providerKey); Task> GetListAsync(string[] names, string providerName, string providerKey); + + Task> GetListByPrefixAsync(string prefix, string providerName, string providerKey); Task SetAsync(string name, string value, string providerName, string providerKey); diff --git a/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/SettingManagementStore.cs b/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/SettingManagementStore.cs index d926276..d48e4ff 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/SettingManagementStore.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.Domain/Store/SettingManagementStore.cs @@ -2,7 +2,6 @@ using NPin.Framework.SettingManagement.Domain.Entities; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; -using Volo.Abp.Guids; using Volo.Abp.Settings; using Volo.Abp.Uow; @@ -56,6 +55,16 @@ public class SettingManagementStore : ISettingManagementStore, ITransientDepende return result; } + [UnitOfWork] + public virtual async Task> GetListByPrefixAsync(string prefix, string providerName, + string providerKey) + { + Check.NotNullOrEmpty(prefix, nameof(prefix)); + + var settings = await SettingRepository.GetListByPrefixAsync(prefix, providerName, providerKey); + return settings.Select(s => new SettingValue(s.Name, s.Value)).ToList(); + } + [UnitOfWork] public virtual async Task SetAsync(string name, string value, string providerName, string providerKey) { diff --git a/module/setting-management/NPin.Framework.SettingManagement.SqlSugarCore/SqlSugarCoreSettingRepository.cs b/module/setting-management/NPin.Framework.SettingManagement.SqlSugarCore/SqlSugarCoreSettingRepository.cs index 0542ab8..f0e4579 100644 --- a/module/setting-management/NPin.Framework.SettingManagement.SqlSugarCore/SqlSugarCoreSettingRepository.cs +++ b/module/setting-management/NPin.Framework.SettingManagement.SqlSugarCore/SqlSugarCoreSettingRepository.cs @@ -29,6 +29,15 @@ public class SqlSugarCoreSettingRepository : SqlSugarRepository> GetListByPrefixAsync(string prefix, string providerName, string providerKey, + CancellationToken cancellationToken = default) + { + return await DbQueryable + .Where(s => s.Name.StartsWith(prefix)) + .Where(s => s.ProviderName == providerName && s.ProviderKey == providerKey) + .ToListAsync(cancellationToken); + } + public virtual async Task> GetListAsync(string[] names, string providerName, string providerKey, CancellationToken cancellationToken = default) { diff --git a/src/NPin.Web/.gitignore b/src/NPin.Web/.gitignore new file mode 100644 index 0000000..324fc50 --- /dev/null +++ b/src/NPin.Web/.gitignore @@ -0,0 +1 @@ +np-dev.db \ No newline at end of file