ABP vNext 多租户系统实现登录页自定义 Logo 的最佳实践

发布于:2025-05-20 ⋅ 阅读:(14) ⋅ 点赞:(0)

🚀 ABP vNext 多租户系统实现登录页自定义 Logo 的最佳实践


🧭 版本信息与运行环境

  • ABP Framework:v8.1.5
  • .NET SDK:8.0
  • 数据库:PostgreSQL(支持 SQLServer、MySQL 等)
  • BLOB 存储:本地/OSS/Azure Blob(推荐启用 CDN)
  • 部署建议:支持单库/多库多租户结构,推荐搭配 CI/CD 自动迁移


🔍 实现背景

ABP vNext 提供完备的多租户能力,支持根据请求自动切换上下文。在实际项目中,为每个租户提供个性化登录 Logo 是增强品牌感、提升用户体验的重要方式。


📦 总体思路与流程图

使用 ABP 的实体扩展和 Blob 模块,结合租户解析机制,动态加载 Logo:

访问登录页
TenantResolutionMiddleware
当前租户 ID 是否为空?
调用 AppService 获取 Logo URL
使用默认 Logo
渲染 Logo 图片

🧱 步骤一:扩展租户实体添加 Logo 字段

🔧 实体扩展配置流程

应用启动
触发 PreConfigureServices
调用 MyTenantEntityExtension.Configure()
ObjectExtensionManager 注册 LogoUrl 属性
生成 EF Core Migration 包含 LogoUrl 字段

💡 推荐位置*.Domain.Shared + *.Domain

// MyTenantConsts.cs
public static class MyTenantConsts
{
    public const string LogoUrlPropertyName = "LogoUrl";
}
// MyTenantEntityExtension.cs
public static class MyTenantEntityExtension
{
    public static void Configure()
    {
        ObjectExtensionManager.Instance
            .Modules()
            .ConfigureSaas(sa =>
            {
                sa.ConfigureTenant(t =>
                {
                    t.AddOrUpdateProperty<string>(MyTenantConsts.LogoUrlPropertyName);
                });
            });
    }
}

在模块类中 PreConfigureServices 阶段调用:

public override void PreConfigureServices(ServiceConfigurationContext context)
{
    MyTenantEntityExtension.Configure();
}

确保迁移脚本包含新字段


🖼 步骤二:上传并持久化租户 Logo

📥 Logo 上传与持久化流程

前端页面 TenantUiAppService BlobContainer ITenantRepository UploadLogoAsync(tenantId, file) 校验文件大小和格式 SaveAsync(blobPath, stream) 保存成功 GetAsync(tenantId) 返回 Tenant 实体 UpdateAsync(tenant.SetProperty) 持久化完成 返回带签名的下载 URL 前端页面 TenantUiAppService BlobContainer ITenantRepository

💡 建议封装至 Application Service:

public class TenantUiAppService : ApplicationService
{
    private readonly IBlobContainer _blobContainer;
    private readonly ITenantRepository _tenantRepository;

    public TenantUiAppService(IBlobContainer blobContainer, ITenantRepository tenantRepository)
    {
        _blobContainer = blobContainer;
        _tenantRepository = tenantRepository;
    }

    public async Task<string> UploadLogoAsync(Guid tenantId, IFormFile file)
    {
        if (file.Length > 1024 * 1024)
            throw new UserFriendlyException("文件不能超过 1MB");
        
        if (!file.ContentType.StartsWith("image/"))
            throw new UserFriendlyException("请上传 PNG 或 JPG 图片");

        var blobPath = $"{tenantId}/logo.png";

        using var stream = file.OpenReadStream();
        await _blobContainer.SaveAsync(blobPath, stream, overrideExisting: true);

        var tenant = await _tenantRepository.GetAsync(tenantId);
        tenant.SetProperty(MyTenantConsts.LogoUrlPropertyName, blobPath);
        await _tenantRepository.UpdateAsync(tenant); // 持久化

        return await _blobContainer.GetDownloadUrlAsync(blobPath); // 带签名 URL
    }
}

🌐 步骤三:登录页动态加载租户 Logo

⚙️ 登录页 Logo 加载流程

用户访问登录页
TenantUiQueryService.GetLogoUrlAsync()
CurrentTenant.Id 是否为空
返回默认 Logo 路径
从 Repository 获取 Tenant
ExtraProperties 中是否有 LogoUrl
BlobContainer.GetDownloadUrlAsync(rawPath)
返回签名 URL
前端 渲染

封装服务接口:

public class TenantUiQueryService : ApplicationService
{
    private readonly ITenantRepository _tenantRepository;
    private readonly IBlobContainer _blobContainer;

    public TenantUiQueryService(ITenantRepository tenantRepository, IBlobContainer blobContainer)
    {
        _tenantRepository = tenantRepository;
        _blobContainer = blobContainer;
    }

    public async Task<string> GetLogoUrlAsync()
    {
        var tenantId = CurrentTenant.Id;
        if (tenantId == null)
            return "/images/default-logo.png";

        var tenant = await _tenantRepository.GetAsync(tenantId.Value);

        if (!tenant.ExtraProperties.TryGetValue(MyTenantConsts.LogoUrlPropertyName, out var rawPath) ||
            rawPath == null)
        {
            return "/images/default-logo.png";
        }

        return await _blobContainer.GetDownloadUrlAsync(rawPath.ToString());
    }
}

前端渲染:

<img src="@logoUrl" onerror="this.src='/images/default-logo.png'" class="login-logo" />

🏗️ CI/CD 自动迁移流程

CI/CD Pipeline Trigger
Checkout 代码
Restore NuGet 包
Build 项目
为每个物理库执行迁移脚本
Update-Database 或 flyway migrate
部署新版本到目标环境

🧠 最佳实践建议

分类 建议
性能 租户 Logo 可使用 IDistributedCache 缓存
用户体验 加载失败 fallback 默认图
可扩展性 建议将租户个性化信息封装至 TenantUiCustomization 模块
安全 Blob 下载 URL 推荐使用带签名的临时访问
发布 配合数据库迁移工具(Flyway、DbUp)

网站公告

今日签到

点亮在社区的每一天
去签到