MenuService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. use App\Menu;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. class MenuService
  8. {
  9. use ServiceAppAop;
  10. protected $modelClass=Menu::class;
  11. public function getMenu()
  12. {
  13. if (!Cache::has("menus")){
  14. if (Cache::lock("menus",2)){
  15. $this->setMenu();
  16. Cache::lock("menus")->release();
  17. }else{
  18. sleep(1);
  19. return Cache::get("menus") ?? [];
  20. }
  21. }
  22. return Cache::get("menus");
  23. }
  24. public function setMenu()
  25. {
  26. //如果需要热更新 为所有用户个人菜单锁定缓存 标记TAGS 在此清除 Cache::tag("tags")->flush()
  27. Cache::forever("menus",Menu::query()
  28. ->select("id","name","parent_id","route","font","font_style")
  29. ->orderByRaw("level DESC,sequence")->get());
  30. }
  31. public function appendMenu(Menu $menu)
  32. {
  33. $menus = $this->getMenu();
  34. if ($menus){
  35. $menus->add($menu);
  36. Cache::forever("menus",$menus);
  37. }
  38. }
  39. public function getVisibleFunctionList()
  40. {
  41. /** @var Collection $authorities */
  42. $authorities = app("AuthorityService")->getUserAuthority();
  43. if (!$authorities)$authorities = [];
  44. $authMap = [];
  45. foreach ($authorities as $authority){
  46. $item = $authority->toArray();
  47. $item["child"] = [];
  48. $authMap[$authority->id] = $item;
  49. }
  50. foreach ($authorities as $authority){
  51. if ($authority->id_parent){
  52. if (isset($authMap[$authority->id_parent]))$authMap[$authority->id_parent]["child"][] = $authMap[$authority->id];
  53. else $authMap = $this->formatAuthority($authMap,$authMap[$authority->id]);
  54. unset($authMap[$authority->id]);
  55. }
  56. }
  57. $menus = $this->getMenu();
  58. $menuMap = [];
  59. foreach ($menus as $menu){
  60. $menuMap[$menu->id] = [
  61. "child" => [],
  62. "name" => $menu->name,
  63. "font" => $menu->font,
  64. "fontStyle" => $menu->font_style,
  65. "route" => $menu->route,
  66. ];
  67. }
  68. foreach ($menus as $menu){
  69. if ($menu->parent_id){
  70. $menuMap[$menu->parent_id]["child"][] = $menuMap[$menu->id];
  71. unset($menuMap[$menu->id]);
  72. }
  73. }
  74. return array_values($this->formatMenu($menuMap,$authMap));
  75. }
  76. /**
  77. * 递归格式化权限组
  78. *
  79. * @param array $authMap
  80. * @param array $authorities
  81. *
  82. * @return array|bool
  83. */
  84. private function formatAuthority(array $authMap,array $authorities)
  85. {
  86. foreach ($authMap as $index=>$data){
  87. if ($data["id"]==$authorities["id_parent"]){
  88. $authMap[$index]["child"][] = $authorities;
  89. unset($authMap[$authorities["id"]]);
  90. return $authMap;
  91. }else{
  92. if ($authMap[$index]["child"]){
  93. $re = $this->formatAuthority($authMap[$index]["child"],$authorities);
  94. if ($re){
  95. $authMap[$index]["child"] = $re;
  96. return $authMap;
  97. }
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * 递归格式化菜单组
  105. *
  106. * @param array $menus
  107. * @param array $authorities
  108. *
  109. * @return array|bool
  110. */
  111. private function formatMenu(array $menus,array $authorities)
  112. {
  113. foreach ($menus as $index=>$menu){
  114. $mark = false;
  115. foreach ($authorities as $j=>$authority){
  116. if ($menu["name"] == $authority["name"]){
  117. $mark = true;
  118. $menus[$index]["authority"] = $j;
  119. $authorities[$j]["mark"] = true;
  120. if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"]));
  121. }
  122. }
  123. if (!$mark)unset($menus[$index]);
  124. }
  125. foreach ($authorities as $authority){
  126. if (!isset($authority["mark"])){
  127. $menus[] = [
  128. "name" => $authority["name"],
  129. "route" => "",
  130. ];
  131. }
  132. }
  133. return $menus;
  134. }
  135. }