From d069ff4727f2ca2192afb3e26cc88cf216d3b3e2 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sat, 11 May 2024 14:36:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=8D=E5=AD=90=E6=95=B0=E6=8D=AE+?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NPin.Framework.Upms.Application.csproj | 3 + .../Consts/ConfigConst.cs | 18 -- .../Consts/ConfigKeyConst.cs | 32 ++++ .../Options/UpmsOptions.cs | 62 +++---- .../EventHandlers/LoginEventHandler.cs | 63 +++---- .../OperLog/OperLogGlobalAttribute.cs | 157 +++++++++--------- .../Settings/UpmsSettingManagerExtensions.cs | 82 ++++++++- .../Settings/UpmsStaticSettingProvider.cs | 24 ++- .../DataSeeds/RoleDataSeed.cs | 25 +++ .../DataSeeds/SmsConfigDataSeed.cs | 27 +-- .../DataSeeds/SysSettingDataSeed.cs | 48 ++++++ .../DataSeeds/UserDataSeed.cs | 68 ++++++++ 12 files changed, 435 insertions(+), 174 deletions(-) delete mode 100644 module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigConst.cs create mode 100644 module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigKeyConst.cs create mode 100644 module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/RoleDataSeed.cs create mode 100644 module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SysSettingDataSeed.cs create mode 100644 module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/UserDataSeed.cs diff --git a/module/upms/NPin.Framework.Upms.Application/NPin.Framework.Upms.Application.csproj b/module/upms/NPin.Framework.Upms.Application/NPin.Framework.Upms.Application.csproj index 7ec7c6f..ec3189b 100644 --- a/module/upms/NPin.Framework.Upms.Application/NPin.Framework.Upms.Application.csproj +++ b/module/upms/NPin.Framework.Upms.Application/NPin.Framework.Upms.Application.csproj @@ -11,5 +11,8 @@ + + + diff --git a/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigConst.cs b/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigConst.cs deleted file mode 100644 index 7c5e731..0000000 --- a/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigConst.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace NPin.Framework.Upms.Domain.Shared.Consts; - -public class ConfigConst -{ - /// - /// 系统配置前缀 - /// - public const string SysConfigPrefix = "sys"; - - /// - /// 短信配置相关 - /// - 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"; -} \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigKeyConst.cs b/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigKeyConst.cs new file mode 100644 index 0000000..3e7e275 --- /dev/null +++ b/module/upms/NPin.Framework.Upms.Domain.Shared/Consts/ConfigKeyConst.cs @@ -0,0 +1,32 @@ +namespace NPin.Framework.Upms.Domain.Shared.Consts; + +public class ConfigKeyConst +{ + /// + /// 系统配置前缀 + /// + public const string SysConfigPrefix = "sys"; + + /// + /// 系统设置 + /// + 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"; + + /// + /// 短信配置相关 + /// + 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"; +} \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain.Shared/Options/UpmsOptions.cs b/module/upms/NPin.Framework.Upms.Domain.Shared/Options/UpmsOptions.cs index ba14c39..4e24eb1 100644 --- a/module/upms/NPin.Framework.Upms.Domain.Shared/Options/UpmsOptions.cs +++ b/module/upms/NPin.Framework.Upms.Domain.Shared/Options/UpmsOptions.cs @@ -1,32 +1,32 @@ -namespace NPin.Framework.Upms.Domain.Shared.Options; - -/// -/// Upms模块的总体配置 -/// -public class UpmsOptions -{ - /// - /// 超级管理员默认密码 - /// - public string AdminPassword { get; set; } = "a123456"; - - /// - /// 租户超级管理员默认密码 - /// - public string TenantAdminPassword { get; set; } = "a123456"; - - /// - /// 是否开启登录图形验证码 - /// - public bool EnableCaptcha { get; set; } = false; - - /// - /// 是否开启注册功能 - /// - public bool EnableRegister { get; set; } = false; - - /// - /// 是否开启数据库自动o备份功能 - /// - public bool EnableDataBaseBackup { get; set; } = false; +namespace NPin.Framework.Upms.Domain.Shared.Options; + +/// +/// Upms模块的默认总体配置 +/// +public class UpmsOptions +{ + /// + /// 超级管理员默认密码 + /// + public string AdminPassword { get; set; } = "a123456"; + + /// + /// 租户超级管理员默认密码 + /// + public string TenantAdminPassword { get; set; } = "a123456"; + + /// + /// 是否开启登录图形验证码 + /// + public bool EnableCaptcha { get; set; } = false; + + /// + /// 是否开启注册功能 + /// + public bool EnableRegister { get; set; } = false; + + /// + /// 是否开启数据库自动o备份功能 + /// + public bool EnableDataBaseBackup { get; set; } = false; } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain/EventHandlers/LoginEventHandler.cs b/module/upms/NPin.Framework.Upms.Domain/EventHandlers/LoginEventHandler.cs index b1ad395..88fd809 100644 --- a/module/upms/NPin.Framework.Upms.Domain/EventHandlers/LoginEventHandler.cs +++ b/module/upms/NPin.Framework.Upms.Domain/EventHandlers/LoginEventHandler.cs @@ -1,32 +1,33 @@ -using Mapster; -using Microsoft.Extensions.Logging; -using NPin.Framework.Upms.Domain.Entities; -using NPin.Framework.Upms.Domain.Shared.Etos; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Repositories; -using Volo.Abp.EventBus; - -namespace NPin.Framework.Upms.Domain.EventHandlers; - -public class LoginEventHandler : ILocalEventHandler, ITransientDependency -{ - private readonly ILogger _logger; - private readonly IRepository _repository; - - public LoginEventHandler(ILogger logger, IRepository repository) - { - _logger = logger; - _repository = repository; - } - - public async Task HandleEventAsync(LoginEventArgs eventData) - { - _logger.LogInformation($"用户[{eventData.UserId}:{eventData.Username}]登录"); - var loginLogEntity = eventData.Adapt(); - loginLogEntity.LoginMsg = $"{eventData.Username}登录系统"; - loginLogEntity.LoginUser = eventData.Username; - loginLogEntity.LoginUserId = eventData.UserId; - // 插入 - await _repository.InsertAsync(loginLogEntity); - } +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; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.EventBus; + +namespace NPin.Framework.Upms.Domain.EventHandlers; + +public class LoginEventHandler : ILocalEventHandler, ITransientDependency +{ + public ILogger Logger { get; set; } + private readonly IRepository _repository; + + public LoginEventHandler(IRepository repository) + { + Logger = NullLogger.Instance; + _repository = repository; + } + + public async Task HandleEventAsync(LoginEventArgs eventData) + { + Logger.LogInformation($"用户[{eventData.UserId}:{eventData.Username}]登录"); + var loginLogEntity = eventData.Adapt(); + loginLogEntity.LoginMsg = $"{eventData.Username}登录系统"; + loginLogEntity.LoginUser = eventData.Username; + loginLogEntity.LoginUserId = eventData.UserId; + // 插入 + await _repository.InsertAsync(loginLogEntity); + } } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain/OperLog/OperLogGlobalAttribute.cs b/module/upms/NPin.Framework.Upms.Domain/OperLog/OperLogGlobalAttribute.cs index eb60a52..c6a3e4f 100644 --- a/module/upms/NPin.Framework.Upms.Domain/OperLog/OperLogGlobalAttribute.cs +++ b/module/upms/NPin.Framework.Upms.Domain/OperLog/OperLogGlobalAttribute.cs @@ -1,79 +1,80 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using NPin.Framework.Core.Extensions; -using NPin.Framework.Upms.Domain.Shared.OperLog; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Repositories; -using Volo.Abp.Users; - -namespace NPin.Framework.Upms.Domain.OperLog; - -public class OperLogGlobalAttribute : ActionFilterAttribute, ITransientDependency -{ - private readonly ILogger _logger; - private IRepository _repository; - private ICurrentUser _currentUser; - - public OperLogGlobalAttribute(ILogger logger, IRepository repository, - ICurrentUser currentUser) - { - _logger = logger; - _repository = repository; - _currentUser = currentUser; - } - - public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) - { - // 获取执行结果 - var resultContext = await next.Invoke(); - - // 判断是否在 控制器方法 上 - if (resultContext.ActionDescriptor is not ControllerActionDescriptor contextActionDescriptor) return; - - // 查找特性 - var operLogAttribute = contextActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) - .FirstOrDefault(a => a.GetType() == typeof(OperLogAttribute)) as OperLogAttribute; - // 无特性表达式 直接返回 不处理 - if (operLogAttribute is null) return; - - // 获取Ip - var logEntity = OperationLogEntity.GetInfoByHttpContext(resultContext.HttpContext); - - logEntity.OperType = operLogAttribute.OperType; - logEntity.Title = operLogAttribute.Title; - logEntity.RequestMethod = resultContext.HttpContext.Request.Method; - logEntity.Method = resultContext.HttpContext.Request.Path.Value; - logEntity.OperUser = _currentUser.UserName; - - // 请求结果保存 - if (operLogAttribute.IsSaveResponseData) - { - if (resultContext.Result is ContentResult { ContentType: "application/json" } result) - { - logEntity.RequestResult = result.Content?.Replace("\r\n", "").Trim(); - } - - if (resultContext.Result is JsonResult result2) - { - logEntity.RequestResult = result2.Value?.ToString(); - } - - if (resultContext.Result is ObjectResult result3) - { - logEntity.RequestResult = JsonConvert.SerializeObject(result3); - } - } - - // 请求参数保存 - if (operLogAttribute.IsSaveRequestData) - { - // 不建议保存 比较吃性能 - // logEntity.RequestParam = context.HttpContext.GetRequestValue(); - } - - await _repository.InsertAsync(logEntity); - } +using Microsoft.AspNetCore.Mvc; +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; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Users; + +namespace NPin.Framework.Upms.Domain.OperLog; + +public class OperLogGlobalAttribute : ActionFilterAttribute, ITransientDependency +{ + public ILogger Logger { get; set; } + private IRepository _repository; + private ICurrentUser _currentUser; + + public OperLogGlobalAttribute(IRepository repository, + ICurrentUser currentUser) + { + Logger = NullLogger.Instance; + _repository = repository; + _currentUser = currentUser; + } + + public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + // 获取执行结果 + var resultContext = await next.Invoke(); + + // 判断是否在 控制器方法 上 + if (resultContext.ActionDescriptor is not ControllerActionDescriptor contextActionDescriptor) return; + + // 查找特性 + var operLogAttribute = contextActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) + .FirstOrDefault(a => a.GetType() == typeof(OperLogAttribute)) as OperLogAttribute; + // 无特性表达式 直接返回 不处理 + if (operLogAttribute is null) return; + + // 获取Ip + var logEntity = OperationLogEntity.GetInfoByHttpContext(resultContext.HttpContext); + + logEntity.OperType = operLogAttribute.OperType; + logEntity.Title = operLogAttribute.Title; + logEntity.RequestMethod = resultContext.HttpContext.Request.Method; + logEntity.Method = resultContext.HttpContext.Request.Path.Value; + logEntity.OperUser = _currentUser.UserName; + + // 请求结果保存 + if (operLogAttribute.IsSaveResponseData) + { + if (resultContext.Result is ContentResult { ContentType: "application/json" } result) + { + logEntity.RequestResult = result.Content?.Replace("\r\n", "").Trim(); + } + + if (resultContext.Result is JsonResult result2) + { + logEntity.RequestResult = result2.Value?.ToString(); + } + + if (resultContext.Result is ObjectResult result3) + { + logEntity.RequestResult = JsonConvert.SerializeObject(result3); + } + } + + // 请求参数保存 + if (operLogAttribute.IsSaveRequestData) + { + // 不建议保存 比较吃性能 + // logEntity.RequestParam = context.HttpContext.GetRequestValue(); + } + + await _repository.InsertAsync(logEntity); + } } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsSettingManagerExtensions.cs b/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsSettingManagerExtensions.cs index 891e93a..f2b7a09 100644 --- a/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsSettingManagerExtensions.cs +++ b/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsSettingManagerExtensions.cs @@ -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> 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 } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsStaticSettingProvider.cs b/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsStaticSettingProvider.cs index 087b429..61244c7 100644 --- a/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsStaticSettingProvider.cs +++ b/module/upms/NPin.Framework.Upms.Domain/Settings/UpmsStaticSettingProvider.cs @@ -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)); } } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/RoleDataSeed.cs b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/RoleDataSeed.cs new file mode 100644 index 0000000..fb9d948 --- /dev/null +++ b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/RoleDataSeed.cs @@ -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 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.Instance; + // } + + public async Task SeedAsync(DataSeedContext context) + { + // throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SmsConfigDataSeed.cs b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SmsConfigDataSeed.cs index 9c4c2ff..969d5ba 100644 --- a/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SmsConfigDataSeed.cs +++ b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SmsConfigDataSeed.cs @@ -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 _logger; + public ILogger Logger { get; set; } private readonly ISettingManager _settingManager; - public SmsConfigDataSeed(ILogger logger, ISettingManager settingManager) + public SmsConfigDataSeed(ISettingManager settingManager) { - _logger = logger; + Logger = NullLogger.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("注入短信配置种子数据成功"); } } \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SysSettingDataSeed.cs b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SysSettingDataSeed.cs new file mode 100644 index 0000000..8f066bb --- /dev/null +++ b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/SysSettingDataSeed.cs @@ -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 Logger { get; set; } + private readonly ISettingManager _settingManager; + private readonly UpmsOptions _options; + + public SysSettingDataSeed(ISettingManager settingManager, IOptions options) + { + _settingManager = settingManager; + _options = options.Value; + Logger = NullLogger.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("注入系统设置种子数据成功"); + } +} \ No newline at end of file diff --git a/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/UserDataSeed.cs b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/UserDataSeed.cs new file mode 100644 index 0000000..b4d8cf0 --- /dev/null +++ b/module/upms/NPin.Framework.Upms.SqlSugarCore/DataSeeds/UserDataSeed.cs @@ -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 Logger { get; set; } + private readonly ISettingManager _settingManager; + private readonly UpmsOptions _options; + private readonly ISqlSugarRepository _userRepository; + + public UserDataSeed(ISettingManager settingManager, IOptions options, + ISqlSugarRepository userRepository + ) + { + _settingManager = settingManager; + _options = options.Value; + _userRepository = userRepository; + Logger = NullLogger.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 + { + 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); + } +} \ No newline at end of file