MenuService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Cache::flush();
  42. /** @var Collection $authorities */
  43. $authorities = app("AuthorityService")->getUserAuthority();
  44. if (!$authorities)$authorities = [];
  45. $authMap = [];
  46. foreach ($authorities as $authority){
  47. $item = $authority->toArray();
  48. $item["child"] = [];
  49. $authMap[$authority->id] = $item;
  50. }
  51. foreach ($authorities as $authority){
  52. if ($authority->id_parent){
  53. if (isset($authMap[$authority->id_parent]))$authMap[$authority->id_parent]["child"][] = $authMap[$authority->id];
  54. else $authMap = $this->formatAuthority($authMap,$authMap[$authority->id]);
  55. unset($authMap[$authority->id]);
  56. }
  57. }
  58. $menus = $this->getMenu();
  59. $menuMap = [];
  60. foreach ($menus as $menu){
  61. $menuMap[$menu->id] = [
  62. "child" => [],
  63. "name" => $menu->name,
  64. "font" => $menu->font,
  65. "fontStyle" => $menu->font_style,
  66. "route" => $menu->route,
  67. ];
  68. }
  69. foreach ($menus as $menu){
  70. if ($menu->parent_id){
  71. $menuMap[$menu->parent_id]["child"][] = $menuMap[$menu->id];
  72. unset($menuMap[$menu->id]);
  73. }
  74. }
  75. return array_values($this->formatMenu($menuMap,$authMap));
  76. }
  77. /**
  78. * 递归格式化权限组
  79. *
  80. * @param array $authMap
  81. * @param array $authorities
  82. *
  83. * @return array|bool
  84. */
  85. private function formatAuthority(array $authMap,array $authorities)
  86. {
  87. foreach ($authMap as $index=>$data){
  88. if ($data["id"]==$authorities["id_parent"]){
  89. $authMap[$index]["child"][] = $authorities;
  90. unset($authMap[$authorities["id"]]);
  91. return $authMap;
  92. }else{
  93. if ($authMap[$index]["child"]){
  94. $re = $this->formatAuthority($authMap[$index]["child"],$authorities);
  95. if ($re){
  96. $authMap[$index]["child"] = $re;
  97. return $authMap;
  98. }
  99. }
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * 递归格式化菜单组
  106. *
  107. * @param array $menus
  108. * @param array $authorities
  109. *
  110. * @return array|bool
  111. */
  112. private function formatMenu(array $menus,array $authorities)
  113. {
  114. foreach ($menus as $index=>$menu){
  115. $mark = false;
  116. foreach ($authorities as $j=>$authority){
  117. if ($menu["name"] == $authority["name"]){
  118. $mark = true;
  119. $menus[$index]["authority"] = $j;
  120. $authorities[$j]["mark"] = true;
  121. if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"]));
  122. }
  123. }
  124. if (!$mark)unset($menus[$index]);
  125. }
  126. foreach ($authorities as $authority){
  127. if (!isset($authority["mark"])){
  128. $menus[] = [
  129. "name" => $authority["name"],
  130. "route" => "",
  131. ];
  132. }
  133. }
  134. return $menus;
  135. }
  136. }