feat: 增加系统上下文服务

This commit is contained in:
Argo-Tianyi 2022-01-24 00:48:09 +08:00
parent ebf60eabf7
commit 24fe9e7d35
5 changed files with 53 additions and 35 deletions

View File

@ -0,0 +1,36 @@
using Microsoft.Extensions.Configuration;
namespace BootstrapClient.Web.Shared.Services
{
/// <summary>
/// AppContext 实体类
/// </summary>
public class BootstrapAppContext
{
/// <summary>
/// 获得/设置 当前网站 AppId
/// </summary>
public string AppId { get; }
/// <summary>
/// 获得/设置 当前登录账号
/// </summary>
[NotNull]
public string? UserName { get; set; }
/// <summary>
/// 获得/设置 当前用户显示名称
/// </summary>
[NotNull]
public string? DisplayName { get; internal set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="configuration"></param>
public BootstrapAppContext(IConfiguration configuration)
{
AppId = configuration.GetValue("AppId", "BA");
}
}
}

View File

@ -8,7 +8,7 @@
<Header>
<span class="ml-3 flex-sm-fill d-none d-sm-block">Bootstrap of Blazor</span>
<Widget></Widget>
<Logout ImageUrl="_content/BootstrapClient.Web.Shared/images/Argo.png" DisplayName="@DisplayName" UserName="@UserName">
<Logout ImageUrl="_content/BootstrapClient.Web.Shared/images/Argo.png" DisplayName="@Context.DisplayName" UserName="@Context.UserName">
<LinkTemplate>
<a href="@ProfileUrl"><i class="fa fa-suitcase"></i>个人中心</a>
<a href="@SettingsUrl"><i class="fa fa-cog"></i>设置</a>
@ -28,7 +28,7 @@
<div class="layout-user">
<img class="layout-avatar" src="_content/BootstrapClient.Web.Shared/images/Argo-C.png">
<div class="layout-title">
<span>@DisplayName</span>
<span>@Context.DisplayName</span>
</div>
<div class="layout-user-state"></div>
</div>

View File

@ -2,10 +2,10 @@
using BootstrapBlazor.Components;
using BootstrapClient.Web.Core;
using BootstrapClient.Web.Shared.Extensions;
using BootstrapClient.Web.Shared.Services;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace BootstrapClient.Web.Shared.Shared;
@ -37,22 +37,10 @@ public sealed partial class MainLayout
private string? NotificationUrl { get; set; }
/// <summary>
/// 获得 当前用户登录显示名称
/// </summary>
[NotNull]
public string? DisplayName { get; private set; }
private string? Title { get; set; }
private string? Footer { get; set; }
/// <summary>
/// 获得 当前用户登录名
/// </summary>
[NotNull]
public string? UserName { get; private set; }
[Inject]
[NotNull]
private IBootstrapAdminService? SecurityService { get; set; }
@ -83,9 +71,7 @@ public sealed partial class MainLayout
[Inject]
[NotNull]
private IConfiguration? Configuration { get; set; }
private string? AppId { get; set; }
private BootstrapAppContext? Context { get; set; }
/// <summary>
/// OnInitialized 方法
@ -94,10 +80,9 @@ public sealed partial class MainLayout
{
base.OnInitialized();
AppId = Configuration.GetValue("AppId", "Blazor");
ProfileUrl = CombinePath(DictsService.GetProfileUrl(AppId));
SettingsUrl = CombinePath(DictsService.GetSettingsUrl(AppId));
NotificationUrl = CombinePath(DictsService.GetNotificationUrl(AppId));
ProfileUrl = CombinePath(DictsService.GetProfileUrl(Context.AppId));
SettingsUrl = CombinePath(DictsService.GetSettingsUrl(Context.AppId));
NotificationUrl = CombinePath(DictsService.GetNotificationUrl(Context.AppId));
}
private string CombinePath(string? url)
@ -117,12 +102,12 @@ public sealed partial class MainLayout
if (!string.IsNullOrEmpty(userName))
{
UserName = userName;
Context.UserName = userName;
var user = UsersService.GetUserByUserName(userName);
MenuItems = NavigationsService.GetMenus(userName).ToClientMenus();
MenuItems = NavigationsService.GetMenus(userName).Where(i => i.Application == Context.AppId).ToMenus();
DisplayName = user?.DisplayName ?? "未注册账户";
Context.DisplayName = user?.DisplayName ?? "未注册账户";
Title = DictsService.GetWebTitle();
Footer = DictsService.GetWebFooter();
}
@ -130,10 +115,10 @@ public sealed partial class MainLayout
private Task<bool> OnAuthorizing(string url)
{
return SecurityService.AuhorizingNavigation(UserName, url);
return SecurityService.AuhorizingNavigation(Context.UserName, url);
}
private string LogoutUrl => CombinePath($"/Account/Logout?AppId={AppId}");
private string LogoutUrl => CombinePath($"/Account/Logout?AppId={Context.AppId}");
private string AuthorUrl => CombinePath($"/Account/Login?ReturnUrl={NavigationManager.Uri}&AppId={AppId}");
private string AuthorUrl => CombinePath($"/Account/Login?ReturnUrl={NavigationManager.Uri}&AppId={Context.AppId}");
}

View File

@ -1,7 +0,0 @@
namespace BootstrapClient.Web.Shared.Utils;
class AppContext
{
[NotNull]
public string? AppId { get; set; }
}

View File

@ -1,4 +1,5 @@
using BootstrapClient.Web.Core.Services;
using BootstrapClient.Web.Shared.Services;
using PetaPoco;
using PetaPoco.Providers;
@ -24,6 +25,9 @@ namespace Microsoft.Extensions.DependencyInjection
// 增加认证授权服务
services.AddBootstrapAdminSecurity<AdminService>();
// 增加 BootstrapApp 上下文服务
services.AddScoped<BootstrapAppContext>();
// 增加 PetaPoco 数据服务
services.AddPetaPocoDataAccessServices((provider, builder) =>
{