feat: 增加 Header 图标显示功能

This commit is contained in:
Argo-Tianyi 2022-01-06 09:31:01 +08:00
parent 06b3368a9f
commit 913f4db051
7 changed files with 26 additions and 6 deletions

View File

@ -170,4 +170,14 @@ class DictService : BaseDatabase, IDict
} }
return url; return url;
} }
/// <summary>
/// 获取头像路径
/// </summary>
/// <returns></returns>
public string RetrieveIconFolderPath()
{
var dicts = GetAll();
return dicts.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "images/uploder/";
}
} }

View File

@ -43,7 +43,7 @@ class UserService : BaseDatabase, IUser
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public string? GetDisplayName(string? userName) => string.IsNullOrEmpty(userName) ? "" : Database.ExecuteScalar<string>("select DisplayName from Users where UserName = @0", userName); public User? GetUserByUserName(string? userName) => string.IsNullOrEmpty(userName) ? null : Database.FirstOrDefault<User>("Where UserName = @0", userName);
/// <summary> /// <summary>
/// ///

View File

@ -117,5 +117,7 @@ namespace BootstrapAdmin.Web.Core
string? GetSettingsUrl(string appId); string? GetSettingsUrl(string appId);
string? GetNotificationUrl(string appId); string? GetNotificationUrl(string appId);
string RetrieveIconFolderPath();
} }
} }

View File

@ -12,7 +12,7 @@ public interface IUser
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
string? GetDisplayName(string? userName); User? GetUserByUserName(string? userName);
/// <summary> /// <summary>
/// 通过用户名获取角色列表 /// 通过用户名获取角色列表

View File

@ -6,7 +6,7 @@
UseTabSet="true" TabDefaultUrl="/Admin/Index"> UseTabSet="true" TabDefaultUrl="/Admin/Index">
<Header> <Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span> <span class="ms-3 flex-fill">Bootstrap of Blazor</span>
<Logout ImageUrl="/images/Argo.png" DisplayName="@DisplayName" UserName="@UserName"> <Logout ImageUrl="@Icon" DisplayName="@DisplayName" UserName="@UserName">
<LinkTemplate> <LinkTemplate>
<a href="/Admin/Profiles"><i class="fa fa-suitcase"></i>个人中心</a> <a href="/Admin/Profiles"><i class="fa fa-suitcase"></i>个人中心</a>
<a href="/Admin/Index"><i class="fa fa-cog"></i>设置</a> <a href="/Admin/Index"><i class="fa fa-cog"></i>设置</a>
@ -24,7 +24,7 @@
</div> </div>
</div> </div>
<div class="layout-user"> <div class="layout-user">
<img class="layout-avatar" src="/images/Argo-C.png"> <img class="layout-avatar" src="@Icon">
<div class="layout-title"> <div class="layout-title">
<span>@DisplayName</span> <span>@DisplayName</span>
</div> </div>

View File

@ -1,4 +1,5 @@
using Bootstrap.Security.Blazor; using Bootstrap.Security.Blazor;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions; using BootstrapAdmin.Web.Extensions;
using BootstrapAdmin.Web.Services; using BootstrapAdmin.Web.Services;
@ -49,6 +50,9 @@ namespace BootstrapAdmin.Web.Shared
private string? UserName { get; set; } private string? UserName { get; set; }
[NotNull]
private string? Icon { get; set; }
/// <summary> /// <summary>
/// OnInitializedAsync 方法 /// OnInitializedAsync 方法
/// </summary> /// </summary>
@ -60,15 +64,19 @@ namespace BootstrapAdmin.Web.Shared
if (!string.IsNullOrEmpty(UserName)) if (!string.IsNullOrEmpty(UserName))
{ {
DisplayName = UsersService.GetDisplayName(UserName); var user = UsersService.GetUserByUserName(UserName);
DisplayName = user?.DisplayName ?? "未注册账户";
Context.UserName = UserName; Context.UserName = UserName;
Context.DisplayName = DisplayName; Context.DisplayName = DisplayName;
Icon = string.IsNullOrEmpty(user?.Icon) ? "images/uploader/default.jpg" : GetIcon(user.Icon);
MenuItems = NavigationsService.GetAllMenus(UserName).ToAdminMenus(); MenuItems = NavigationsService.GetAllMenus(UserName).ToAdminMenus();
} }
Title = DictsService.GetWebTitle(); Title = DictsService.GetWebTitle();
Footer = DictsService.GetWebFooter(); Footer = DictsService.GetWebFooter();
string GetIcon(string icon) => icon.Contains("://", StringComparison.OrdinalIgnoreCase) ? icon : string.Format("{0}{1}", DictsService.RetrieveIconFolderPath(), icon);
} }
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url); private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);

View File

@ -23,7 +23,7 @@ public class UserNameValidator : IValidator
/// <param name="results"></param> /// <param name="results"></param>
public void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results) public void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results)
{ {
var displayName = UserService.GetDisplayName(propertyValue?.ToString()); var displayName = UserService.GetUserByUserName(propertyValue?.ToString())?.DisplayName;
if (!string.IsNullOrEmpty(displayName)) if (!string.IsNullOrEmpty(displayName))
{ {
ErrorMessage = $"{context.DisplayName}已存在"; ErrorMessage = $"{context.DisplayName}已存在";