You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.2 KiB
C#

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<string?> GetOrNullUpmsAsync(this ISettingManager settingManager, string name,
bool fallback = true)
{
return settingManager.GetOrNullAsync(name, UpmsSettingManagementProvider.ProviderName, null, fallback);
}
public static Task<List<SettingValue>> GetAllUpmsAsync(this ISettingManager settingManager, bool fallback = true)
{
return settingManager.GetAllAsync(UpmsSettingManagementProvider.ProviderName, null, fallback);
}
public static Task<Dictionary<string, SettingValue>> 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<UpmsOptions> 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<T>(string key, Func<string?, T, T> 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;
}
}
/// <summary>
/// 获取短信配置
/// </summary>
/// <param name="settingManager"></param>
/// <returns></returns>
public static async Task<SmsConfigModel> 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<SmsConfigModel>(configJson ?? string.Empty) ?? new SmsConfigModel();
ret.Settings =
JsonConvert.DeserializeObject<Dictionary<SmsTypeEnum, SmsSettings>>(settingsJson ?? string.Empty) ??
new Dictionary<SmsTypeEnum, SmsSettings>();
ret.Providers = JsonConvert.DeserializeObject<List<SmsProvider>>(providersJson ?? string.Empty) ?? [];
ret.Templates = JsonConvert.DeserializeObject<List<SmsTemplate>>(templateJson ?? string.Empty) ?? [];
return ret;
}
#endregion
}