RightContent.razor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using EasyTemplate.Tool;
  2. using EasyTemplate.Tool.Util;
  3. using Mapster;
  4. using Microsoft.JSInterop;
  5. namespace EasyTemplate.Blazor.Web.Components;
  6. public partial class RightContent
  7. {
  8. protected override async Task OnInitializedAsync()
  9. {
  10. NavigationManager.RedirectLogin();
  11. await base.OnInitializedAsync();
  12. SetClassMap();
  13. CurrentUser = Global.CurrentUser;
  14. // 配置 Mapster 映射规则
  15. TypeAdapterConfig<SystemNotification, NoticeIconData>.NewConfig()
  16. .Map(dest => dest.Key, src => src.Id)
  17. .Map(dest => dest.Title, src => src.Title)
  18. .Map(dest => dest.Description, src => src.Info)
  19. .Map(dest => dest.Read, src => src.Seen)
  20. .Map(dest => dest.Datetime, src => src.CreateTime);
  21. await Notifications();
  22. }
  23. private async Task Notifications()
  24. {
  25. var notices = await Notices.AsQueryable().ToArrayAsync();
  26. IconNotifications = notices.Where(x => x.NotifyType == PublicEnum.NotifyType.Notify).Adapt<NoticeIconData[]>();
  27. Messages = notices.Where(x => x.NotifyType == PublicEnum.NotifyType.Info).Adapt<NoticeIconData[]>();
  28. Count = notices.Count(x => x.Seen == false);
  29. }
  30. protected void SetClassMap()
  31. {
  32. ClassMapper
  33. .Clear()
  34. .Add("right");
  35. }
  36. public async Task HandleSelectUser(MenuItem item)
  37. {
  38. switch (item.Key)
  39. {
  40. case "center":
  41. NavigationManager.NavigateTo("/account/center");
  42. break;
  43. case "setting":
  44. NavigationManager.NavigateTo("/account/setting");
  45. break;
  46. case "logout":
  47. var localStorageHelper = new LocalStorage(JSRuntime);
  48. await localStorageHelper.RemoveLocalStorage(LocalStorage.AutoLogin);
  49. await localStorageHelper.RemoveLocalStorage(LocalStorage.UserInfo);
  50. var host = _accessor.GetHost();
  51. LogLogin.Insert(new SystemLogLogin() { Info = $"用户【{Global.CurrentUser.Account}】登出账号", UserId = Global.CurrentUser.Id, CreateTime = DateTime.Now });
  52. NavigationManager.NavigateTo("/account/login");
  53. Global.CurrentUser = null;
  54. break;
  55. }
  56. }
  57. public async Task Seen(string key)
  58. {
  59. Notices.AsUpdateable()
  60. .SetColumns(x => x.Seen == true)
  61. .Where(x => x.Id == Convert.ToInt32(key))
  62. .ExecuteCommand();
  63. await Notifications();
  64. }
  65. public void HandleSelectLang(MenuItem item)
  66. {
  67. LocalizationService.SetLanguage(CultureInfo.GetCultureInfo(item.Key));
  68. }
  69. public async Task HandleClear(string key)
  70. {
  71. switch (key)
  72. {
  73. case "notification":
  74. Notices.AsDeleteable().Where(x => x.NotifyType == PublicEnum.NotifyType.Notify).ExecuteCommand();
  75. IconNotifications = [];
  76. break;
  77. case "message":
  78. Notices.AsDeleteable().Where(x => x.NotifyType == PublicEnum.NotifyType.Info).ExecuteCommand();
  79. Messages = [];
  80. break;
  81. }
  82. MessageService.Success($"清空了{key}");
  83. await Notifications();
  84. }
  85. public async Task HandleViewMore(string key)
  86. {
  87. MessageService.Info("点击查看更多");
  88. }
  89. private NoticeIconData[] IconNotifications = { };
  90. private NoticeIconData[] Messages = { };
  91. private int Count = 0;
  92. private AvatarMenuItem[] AvatarMenuItems =>
  93. [
  94. //new() { Key = "center", IconType = "user", Option = L["menu.account.center"]},
  95. new() { Key = "setting", IconType = "setting", Option = L["menu.account.settings"] },
  96. new() { IsDivider = true },
  97. new() { Key = "logout", IconType = "logout", Option = L["menu.account.logout"]}
  98. ];
  99. private SystemUser CurrentUser = new SystemUser();
  100. /// <summary>
  101. ///
  102. /// </summary>
  103. [Inject] protected NavigationManager NavigationManager { get; set; }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. [Inject] protected MessageService MessageService { get; set; }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. [Inject] private IStringLocalizer<I18n> L { get; set; }
  112. /// <summary>
  113. ///
  114. /// </summary>
  115. [Inject] private ILocalizationService LocalizationService { get; set; }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. [Inject] private IWebHostEnvironment WebHostEnvironment { get; set; }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. [Inject] private IHttpContextAccessor _accessor { get; set; }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. [Inject] private IJSRuntime JSRuntime { get; set; }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. [Inject] private SqlSugarRepository<SystemLogLogin> LogLogin { get; set; }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. [Inject] private SqlSugarRepository<SystemNotification> Notices { get; set; }
  136. }