forked from gongxuegit/tiku-backend.net
feat: modularize external providers
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp;
|
||||
using Senparc.Weixin.WxOpen.Containers;
|
||||
using Tiku.Application.Growth;
|
||||
using Tiku.Application.Storage;
|
||||
using Tiku.Application.Tenancy;
|
||||
using Tiku.Domain.Tenancy;
|
||||
using Tiku.Infrastructure.Persistence;
|
||||
|
||||
namespace Tiku.Infrastructure.Growth;
|
||||
|
||||
public sealed class ReferralQrcodeGenerator(
|
||||
TikuDbContext dbContext,
|
||||
IObjectStorageService objectStorageService) : IReferralQrcodeGenerator
|
||||
IObjectStorageService objectStorageService,
|
||||
ITenantExternalProviderConfigService providerConfigService) : IReferralQrcodeGenerator
|
||||
{
|
||||
private static readonly string[] WechatMiniappProviderAliases =
|
||||
[
|
||||
@@ -84,17 +83,23 @@ public sealed class ReferralQrcodeGenerator(
|
||||
Guid tenantId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var providers = await dbContext.TenantAuthProviders
|
||||
.AsNoTracking()
|
||||
.Where(entity =>
|
||||
entity.TenantId == tenantId &&
|
||||
WechatMiniappProviderAliases.Contains(entity.Provider) &&
|
||||
(entity.Status == TenantAuthProviderStatus.Active ||
|
||||
entity.Status == TenantAuthProviderStatus.Testing))
|
||||
.ToListAsync(cancellationToken);
|
||||
var provider = WechatMiniappProviderAliases
|
||||
.Select(alias => providers.FirstOrDefault(entity => entity.Provider == alias))
|
||||
.FirstOrDefault(entity => entity is not null);
|
||||
TenantExternalProviderAccount? provider = null;
|
||||
foreach (var alias in WechatMiniappProviderAliases)
|
||||
{
|
||||
try
|
||||
{
|
||||
provider = await providerConfigService.GetActiveProviderAsync(
|
||||
tenantId,
|
||||
TenantExternalProviderCapability.Identity,
|
||||
alias,
|
||||
cancellationToken);
|
||||
break;
|
||||
}
|
||||
catch (TenantExternalProviderException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if (provider is null)
|
||||
{
|
||||
throw new ReferralException(
|
||||
@@ -103,7 +108,7 @@ public sealed class ReferralQrcodeGenerator(
|
||||
}
|
||||
|
||||
var appId = GetJsonString(provider.ConfigPublic, "appId", "clientId");
|
||||
var appSecret = GetJsonString(provider.ConfigPublic, "appSecret", "clientSecret", "secret");
|
||||
var appSecret = GetJsonString(provider.SecretPayload, "appSecret", "clientSecret", "secret");
|
||||
if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(appSecret))
|
||||
{
|
||||
throw new ReferralException(
|
||||
|
||||
Reference in New Issue
Block a user