313 lines
10 KiB
C#
313 lines
10 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// app配置项扩展
|
|
/// </summary>
|
|
public static class AppConfigurationExtend
|
|
{
|
|
/// <summary>
|
|
/// 配置数据
|
|
/// </summary>
|
|
//public static ConfigClient AppConfigClient { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static ConfigurationManager ConfigurationManager { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static ConcurrentDictionary<string, AppConfig> AppConfigs { get; set; } = new ConcurrentDictionary<string, AppConfig>();
|
|
|
|
/// <summary>
|
|
/// 获取配置项
|
|
/// </summary>
|
|
/// <param name="domainName"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置项
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <returns></returns>
|
|
public static AppConfig? GetAppConfigIdentifier(string identifier)
|
|
{
|
|
var app = AppConfigs.Where(it => it.Value.Identifier == identifier).Select(it => it.Value).FirstOrDefault();
|
|
|
|
return app;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 配置版本号
|
|
/// </summary>
|
|
public static string AppVersion
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
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);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
/// <param name="builder"></param>
|
|
/// <returns></returns>
|
|
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<AppConfig>();
|
|
builder.Services.AddHostedService<AppConfigExtendServer>();
|
|
builder.Services.AddHostedService<MusicService>();
|
|
|
|
return builder;
|
|
|
|
}
|
|
#region 租户
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="appConfig"></param>
|
|
/// <param name="tenantInfo"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="appConfig"></param>
|
|
/// <param name="newAppConfig"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 验证多租户
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证多租户
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
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);
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 初始化租户数据
|
|
/// </summary>
|
|
/// <param name="configurationManager"></param>
|
|
private static void AppConfigInit(ConfigurationManager configurationManager)
|
|
{
|
|
var tenants = configurationManager.GetSection("Tenant").Get<List<AppConfig>>();
|
|
if (tenants != null)
|
|
{
|
|
ConcurrentDictionary<string, AppConfig> _AppConfigs = new ConcurrentDictionary<string, AppConfig>();
|
|
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 测试用户
|
|
/// <summary>
|
|
/// 测试账号数据
|
|
/// </summary>
|
|
public static ConcurrentDictionary<Guid, FrozenDictionary<string, int>>? TestAccountList { get; set; }
|
|
/// <summary>
|
|
/// 获取测试账号
|
|
/// </summary>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static FrozenDictionary<string, int> GetTestAccount(DAO dao)
|
|
{
|
|
if (TestAccountList == null)
|
|
{
|
|
TestAccountList = new ConcurrentDictionary<Guid, FrozenDictionary<string, int>>();
|
|
}
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否是测试账号
|
|
/// </summary>
|
|
/// <param name="phoneNum"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static bool IsTestAccount(string phoneNum, DAO dao)
|
|
{
|
|
var t = GetTestAccount(dao);
|
|
return t.ContainsKey(phoneNum);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否是测试账号
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static bool IsTestAccount(int userId, DAO dao)
|
|
{
|
|
var t = GetTestAccount(dao);
|
|
return t.Where(it => it.Value == userId).Any();
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
}
|