feat: 种子数据+配置管理优化

main
NoahLan 6 months ago
parent 8790954b9a
commit d069ff4727

@ -11,5 +11,8 @@
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.7" />
<PackageReference Include="Volo.Abp.BackgroundWorkers.Quartz" Version="$(AbpVersion)" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
</Project>

@ -1,18 +0,0 @@
namespace NPin.Framework.Upms.Domain.Shared.Consts;
public class ConfigConst
{
/// <summary>
/// 系统配置前缀
/// </summary>
public const string SysConfigPrefix = "sys";
/// <summary>
/// 短信配置相关
/// </summary>
public const string SmsConfigKey = "sms";
public const string SmsProvidersKey = $"{SysConfigPrefix}.{SmsConfigKey}.providers";
public const string SmsTemplatesKey = $"{SysConfigPrefix}.{SmsConfigKey}.templates";
public const string SmsSettingsKey = $"{SysConfigPrefix}.{SmsConfigKey}.settings";
public const string SmsConfigModelKey = $"{SysConfigPrefix}.{SmsConfigKey}.config";
}

@ -0,0 +1,32 @@
namespace NPin.Framework.Upms.Domain.Shared.Consts;
public class ConfigKeyConst
{
/// <summary>
/// 系统配置前缀
/// </summary>
public const string SysConfigPrefix = "sys";
/// <summary>
/// 系统设置
/// </summary>
public const string SysSettingKey = "setting";
public const string SysSettingPrefix = $"{SysConfigPrefix}.{SysSettingKey}";
public const string SysSettingDataSeed = $"{SysSettingPrefix}.ds";
public const string SysDefaultAdminPassword = $"{SysSettingPrefix}.adminPass";
public const string SysDefaultTenantAdminPassword = $"{SysSettingPrefix}.tenantAdminPass";
public const string SysEnableCaptcha = $"{SysSettingPrefix}.enableCaptcha";
public const string SysEnableRegister = $"{SysSettingPrefix}.enableRegister";
public const string SysEnableDataBaseBackup = $"{SysSettingPrefix}.enableDataBaseBackup";
/// <summary>
/// 短信配置相关
/// </summary>
public const string SmsConfigKey = "sms";
public const string SmsConfigPrefix = $"{SysConfigPrefix}.{SmsConfigKey}";
public const string SmsConfigDataSeed = $"{SmsConfigPrefix}.ds";
public const string SmsProviders = $"{SmsConfigPrefix}.providers";
public const string SmsTemplates = $"{SmsConfigPrefix}.templates";
public const string SmsSettings = $"{SmsConfigPrefix}.settings";
public const string SmsConfigModel = $"{SmsConfigPrefix}.config";
}

@ -1,7 +1,7 @@
namespace NPin.Framework.Upms.Domain.Shared.Options;
/// <summary>
/// Upms模块的总体配置
/// Upms模块的默认总体配置
/// </summary>
public class UpmsOptions
{

@ -1,5 +1,6 @@
using Mapster;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NPin.Framework.Upms.Domain.Entities;
using NPin.Framework.Upms.Domain.Shared.Etos;
using Volo.Abp.DependencyInjection;
@ -10,18 +11,18 @@ namespace NPin.Framework.Upms.Domain.EventHandlers;
public class LoginEventHandler : ILocalEventHandler<LoginEventArgs>, ITransientDependency
{
private readonly ILogger<LoginEventHandler> _logger;
public ILogger<LoginEventHandler> Logger { get; set; }
private readonly IRepository<LoginLogEntity> _repository;
public LoginEventHandler(ILogger<LoginEventHandler> logger, IRepository<LoginLogEntity> repository)
public LoginEventHandler(IRepository<LoginLogEntity> repository)
{
_logger = logger;
Logger = NullLogger<LoginEventHandler>.Instance;
_repository = repository;
}
public async Task HandleEventAsync(LoginEventArgs eventData)
{
_logger.LogInformation($"用户[{eventData.UserId}:{eventData.Username}]登录");
Logger.LogInformation($"用户[{eventData.UserId}:{eventData.Username}]登录");
var loginLogEntity = eventData.Adapt<LoginLogEntity>();
loginLogEntity.LoginMsg = $"{eventData.Username}登录系统";
loginLogEntity.LoginUser = eventData.Username;

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using NPin.Framework.Core.Extensions;
using NPin.Framework.Upms.Domain.Shared.OperLog;
@ -13,14 +14,14 @@ namespace NPin.Framework.Upms.Domain.OperLog;
public class OperLogGlobalAttribute : ActionFilterAttribute, ITransientDependency
{
private readonly ILogger<OperLogGlobalAttribute> _logger;
public ILogger<OperLogGlobalAttribute> Logger { get; set; }
private IRepository<OperationLogEntity> _repository;
private ICurrentUser _currentUser;
public OperLogGlobalAttribute(ILogger<OperLogGlobalAttribute> logger, IRepository<OperationLogEntity> repository,
public OperLogGlobalAttribute(IRepository<OperationLogEntity> repository,
ICurrentUser currentUser)
{
_logger = logger;
Logger = NullLogger<OperLogGlobalAttribute>.Instance;
_repository = repository;
_currentUser = currentUser;
}

@ -1,4 +1,9 @@
using NPin.Framework.SettingManagement.Domain;
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;
@ -16,8 +21,83 @@ public static class UpmsSettingManagerExtensions
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
}

@ -3,18 +3,32 @@ using Volo.Abp.Settings;
namespace NPin.Framework.Upms.Domain.Settings;
public class UpmsStaticSettingProvider: SettingDefinitionProvider
public class UpmsStaticSettingProvider : SettingDefinitionProvider
{
public override void Define(ISettingDefinitionContext context)
{
DefineSysSettingDefinitions(context);
DefineSmsSettingDefinitions(context);
}
private void DefineSysSettingDefinitions(ISettingDefinitionContext context)
{
context.Add(new SettingDefinition(ConfigKeyConst.SysSettingPrefix));
context.Add(new SettingDefinition(ConfigKeyConst.SysSettingDataSeed));
context.Add(new SettingDefinition(ConfigKeyConst.SysDefaultAdminPassword));
context.Add(new SettingDefinition(ConfigKeyConst.SysDefaultTenantAdminPassword));
context.Add(new SettingDefinition(ConfigKeyConst.SysEnableCaptcha));
context.Add(new SettingDefinition(ConfigKeyConst.SysEnableRegister));
context.Add(new SettingDefinition(ConfigKeyConst.SysEnableDataBaseBackup));
}
private void DefineSmsSettingDefinitions(ISettingDefinitionContext context)
{
context.Add(new SettingDefinition(ConfigConst.SmsConfigModelKey));
context.Add(new SettingDefinition(ConfigConst.SmsProvidersKey));
context.Add(new SettingDefinition(ConfigConst.SmsSettingsKey));
context.Add(new SettingDefinition(ConfigConst.SmsTemplatesKey));
context.Add(new SettingDefinition(ConfigKeyConst.SmsConfigPrefix));
context.Add(new SettingDefinition(ConfigKeyConst.SmsConfigDataSeed));
context.Add(new SettingDefinition(ConfigKeyConst.SmsConfigModel));
context.Add(new SettingDefinition(ConfigKeyConst.SmsProviders));
context.Add(new SettingDefinition(ConfigKeyConst.SmsSettings));
context.Add(new SettingDefinition(ConfigKeyConst.SmsTemplates));
}
}

@ -0,0 +1,25 @@
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace NPin.Framework.Upms.SqlSugarCore.DataSeeds;
public class RoleDataSeed : IDataSeedContributor, ITransientDependency
{
// public ILogger<RoleDataSeed> Logger { get; set; }
// private readonly ISettingManager _settingManager;
// private readonly UpmsOptions _options;
// private readonly IRolerepo _userRepository;
//
// public RoleDataSeed(ISettingManager settingManager, UpmsOptions options, IUserRepository userRepository)
// {
// _settingManager = settingManager;
// _options = options;
// _userRepository = userRepository;
// Logger = NullLogger<RoleDataSeed>.Instance;
// }
public async Task SeedAsync(DataSeedContext context)
{
// throw new NotImplementedException();
}
}

@ -1,7 +1,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using NPin.Framework.SettingManagement.Domain;
using NPin.Framework.Upms.Domain.Managers;
using NPin.Framework.Upms.Domain.Settings;
using NPin.Framework.Upms.Domain.Shared.Consts;
using NPin.Framework.Upms.Domain.Shared.Model;
@ -12,24 +12,27 @@ namespace NPin.Framework.Upms.SqlSugarCore.DataSeeds;
public class SmsConfigDataSeed : IDataSeedContributor, ITransientDependency
{
private readonly ILogger<SmsManager> _logger;
public ILogger<SmsConfigDataSeed> Logger { get; set; }
private readonly ISettingManager _settingManager;
public SmsConfigDataSeed(ILogger<SmsManager> logger, ISettingManager settingManager)
public SmsConfigDataSeed(ISettingManager settingManager)
{
_logger = logger;
Logger = NullLogger<SmsConfigDataSeed>.Instance;
_settingManager = settingManager;
}
public async Task SeedAsync(DataSeedContext context)
{
var configJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsConfigModelKey);
if (!string.IsNullOrEmpty(configJson))
var dataSeedStr = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SmsConfigDataSeed) ?? bool.FalseString;
if (bool.Parse(dataSeedStr))
{
return;
}
await _settingManager.SetUpmsAsync(ConfigKeyConst.SmsConfigDataSeed, Boolean.TrueString);
//
Logger.LogDebug("开始注入短信配置种子数据...");
var configModel = new SmsConfigModel
{
Enabled = false,
@ -37,10 +40,14 @@ public class SmsConfigDataSeed : IDataSeedContributor, ITransientDependency
Settings = [],
Templates = []
};
await _settingManager.SetUpmsAsync(ConfigKeyConst.SmsConfigModel, JsonConvert.SerializeObject(configModel));
await _settingManager.SetUpmsAsync(ConfigKeyConst.SmsSettings,
JsonConvert.SerializeObject(configModel.Settings));
await _settingManager.SetUpmsAsync(ConfigKeyConst.SmsProviders,
JsonConvert.SerializeObject(configModel.Providers));
await _settingManager.SetUpmsAsync(ConfigKeyConst.SmsTemplates,
JsonConvert.SerializeObject(configModel.Templates));
await _settingManager.SetUpmsAsync(ConfigConst.SmsConfigModelKey, JsonConvert.SerializeObject(configModel));
await _settingManager.SetUpmsAsync(ConfigConst.SmsSettingsKey, JsonConvert.SerializeObject(configModel.Settings));
await _settingManager.SetUpmsAsync(ConfigConst.SmsProvidersKey, JsonConvert.SerializeObject(configModel.Providers));
await _settingManager.SetUpmsAsync(ConfigConst.SmsTemplatesKey, JsonConvert.SerializeObject(configModel.Templates));
Logger.LogDebug("注入短信配置种子数据成功");
}
}

@ -0,0 +1,48 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NPin.Framework.SettingManagement.Domain;
using NPin.Framework.Upms.Domain.Settings;
using NPin.Framework.Upms.Domain.Shared.Consts;
using NPin.Framework.Upms.Domain.Shared.Options;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace NPin.Framework.Upms.SqlSugarCore.DataSeeds;
public class SysSettingDataSeed : IDataSeedContributor, ITransientDependency
{
public ILogger<SysSettingDataSeed> Logger { get; set; }
private readonly ISettingManager _settingManager;
private readonly UpmsOptions _options;
public SysSettingDataSeed(ISettingManager settingManager, IOptions<UpmsOptions> options)
{
_settingManager = settingManager;
_options = options.Value;
Logger = NullLogger<SysSettingDataSeed>.Instance;
}
public async Task SeedAsync(DataSeedContext context)
{
var dataSeedStr = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SysSettingDataSeed) ??
bool.FalseString;
if (bool.Parse(dataSeedStr))
{
return;
}
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysSettingDataSeed, Boolean.TrueString);
//
Logger.LogDebug("开始注入系统设置种子数据...");
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysDefaultAdminPassword, _options.AdminPassword);
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysDefaultTenantAdminPassword, _options.TenantAdminPassword);
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysEnableCaptcha, _options.EnableCaptcha.ToString());
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysEnableRegister, _options.EnableRegister.ToString());
await _settingManager.SetUpmsAsync(ConfigKeyConst.SysEnableDataBaseBackup,
_options.EnableDataBaseBackup.ToString());
Logger.LogDebug("注入系统设置种子数据成功");
}
}

@ -0,0 +1,68 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NPin.Framework.SettingManagement.Domain;
using NPin.Framework.SqlSugarCore.Abstractions;
using NPin.Framework.Upms.Domain.Entities;
using NPin.Framework.Upms.Domain.Entities.ValueObjects;
using NPin.Framework.Upms.Domain.Settings;
using NPin.Framework.Upms.Domain.Shared.Enums;
using NPin.Framework.Upms.Domain.Shared.Options;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace NPin.Framework.Upms.SqlSugarCore.DataSeeds;
public class UserDataSeed : IDataSeedContributor, ITransientDependency
{
public ILogger<UserDataSeed> Logger { get; set; }
private readonly ISettingManager _settingManager;
private readonly UpmsOptions _options;
private readonly ISqlSugarRepository<UserEntity> _userRepository;
public UserDataSeed(ISettingManager settingManager, IOptions<UpmsOptions> options,
ISqlSugarRepository<UserEntity> userRepository
)
{
_settingManager = settingManager;
_options = options.Value;
_userRepository = userRepository;
Logger = NullLogger<UserDataSeed>.Instance;
}
public async Task SeedAsync(DataSeedContext context)
{
if (await _userRepository.IsAnyAsync(x => true))
{
return;
}
Logger.LogDebug("开始注入用户种子数据...");
// 获取系统配置数据
var sysSetting = await _settingManager.GetSysSettingAsync(_options);
var users = new List<UserEntity>
{
new()
{
Username = "admin",
PhoneNumber = "18888888888",
Email = "admin@npin.com",
Nickname = "超级管理员",
Introduction = "超级管理员,具有所有权限。",
Gender = GenderEnum.Secrecy,
IpAddr = "127.0.0.1",
EncryptPassword = new EncryptPasswordValueObject(sysSetting.AdminPassword),
IsEnabled = true,
OrderNum = 0,
}
};
foreach (var user in users)
{
user.BuildPassword();
}
await _userRepository.InsertManyAsync(users);
}
}
Loading…
Cancel
Save