diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs index 00397f82..6fafc5f1 100644 --- a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs @@ -182,4 +182,14 @@ class DictService : IDict { throw new NotImplementedException(); } + + public string GetIconFolderPath() + { + throw new NotImplementedException(); + } + + public string GetDefaultIcon() + { + throw new NotImplementedException(); + } } diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/UserService.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/UserService.cs index 3aaa69f4..dc856b6c 100644 --- a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/UserService.cs +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/UserService.cs @@ -218,4 +218,9 @@ class UserService : IUser { throw new NotImplementedException(); } + + public bool SaveLogo(string userName, string? logo) + { + throw new NotImplementedException(); + } } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictService.cs index 41922bfd..71b7affa 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/DictService.cs @@ -221,10 +221,20 @@ class DictService : IDict /// 获取头像路径 /// /// - public string RetrieveIconFolderPath() + public string GetIconFolderPath() { var dicts = GetAll(); - return dicts.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "images/uploder/"; + return dicts.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "/images/uploder/"; + } + + /// + /// 获取头像路径 + /// + /// + public string GetDefaultIcon() + { + var dicts = GetAll(); + return dicts.FirstOrDefault(d => d.Name == "头像文件" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "default.jpg"; } /// diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs index df9a4cf5..73478ec1 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs @@ -165,6 +165,11 @@ class UserService : IUser /// public bool SaveTheme(string userName, string theme) => Database.Update("set Css = @1 where UserName = @0", userName, theme) == 1; + /// + /// + /// + public bool SaveLogo(string userName, string? logo) => Database.Update("set Icon = @1 where UserName = @0", userName, logo) == 1; + /// /// 创建手机用户 /// diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IDict.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IDict.cs index 3831593d..176eaa7e 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IDict.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IDict.cs @@ -148,7 +148,13 @@ public interface IDict /// /// /// - string RetrieveIconFolderPath(); + string GetIconFolderPath(); + + /// + /// + /// + /// + string GetDefaultIcon(); /// /// 通过指定 appId 获得配置首页地址 diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs index 3ef7d126..56dcd682 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs @@ -89,6 +89,14 @@ public interface IUser /// bool SaveTheme(string userName, string theme); + /// + /// 保存用户头像方法 + /// + /// + /// + /// + bool SaveLogo(string userName, string? logo); + /// /// 获得所有用户 /// diff --git a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.db b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.db index bc2f736f..07538134 100644 Binary files a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.db and b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.db differ diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor index 16c0ff69..f30a896e 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor @@ -78,7 +78,7 @@
- +
diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor.cs index ac8fb3ef..b425e73f 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Profiles.razor.cs @@ -41,7 +41,16 @@ public partial class Profiles [NotNull] private ToastService? ToastService { get; set; } - private List PreviewFileList { get; } = new(new[] { new UploadFile { PrevUrl = "/images/Argo.png" } }); + [Inject] + [NotNull] + private IWebHostEnvironment? WebHost { get; set; } + + private List PreviewFileList { get; } = new(); + + private string? DefaultLogo { get; set; } + + [NotNull] + private string? DefaultLogoFolder { get; set; } /// /// @@ -55,11 +64,31 @@ public partial class Profiles { App = user?.App ?? AppContext.AppId, UserName = AppContext.UserName, - DisplayName = AppContext.DisplayName + DisplayName = AppContext.DisplayName, + Css = user?.Css }; IsDemo = DictService.IsDemo(); Apps = DictService.GetApps().ToSelectedItemList(); Themes = DictService.GetThemes().ToSelectedItemList(); + + DefaultLogo = DictService.GetDefaultIcon(); + DefaultLogoFolder = DictService.GetIconFolderPath(); + + var logoFile = user?.Icon ?? DefaultLogo; + var logoFolder = DefaultLogoFolder; + CurrentUser.Icon = Path.Combine(logoFolder, logoFile); + var fileName = Path.Combine(WebHost.WebRootPath, logoFolder.Replace("/", "\\").TrimStart('\\'), logoFile); + if (File.Exists(fileName)) + { + var uploadFile = new UploadFile() + { + FileName = logoFile, + PrevUrl = CurrentUser.Icon + }; + var fi = new FileInfo(fileName); + uploadFile.Size = fi.Length; + PreviewFileList.Add(uploadFile); + } } private async Task ShowToast(bool result, string title) @@ -98,8 +127,50 @@ public partial class Profiles await ShowToast(ret, "网站样式"); } - private Task OnSaveIcon() + private async Task OnSaveIcon(UploadFile file) { - return Task.CompletedTask; + // 保存到物理文件 + var logoFile = $"{CurrentUser.UserName}{Path.GetExtension(file.OriginFileName)}"; + var fileName = Path.Combine(WebHost.WebRootPath, DefaultLogoFolder.Replace("/", "\\").TrimStart('\\'), logoFile); + if (File.Exists(fileName)) + { + File.Delete(fileName); + } + + // 文件大小 10 M + var ret = await file.SaveToFile(fileName, 10 * 1024 * 1000); + + // 更新用户信息 + if (ret) + { + ret = UserService.SaveLogo(CurrentUser.UserName, logoFile); + + CurrentUser.Icon = Path.Combine(DefaultLogoFolder, logoFile); + PreviewFileList.Clear(); + PreviewFileList.Add(new UploadFile() + { + PrevUrl = $"{CurrentUser.Icon}?v={DateTime.Now.Ticks}", + Size = file.Size, + FileName = logoFile + }); + } + await ShowToast(ret, "用户头像"); + } + + private async Task OnDeleteIcon(UploadFile file) + { + var ret = false; + var logoFile = file.FileName; + if (!string.IsNullOrEmpty(logoFile)) + { + var fileName = Path.Combine(WebHost.WebRootPath, DefaultLogoFolder.Replace("/", "\\").TrimStart('\\'), logoFile); + if (!logoFile.Equals(DefaultLogo, StringComparison.OrdinalIgnoreCase) && File.Exists(fileName)) + { + File.Delete(fileName); + } + ret = UserService.SaveLogo(CurrentUser.UserName, null); + } + await ShowToast(ret, "用户头像"); + return ret; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index 861b35c7..87ad38a9 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -68,7 +68,7 @@ namespace BootstrapAdmin.Web.Shared DisplayName = user?.DisplayName ?? "未注册账户"; Context.UserName = UserName; Context.DisplayName = DisplayName; - Icon = string.IsNullOrEmpty(user?.Icon) ? "images/uploader/default.jpg" : GetIcon(user.Icon); + Icon = string.IsNullOrEmpty(user?.Icon) ? "/images/uploader/default.jpg" : GetIcon(user.Icon); MenuItems = NavigationsService.GetAllMenus(UserName).ToAdminMenus(); } @@ -76,7 +76,7 @@ namespace BootstrapAdmin.Web.Shared Title = DictsService.GetWebTitle(); Footer = DictsService.GetWebFooter(); - string GetIcon(string icon) => icon.Contains("://", StringComparison.OrdinalIgnoreCase) ? icon : string.Format("{0}{1}", DictsService.RetrieveIconFolderPath(), icon); + string GetIcon(string icon) => icon.Contains("://", StringComparison.OrdinalIgnoreCase) ? icon : string.Format("{0}{1}", DictsService.GetIconFolderPath(), icon); } private Task OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);