SysRoleMenuSeedData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. /// <summary>
  8. /// 系统角色菜单表种子数据
  9. /// </summary>
  10. public class SysRoleMenuSeedData : ISqlSugarEntitySeedData<SysRoleMenu>
  11. {
  12. private const long CollisionFallbackBaseId = 9_000_000_000_000_000L;
  13. private const long CollisionFallbackRoleStride = 1_000_000_000L;
  14. /// <summary>
  15. /// 种子数据
  16. /// </summary>
  17. /// <returns></returns>
  18. public IEnumerable<SysRoleMenu> HasData()
  19. {
  20. var roleMenuList = new List<SysRoleMenu>();
  21. var roleMenuPairs = new HashSet<(long RoleId, long MenuId)>();
  22. var roleList = new SysRoleSeedData().HasData().ToList();
  23. var menuList = new SysMenuSeedData().HasData().ToList();
  24. var defaultMenuList = new SysTenantMenuSeedData().HasData().ToList();
  25. // 第一个角色拥有全部默认租户菜单
  26. AddRoleMenus(roleMenuList, roleMenuPairs, defaultMenuList.Select(u => new SysRoleMenu
  27. {
  28. Id = u.MenuId + (roleList[0].Id % 1300000000000),
  29. RoleId = roleList[0].Id,
  30. MenuId = u.MenuId
  31. }));
  32. // 其他角色权限:工作台、帮助文档、关于项目、个人中心(整棵子树)
  33. // 系统管理:仅挂「机构管理」子树(与租户基线一致),避免仅有空目录导致侧栏有「系统管理」却无子路由、访问未授权路径时应用内 404
  34. var otherRoleMenuList = menuList.ToChildList(u => u.Id, u => u.Pid, u => new[] { "工作台", "帮助文档", "关于项目", "个人中心" }.Contains(u.Title)).ToList();
  35. var systemDir = menuList.FirstOrDefault(u => u.Type == MenuTypeEnum.Dir && u.Title == "系统管理");
  36. if (systemDir != null)
  37. {
  38. if (!otherRoleMenuList.Any(u => u.Id == systemDir.Id))
  39. otherRoleMenuList.Add(systemDir);
  40. otherRoleMenuList.AddRange(menuList.ToChildList(u => u.Id, u => u.Pid, u => u.Pid == systemDir.Id && u.Title == "机构管理"));
  41. }
  42. foreach (var role in roleList.Skip(1))
  43. {
  44. AddRoleMenus(roleMenuList, roleMenuPairs, otherRoleMenuList.Select(u => new SysRoleMenu
  45. {
  46. Id = u.Id + (role.Id % 1300000000000),
  47. RoleId = role.Id,
  48. MenuId = u.Id
  49. }));
  50. }
  51. // 演示一般账户:在「其他角色」基线之上追加 Ai-DOP 全子树(智慧运营首页等)
  52. var demoGeneralRole = roleList.FirstOrDefault(r => r.Code == "demo_general");
  53. if (demoGeneralRole != null)
  54. {
  55. var allFlat = App.GetService<SysTenantService>().GetAllSeedMenusFlat();
  56. var aidopRoot = allFlat.FirstOrDefault(u =>
  57. u.Name == "aidopRoot" || string.Equals(u.Path?.Trim(), "/aidop", StringComparison.Ordinal));
  58. if (aidopRoot != null)
  59. {
  60. // ToChildList(..., topParentIdValue) 默认包含根节点,勿再手动 Add(aidopRoot),否则 SysRoleMenu 主键重复导致启动失败
  61. var subtree = allFlat.ToChildList(u => u.Id, u => u.Pid, aidopRoot.Id).ToList();
  62. AddRoleMenus(roleMenuList, roleMenuPairs, subtree.Select(m => new SysRoleMenu
  63. {
  64. Id = m.Id + (demoGeneralRole.Id % 1300000000000),
  65. RoleId = demoGeneralRole.Id,
  66. MenuId = m.Id
  67. }));
  68. }
  69. }
  70. EnsureUniqueIds(roleMenuList);
  71. return roleMenuList;
  72. }
  73. private static void AddRoleMenus(List<SysRoleMenu> roleMenuList, HashSet<(long RoleId, long MenuId)> roleMenuPairs, IEnumerable<SysRoleMenu> menus)
  74. {
  75. foreach (var menu in menus)
  76. {
  77. if (!roleMenuPairs.Add((menu.RoleId, menu.MenuId))) continue;
  78. roleMenuList.Add(menu);
  79. }
  80. }
  81. private static void EnsureUniqueIds(List<SysRoleMenu> roleMenuList)
  82. {
  83. var usedIds = new HashSet<long>();
  84. foreach (var roleMenu in roleMenuList.OrderBy(u => u.RoleId).ThenBy(u => u.MenuId))
  85. {
  86. if (usedIds.Add(roleMenu.Id)) continue;
  87. roleMenu.Id = BuildCollisionFallbackId(roleMenu.RoleId, roleMenu.MenuId, usedIds);
  88. }
  89. }
  90. private static long BuildCollisionFallbackId(long roleId, long menuId, HashSet<long> usedIds)
  91. {
  92. var candidate = CollisionFallbackBaseId
  93. + (roleId % 1_000_000L) * CollisionFallbackRoleStride
  94. + (menuId % CollisionFallbackRoleStride);
  95. while (!usedIds.Add(candidate)) candidate++;
  96. return candidate;
  97. }
  98. }