92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
using CloudBuilder.Core.Application.Builders;
|
|
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
|
using CloudBuilder.Core.Policy;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CloudBuilder.Core.DatabaseAccessor
|
|
{
|
|
public class ApplicationService : IApplicationService
|
|
{
|
|
/// <summary>
|
|
/// 显式赋值的登录Id(登录流程/桌面端会主动赋值),为空时按当前请求的令牌解析
|
|
/// </summary>
|
|
private string? userId;
|
|
|
|
/// <summary>
|
|
/// 显式赋值的语言,为空时按当前请求的报文头解析
|
|
/// </summary>
|
|
private string? locale;
|
|
|
|
public ApplicationService(
|
|
CloudBuilderBuilder cloudBuilder,
|
|
IConfiguration configuration,
|
|
IServiceProvider serviceProvider,
|
|
IUnitOfWork unitOfWork,
|
|
IHttpContextAccessor httpContext
|
|
)
|
|
{
|
|
CloudBuilder = cloudBuilder;
|
|
this.Configuration = configuration;
|
|
this.ServiceProvider = serviceProvider;
|
|
this.UnitOfWork = unitOfWork;
|
|
this.HttpContext = httpContext;
|
|
}
|
|
|
|
public IServiceProvider ServiceProvider { get; private set; }
|
|
|
|
public IHttpContextAccessor HttpContext { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 当前登录Id:取自当前请求的令牌,每次访问都重新解析
|
|
/// (不能在构造函数中缓存,否则实例被长生命周期对象持有时会一直返回上一个登录用户)
|
|
/// </summary>
|
|
public string UserId
|
|
{
|
|
get
|
|
{
|
|
if (!string.IsNullOrEmpty(userId)) return userId!;
|
|
if (HttpContext?.HttpContext == null) return string.Empty;
|
|
return ClaimPolicy.GetClaimValue(HttpContext, ClaimPolicy.CLAINM_USERID);
|
|
}
|
|
set
|
|
{
|
|
userId = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前语言:取自当前请求的报文头,每次访问都重新解析
|
|
/// </summary>
|
|
public string Locale
|
|
{
|
|
get
|
|
{
|
|
if (!string.IsNullOrEmpty(locale)) return locale!;
|
|
if (HttpContext?.HttpContext == null) return ClaimPolicy.CLAINM_LOCALE;
|
|
string key = ClaimPolicy.GetHeaderKeyValue(HttpContext, ClaimPolicy.HEADER_LOCALE);
|
|
return string.IsNullOrEmpty(key) ? ClaimPolicy.CLAINM_LOCALE : key;
|
|
}
|
|
set
|
|
{
|
|
locale = value;
|
|
}
|
|
}
|
|
public IUnitOfWork UnitOfWork { get; set; }
|
|
public CloudBuilderBuilder CloudBuilder { get; set; }
|
|
public IConfiguration Configuration { get; private set; }
|
|
|
|
public KRepository GetRepository<KRepository>()
|
|
{
|
|
return ServiceProvider.GetRequiredService<KRepository>();
|
|
}
|
|
}
|
|
}
|