using AutoMapper; using ChouBox.Code.TencentCloudExtend.Model; using ChouBox.Model.Entities; using HuanMeng.DotNetCore.Base; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using StackExchange.Redis; using System.Threading.Tasks; namespace ChouBox.Code.AppExtend; /// /// bll 基础类 /// public class ChouBoxCodeBase { /// /// _serviceProvider,提供基本依赖注入支持 /// protected readonly IServiceProvider _serviceProvider; /// /// 构造函数 /// /// public ChouBoxCodeBase(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } private EfCoreDaoBase? _dao; /// /// 数据库, /// 更新删除,尽量只操作本bll实例dao获取到的对象,取和存要同一个dao /// public EfCoreDaoBase Dao { get { if (_dao == null) { _dao = new EfCoreDaoBase(_serviceProvider.GetRequiredService()); } return _dao; } } #region 映射 private IMapper _mapper; /// /// DTO 映射 /// public virtual IMapper Mapper { get { if (_mapper == null) { _mapper = _serviceProvider.GetRequiredService(); } return _mapper; } set { _mapper = value; } } #endregion #region 请求信息 private IHttpContextAccessor _httpContextAccessor; /// /// HttpContextAccessor /// public IHttpContextAccessor HttpContextAccessor { get { if (_httpContextAccessor == null) { _httpContextAccessor = _serviceProvider.GetRequiredService(); } return _httpContextAccessor; } } #endregion #region 日志 private ILogger _logger; /// /// 日志 /// public ILogger Logger { get { if (_logger == null) { _logger = _serviceProvider.GetRequiredService>(); } return _logger; } } #endregion public IConfiguration Configuration() { return _serviceProvider.GetRequiredService(); } #region Redis private IDatabase _redis; /// /// Redis 缓存 /// public IDatabase RedisCache { get { if (_redis == null) { var connectionMultiplexer = _serviceProvider.GetRequiredService(); _redis = connectionMultiplexer.GetDatabase(); } return _redis; } } #endregion /// /// 获取腾讯云短信配置 /// /// public async Task GetTencentSMSConfigAsync() { var config = await this.GetConfigAsync("tencent_sms_config"); if (config == null) { config = new TencentSMSConfig() { SecretId = "AKIDLbhdP0Vs57yd7QZWu8A2jFbno8JKBUp6", SecretKey = "", ReqMethod = "POST", Timeout = 30, SmsSdkAppId = "1400923253", SignName = "上海寰梦科技发展", TemplateId = "2209122" }; } return config; } /// /// 获取系统配置 /// /// 配置关键词 /// 配置信息 public Dictionary GetConfig(string type) { // 生成缓存键 string cacheKey = $"config:{type}"; // 尝试从缓存获取数据 var cachedData = RedisCache.StringGet>(cacheKey); if (cachedData != null) { return cachedData; } // 从数据库查询 var content = Dao.Context.Config.Where(it => it.Key == type).FirstOrDefault(); Dictionary config = null; if (content != null) { var d = JsonConvert.DeserializeObject>(content.Value); RedisCache.StringSet(cacheKey, content.Value, TimeSpan.FromMinutes(10)); return d; } return null; } /// /// 获取系统配置 /// /// 实体类 /// 配置key /// public async Task GetConfigAsync(string type) where T : class { // 生成缓存键 string cacheKey = $"config:{type}"; // 尝试从缓存获取数据 var cachedData = RedisCache.StringGet(cacheKey); if (cachedData != null) { return cachedData; } // 从数据库查询 var content = await Dao.Context.Config.Where(it => it.Key == type).FirstOrDefaultAsync(); if (content != null && !string.IsNullOrEmpty(content.Value)) { var d = JsonConvert.DeserializeObject(content.Value); RedisCache.StringSet(cacheKey, content.Value, TimeSpan.FromMinutes(10)); return d; } return null; } }