BasicLayout.razor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using AI.Platform.Core;
  2. using AI.Platform.Core.Util;
  3. using Microsoft.JSInterop;
  4. namespace AI.Platform.Web.Components.Layout;
  5. public partial class BasicLayout : LayoutComponentBase, IDisposable
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. /// <returns></returns>
  11. protected override async Task OnInitializedAsync()
  12. {
  13. try
  14. {
  15. LocalizationChanged = (sender, args) => InvokeAsync(StateHasChanged);
  16. LocalizationService.LanguageChanged += LocalizationChanged;
  17. var menuList = await Menu.AsQueryable().Where(x => x.Enabled).OrderBy(x => x.Sort).ToListAsync();
  18. Global.Menus = BuildMenuTree(menuList, 0);
  19. if (Global.CurrentUser != null)
  20. {
  21. var key = $"menu_{Global.CurrentUser.RoleId}";
  22. if (!await Cache.Exist(key))
  23. {
  24. var menus = RoleMenu.AsQueryable()
  25. .LeftJoin<SystemMenu>((rm, m) => rm.MenuId == m.Id)
  26. .Where((rm, m) => rm.RoleId == Global.CurrentUser.RoleId && m.Id > 0 && m.Enabled)
  27. .OrderBy((rm, m) => m.Sort)
  28. .Select((rm, m) => new SystemMenu()
  29. {
  30. Id = m.Id,
  31. Name = m.Name,
  32. Path = m.Path,
  33. Icon = m.Icon,
  34. ParentId = m.ParentId,
  35. Sort = m.Sort
  36. })
  37. .ToList();
  38. var menuTree = BuildMenuTree(menus, 0);
  39. MenuData = menuTree.Select(ConvertToMenuDataItem).ToArray();
  40. Cache.Set(key, MenuData);
  41. }
  42. else
  43. {
  44. MenuData = await Cache.Get<MenuDataItem[]>(key);
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. Log.Error(ex);
  51. }
  52. }
  53. protected override Task OnAfterRenderAsync(bool firstRender)
  54. {
  55. if (firstRender && Global.CurrentUser == null)
  56. {
  57. NavigationManager.NavigateTo("/account/login");
  58. }
  59. return Task.CompletedTask;
  60. }
  61. /// <summary>
  62. /// 创建菜单树
  63. /// </summary>
  64. /// <param name="menus"></param>
  65. /// <param name="parentId"></param>
  66. /// <returns></returns>
  67. private List<SystemMenu> BuildMenuTree(List<SystemMenu> menus, long parentId)
  68. {
  69. var children = menus
  70. .Where(m => m.ParentId == parentId)
  71. .Select(m => {
  72. m.Children = BuildMenuTree(menus, m.Id);
  73. return m;
  74. })
  75. .ToList();
  76. return children.Count == 0 ? null : children;
  77. }
  78. /// <summary>
  79. /// 将菜单树转换为AntDesign的菜单结构
  80. /// </summary>
  81. /// <param name="menu"></param>
  82. /// <returns></returns>
  83. private MenuDataItem ConvertToMenuDataItem(SystemMenu menu)
  84. {
  85. return new MenuDataItem
  86. {
  87. Path = menu.Path,
  88. Name = menu.Name,
  89. Key = menu.Key,
  90. Icon = menu.Icon,
  91. Children = menu.Children?.Select(ConvertToMenuDataItem).ToArray()
  92. };
  93. }
  94. public void Dispose()
  95. {
  96. LocalizationService.LanguageChanged -= LocalizationChanged;
  97. }
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. private MenuDataItem[] MenuData;
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. [Inject] public NavigationManager NavigationManager { get; set; }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. [Inject] private ILocalizationService LocalizationService { get; set; }
  110. /// <summary>
  111. ///
  112. /// </summary>
  113. [Inject] private SqlSugarRepository<SystemMenu> Menu { get; set; }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. [Inject] private SqlSugarRepository<SystemRoleMenu> RoleMenu { get; set; }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. private EventHandler<CultureInfo> LocalizationChanged;
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. private string CopyRight;
  126. }