using AgileConfig.Client; using HuanMeng.DotNetCore.MultiTenant.Contract; using HuanMeng.DotNetCore.MultiTenant; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using XLib.DotNetCore.CacheHelper; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Protocols; using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; using HuanMeng.MiaoYu.Code.Payment; using System.Collections.Frozen; using HuanMeng.MiaoYu.Code.Music; namespace HuanMeng.MiaoYu.Code.AppExtend { /// /// app配置项扩展 /// public static class AppConfigurationExtend { /// /// 配置数据 /// //public static ConfigClient AppConfigClient { get; set; } /// /// /// public static ConfigurationManager ConfigurationManager { get; set; } /// /// /// public static ConcurrentDictionary AppConfigs { get; set; } = new ConcurrentDictionary(); /// /// 获取配置项 /// /// /// public static AppConfig GetAppConfig(string domainName) { if (AppConfigs.TryGetValue(domainName, out var appConfig)) { return appConfig; } if (AppConfigs.TryGetValue("default", out appConfig)) { return appConfig; } return AppConfigs.FirstOrDefault().Value; } /// /// 获取配置项 /// /// /// public static AppConfig? GetAppConfigIdentifier(string identifier) { var app = AppConfigs.Where(it => it.Value.Identifier == identifier).Select(it => it.Value).FirstOrDefault(); return app; } /// /// 配置版本号 /// public static string AppVersion { get; set; } /// /// /// /// public static void ConfigClient_ConfigChanged(ConfigReloadedArgs args) { if (args.OldConfigs.TryGetValue("Version", out var vresion) && args.NewConfigs.TryGetValue("Version", out var newVersion)) { if (vresion != newVersion) { MemoryCacheHelper.cache.Clear(); } } VerifyTenant(args); if (VerifyTenant(args, "Payment:WeChatConfig")) { PaymentExtend.AddWeChat(ConfigurationManager); } if (VerifyTenant(args, "Payment:AlipayConfig")) { PaymentExtend.AddAlipay(ConfigurationManager); } } /// /// 初始化 /// /// /// public static WebApplicationBuilder AddAppConfigClient(this WebApplicationBuilder builder) { var configClient = new ConfigClient(builder.Configuration); builder.Host.UseAgileConfig(configClient, ConfigClient_ConfigChanged); //AppConfigClient = configClient; ConfigurationManager = builder.Configuration; AppConfigInit(builder.Configuration); builder.Services.AddScoped(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); return builder; } #region 租户 /// /// /// /// /// /// public static ITenantInfo ToITenantInfo(this AppConfig appConfig, ITenantInfo? tenantInfo = null) { if (tenantInfo == null) { tenantInfo = new TenantInfo(); } tenantInfo.TenantId = appConfig.TenantId; tenantInfo.Identifier = appConfig.Identifier; tenantInfo.Name = appConfig.Name; tenantInfo.ConnectionString = appConfig.ConnectionString; return tenantInfo; } /// /// /// /// /// /// public static AppConfig ToAppConfig(this AppConfig appConfig, AppConfig? newAppConfig = null) { if (newAppConfig == null) { newAppConfig = new AppConfig(); } newAppConfig.TenantId = appConfig.TenantId; newAppConfig.Identifier = appConfig.Identifier; newAppConfig.Name = appConfig.Name; newAppConfig.ConnectionString = appConfig.ConnectionString; newAppConfig.DomainName = appConfig.DomainName; newAppConfig.RedisConnectionString = appConfig.RedisConnectionString; if (appConfig.Payment == null) { appConfig.Payment = new PaymentModel() { }; } newAppConfig.Payment = appConfig.Payment; return newAppConfig; } /// /// 验证多租户 /// /// private static bool VerifyTenant(ConfigReloadedArgs args, string key) { var newTenant = args.NewConfigs.Where(it => it.Key.Contains(key)).ToDictionary(); var oldTenant = args.OldConfigs.Where(it => it.Key.Contains(key)).ToDictionary(); bool areEqual = newTenant.Count == oldTenant.Count && !newTenant.Except(oldTenant).Any(); if (!areEqual) { //更新缓存 //AppConfigInit(ConfigurationManager); return true; } return false; } /// /// 验证多租户 /// /// private static void VerifyTenant(ConfigReloadedArgs args) { var newTenant = args.NewConfigs.Where(it => it.Key.Contains("Tenant")).ToDictionary(); var oldTenant = args.OldConfigs.Where(it => it.Key.Contains("Tenant")).ToDictionary(); bool areEqual = newTenant.Count == oldTenant.Count && !newTenant.Except(oldTenant).Any(); if (!areEqual) { //更新缓存 AppConfigInit(ConfigurationManager); } } /// /// 初始化租户数据 /// /// private static void AppConfigInit(ConfigurationManager configurationManager) { var tenants = configurationManager.GetSection("Tenant").Get>(); if (tenants != null) { ConcurrentDictionary _AppConfigs = new ConcurrentDictionary(); if (tenants.Count > 0) { tenants?.ForEach(t => { if (!_AppConfigs.TryAdd(t.DomainName, t)) { Console.WriteLine($"{t.DomainName}配置加载失败"); } if (t.Name == "default") { _AppConfigs.TryAdd("default", t); } }); if (!_AppConfigs.TryGetValue("default", out var x)) { _AppConfigs.TryAdd("default", tenants[0]); } } else { _AppConfigs.TryAdd("default", new AppConfig()); } AppConfigs = _AppConfigs; } } #endregion #region 测试用户 /// /// 测试账号数据 /// public static ConcurrentDictionary>? TestAccountList { get; set; } /// /// 获取测试账号 /// /// /// public static FrozenDictionary GetTestAccount(DAO dao) { if (TestAccountList == null) { TestAccountList = new ConcurrentDictionary>(); } var t = dao.daoDbMiaoYu.context.TenantInfo.TenantId; if (TestAccountList.ContainsKey(t)) { return TestAccountList[t]; } var dir = dao.daoDbMiaoYu.context.T_User.Where(it => it.IsTest == true && it.State == 0).Select(it => new { PhoneNumAccount = dao.daoDbMiaoYu.context.T_User_Phone_Account.FirstOrDefault(item => item.UserId == it.Id).PhoneNum, userid = it.Id }).Where(it => !string.IsNullOrEmpty(it.PhoneNumAccount)).ToDictionary(it => it.PhoneNumAccount, kvp => kvp.userid); var f = dir.ToFrozenDictionary(); TestAccountList.TryAdd(t, f); return f; } /// /// 是否是测试账号 /// /// /// /// public static bool IsTestAccount(string phoneNum, DAO dao) { var t = GetTestAccount(dao); return t.ContainsKey(phoneNum); } /// /// 是否是测试账号 /// /// /// /// public static bool IsTestAccount(int userId, DAO dao) { var t = GetTestAccount(dao); return t.Where(it => it.Value == userId).Any(); } #endregion } }