using Newtonsoft.Json; using NPin.Framework.SettingManagement.Domain; using NPin.Framework.Upms.Domain.Shared.Consts; using NPin.Framework.Upms.Domain.Shared.Enums; using NPin.Framework.Upms.Domain.Shared.Model; using NPin.Framework.Upms.Domain.Shared.Options; using Volo.Abp.Settings; namespace NPin.Framework.Upms.Domain.Settings; public static class UpmsSettingManagerExtensions { public static Task GetOrNullUpmsAsync(this ISettingManager settingManager, string name, bool fallback = true) { return settingManager.GetOrNullAsync(name, UpmsSettingManagementProvider.ProviderName, null, fallback); } public static Task> GetAllUpmsAsync(this ISettingManager settingManager, bool fallback = true) { return settingManager.GetAllAsync(UpmsSettingManagementProvider.ProviderName, null, fallback); } public static Task> GetByPrefixUpmsAsync(this ISettingManager settingManager, string namePrefix, bool fallback = false) { return settingManager.GetAllByPrefixAsync(namePrefix, UpmsSettingManagementProvider.ProviderName, null, fallback); } public static Task SetUpmsAsync(this ISettingManager settingManager, string name, string value) { return settingManager.SetAsync(name, value, UpmsSettingManagementProvider.ProviderName, null); } #region 方便快捷获取配置的语法糖 public static async Task GetSysSettingAsync(this ISettingManager settingManager, UpmsOptions defaultVal) { var configs = await settingManager.GetByPrefixUpmsAsync(ConfigKeyConst.SysSettingPrefix); return new UpmsOptions { AdminPassword = Parser(ConfigKeyConst.SysDefaultAdminPassword, StringParser, defaultVal.AdminPassword), TenantAdminPassword = Parser(ConfigKeyConst.SysDefaultTenantAdminPassword, StringParser, defaultVal.TenantAdminPassword), EnableCaptcha = Parser(ConfigKeyConst.SysEnableCaptcha, BoolParser, defaultVal.EnableCaptcha), EnableRegister = Parser(ConfigKeyConst.SysEnableRegister, BoolParser, defaultVal.EnableRegister), EnableDataBaseBackup = Parser(ConfigKeyConst.SysEnableDataBaseBackup, BoolParser, defaultVal.EnableDataBaseBackup), }; T Parser(string key, Func getter, T defaults) { if (!configs.ContainsKey(key)) { return defaults; } var val = configs[key].Value; return getter(val, defaults); } string StringParser(string? val, string defaults) { return val ?? defaults; } bool BoolParser(string? val, bool defaults) { return bool.TryParse(val ?? bool.FalseString, out var ret) ? ret : defaults; } } /// /// 获取短信配置 /// /// /// public static async Task GetSmsConfigModelAsync(this ISettingManager settingManager) { var smsConfigs = await settingManager.GetByPrefixUpmsAsync(ConfigKeyConst.SmsConfigPrefix); var configJson = smsConfigs[ConfigKeyConst.SmsConfigModel].Value; var settingsJson = smsConfigs[ConfigKeyConst.SmsSettings].Value; var providersJson = smsConfigs[ConfigKeyConst.SmsProviders].Value; var templateJson = smsConfigs[ConfigKeyConst.SmsTemplates].Value; var ret = JsonConvert.DeserializeObject(configJson ?? string.Empty) ?? new SmsConfigModel(); ret.Settings = JsonConvert.DeserializeObject>(settingsJson ?? string.Empty) ?? new Dictionary(); ret.Providers = JsonConvert.DeserializeObject>(providersJson ?? string.Empty) ?? []; ret.Templates = JsonConvert.DeserializeObject>(templateJson ?? string.Empty) ?? []; return ret; } #endregion }