fengling-console/Services/H5LinkService.cs
movingsam 4d2127637d refactor: clean up Member module and update Console
- Remove redundant PointsRule repositories (use single PointsRuleRepository)
- Clean up Member migrations and consolidate to single Init migration
- Update Console frontend API and components for Tenant
- Add H5LinkService for member H5 integration
2026-02-18 23:34:40 +08:00

58 lines
1.7 KiB
C#

using Fengling.Console.Models.Entities;
using Fengling.Console.Repositories;
using Fengling.Platform.Domain.AggregatesModel.TenantAggregate;
using Microsoft.Extensions.Configuration;
using QRCoder;
namespace Fengling.Console.Services;
public interface IH5LinkService
{
Task<H5LinkResult> GenerateH5LinkAsync(long tenantId);
}
public class H5LinkResult
{
public string Link { get; set; } = string.Empty;
public string QrCodeBase64 { get; set; } = string.Empty;
}
public class H5LinkService : IH5LinkService
{
private readonly ITenantRepository _tenantRepository;
private readonly IConfiguration _configuration;
public H5LinkService(ITenantRepository tenantRepository, IConfiguration configuration)
{
_tenantRepository = tenantRepository;
_configuration = configuration;
}
public async Task<H5LinkResult> GenerateH5LinkAsync(long tenantId)
{
var tenant = await _tenantRepository.GetByIdAsync(tenantId)
?? throw new KeyNotFoundException($"Tenant with ID {tenantId} not found");
var link = GenerateLink(tenant);
var qrCodeBase64 = GenerateQrCode(link);
return new H5LinkResult
{
Link = link,
QrCodeBase64 = qrCodeBase64
};
}
private string GenerateLink(Tenant tenant)
{
var baseUrl = _configuration["AppSettings:DefaultH5BaseUrl"] ?? "https://h5.example.com";
return $"{baseUrl.TrimEnd('/')}/{tenant.TenantCode}";
}
private string GenerateQrCode(string content)
{
byte[] qrCodeBytes = PngByteQRCodeHelper.GetQRCode(content, QRCodeGenerator.ECCLevel.M, 20);
return Convert.ToBase64String(qrCodeBytes);
}
}