feat: 个人中心头像上传功能

This commit is contained in:
Argo-Tianyi 2022-01-13 15:32:48 +08:00
parent 926cd75cf6
commit 16ed058850
10 changed files with 125 additions and 10 deletions

View File

@ -182,4 +182,14 @@ class DictService : IDict
{
throw new NotImplementedException();
}
public string GetIconFolderPath()
{
throw new NotImplementedException();
}
public string GetDefaultIcon()
{
throw new NotImplementedException();
}
}

View File

@ -218,4 +218,9 @@ class UserService : IUser
{
throw new NotImplementedException();
}
public bool SaveLogo(string userName, string? logo)
{
throw new NotImplementedException();
}
}

View File

@ -221,10 +221,20 @@ class DictService : IDict
/// 获取头像路径
/// </summary>
/// <returns></returns>
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/";
}
/// <summary>
/// 获取头像路径
/// </summary>
/// <returns></returns>
public string GetDefaultIcon()
{
var dicts = GetAll();
return dicts.FirstOrDefault(d => d.Name == "头像文件" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "default.jpg";
}
/// <summary>

View File

@ -165,6 +165,11 @@ class UserService : IUser
/// </summary>
public bool SaveTheme(string userName, string theme) => Database.Update<User>("set Css = @1 where UserName = @0", userName, theme) == 1;
/// <summary>
///
/// </summary>
public bool SaveLogo(string userName, string? logo) => Database.Update<User>("set Icon = @1 where UserName = @0", userName, logo) == 1;
/// <summary>
/// 创建手机用户
/// </summary>

View File

@ -148,7 +148,13 @@ public interface IDict
///
/// </summary>
/// <returns></returns>
string RetrieveIconFolderPath();
string GetIconFolderPath();
/// <summary>
///
/// </summary>
/// <returns></returns>
string GetDefaultIcon();
/// <summary>
/// 通过指定 appId 获得配置首页地址

View File

@ -89,6 +89,14 @@ public interface IUser
/// <returns></returns>
bool SaveTheme(string userName, string theme);
/// <summary>
/// 保存用户头像方法
/// </summary>
/// <param name="userName"></param>
/// <param name="logo"></param>
/// <returns></returns>
bool SaveLogo(string userName, string? logo);
/// <summary>
/// 获得所有用户
/// </summary>

View File

@ -78,7 +78,7 @@
<AdminCard HeaderText="个人头像" AuthorizeKey="SaveIcon">
<div class="row g-3 form-inline">
<div class="col-12 col-sm-6">
<CardUpload TValue="string" IsSingle DefaultFileList="PreviewFileList" />
<CardUpload TValue="string" IsSingle="true" DefaultFileList="PreviewFileList" OnChange="OnSaveIcon" OnDelete="OnDeleteIcon" />
</div>
</div>
</AdminCard>

View File

@ -41,7 +41,16 @@ public partial class Profiles
[NotNull]
private ToastService? ToastService { get; set; }
private List<UploadFile> PreviewFileList { get; } = new(new[] { new UploadFile { PrevUrl = "/images/Argo.png" } });
[Inject]
[NotNull]
private IWebHostEnvironment? WebHost { get; set; }
private List<UploadFile> PreviewFileList { get; } = new();
private string? DefaultLogo { get; set; }
[NotNull]
private string? DefaultLogoFolder { get; set; }
/// <summary>
///
@ -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<bool> 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;
}
}

View File

@ -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<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);