From 2232cd2eb9cfe8312b16d8e4266ed73882aed783 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Wed, 15 Dec 2021 10:31:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/DictsService.cs | 9 +++++++++ .../Services/DictsService.cs | 6 ++++-- .../Services/UsersService.cs | 2 +- .../admin/BootstrapAdmin.Web.Core/IUsers.cs | 2 +- .../Controllers/AccountController.cs | 1 + .../Services/BootstrapAppContext.cs | 5 +++++ .../Shared/MainLayout.razor.cs | 16 ++++++++++++++-- 7 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs index 9ec8d028..fd3e0174 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.EFCore/Services/DictsService.cs @@ -4,5 +4,14 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services { class DictsService : IDicts { + public string GetWebFooter() + { + throw new NotImplementedException(); + } + + public string GetWebTitle() + { + throw new NotImplementedException(); + } } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs index 8e8e6517..61857c78 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictsService.cs @@ -33,7 +33,8 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name; if (!string.IsNullOrEmpty(name)) { - title = dicts.First(d => d.Category == name && d.Name == "网站标题")?.Code ?? "网站标题"; + var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站标题") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站标题"); + title = dict?.Code ?? "网站标题"; } return title; } @@ -49,7 +50,8 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services var name = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == AppId)?.Name; if (!string.IsNullOrEmpty(name)) { - title = dicts.First(d => d.Category == name && d.Name == "网站页脚")?.Code ?? "网站页脚"; + var dict = dicts.FirstOrDefault(d => d.Category == name && d.Name == "网站页脚") ?? dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "网站页脚"); + title = dict?.Code ?? "网站标题"; } return title; } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs index 14c005ee..fa607e42 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UsersService.cs @@ -37,7 +37,7 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services /// /// /// - public string GetDisplayName(string userName) => Database.ExecuteScalar("select DisplayName from Users where UserName = @0", userName); + public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar("select DisplayName from Users where UserName = @0", userName); /// /// diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs index c1e23722..ed10a3a6 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IUsers.cs @@ -10,7 +10,7 @@ /// /// /// - string GetDisplayName(string userName); + string? GetDisplayName(string? userName); /// /// 通过用户名获取角色列表 diff --git a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs index 15c74ef2..ce6285ce 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs @@ -61,6 +61,7 @@ namespace Bootstrap.Admin.Controllers /// The login. /// /// + /// /// User name. /// Password. /// Remember. diff --git a/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs b/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs index 2f4042ce..1652cebc 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Services/BootstrapAppContext.cs @@ -10,6 +10,11 @@ /// public string AppId { get; } + /// + /// + /// + public string? UserName { get; set; } + /// /// /// diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index d7620416..a5b09f97 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -1,5 +1,6 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; +using Microsoft.AspNetCore.Components.Authorization; namespace BootstrapAdmin.Web.Shared { @@ -20,7 +21,7 @@ namespace BootstrapAdmin.Web.Shared [Inject] [NotNull] - private IUsers? UsersService { get; set; } + private AuthenticationStateProvider? AuthenticationStateProvider { get; set; } [Inject] [NotNull] @@ -43,7 +44,18 @@ namespace BootstrapAdmin.Web.Shared Title = DictsService.GetWebTitle(); Footer = DictsService.GetWebFooter(); - DisplayName = UsersService.GetDisplayName(); + } + + /// + /// + /// + /// + protected override async Task OnInitializedAsync() + { + var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + var userName = state.User.Identity?.Name; + DisplayName = UsersService.GetDisplayName(userName); + } } }