MenuService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\Traits\ServiceAppAop;
  5. use App\Menu;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. class MenuService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass=Menu::class;
  12. public function getMenu()
  13. {
  14. if (!Cache::has("menus")){
  15. if (Cache::lock("menus",2)){
  16. $this->setMenu();
  17. Cache::lock("menus")->release();
  18. }else{
  19. sleep(1);
  20. return Cache::get("menus") ?? [];
  21. }
  22. }
  23. return Cache::get("menus");
  24. }
  25. public function setMenu()
  26. {
  27. //如果需要热更新 为所有用户个人菜单锁定缓存 标记TAGS 在此清除 Cache::tag("tags")->flush()
  28. Cache::forever("menus",Menu::query()
  29. ->select("id","name","parent_id","route","font","font_style")
  30. ->orderByRaw("level DESC,sequence")->get());
  31. }
  32. public function appendMenu(Menu $menu)
  33. {
  34. $menus = $this->getMenu();
  35. if ($menus){
  36. $menus->add($menu);
  37. Cache::forever("menus",$menus);
  38. }
  39. }
  40. //菜单与权限关联依据为 name与parent_id字段 当两个表中任意此字段发生变更 必须刷掉缓存
  41. public function getVisibleFunctionList($authorities = null,&$mapping = [])
  42. {
  43. /** @var Collection $authorities */
  44. if (!$authorities)$authorities = app("AuthorityService")->getUserAuthority();
  45. $authMap = app("AuthorityService")->format($authorities);
  46. $menus = $this->format($this->getMenu());
  47. return array_values($this->formatMenu($menus,$authMap,$mapping));
  48. }
  49. /**
  50. * 格式化为树状结构
  51. *
  52. * @param Collection $menus
  53. * @return array
  54. */
  55. public function format($menus)
  56. {
  57. $menuMap = [];
  58. foreach ($menus as $menu){
  59. $menuMap[$menu->id] = [
  60. "id" => $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 $menuMap;
  75. }
  76. /**
  77. * 递归格式化菜单组
  78. *
  79. * @param array $menus
  80. * @param array $authorities
  81. * @param array $mapping
  82. *
  83. * @return array|bool
  84. */
  85. private function formatMenu(array $menus,array $authorities,&$mapping=[])
  86. {
  87. foreach ($menus as $index=>$menu){
  88. $mark = false;
  89. foreach ($authorities as $j=>$authority){
  90. if ($menu["name"] == $authority["name"]){
  91. $mapping[$menu["id"]] = $authority["id"];
  92. $menus[$index]["authId"] = $authority["id"];
  93. $mark = true;
  94. $authorities[$j]["mark"] = true;
  95. if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"],$mapping));
  96. }
  97. }
  98. if (!$mark)unset($menus[$index]);
  99. }
  100. foreach ($authorities as $authority){
  101. if (!isset($authority["mark"])){
  102. $menus[] = [
  103. "name" => $authority["name"],
  104. "route" => "",
  105. ];
  106. }
  107. }
  108. return $menus;
  109. }
  110. /**
  111. * 获取菜单与权限映射
  112. */
  113. public function getMenuAndAuthorityMapping()
  114. {
  115. $this->getVisibleFunctionList(Authority::query()->get(),$mapping);
  116. return $mapping;
  117. }
  118. /**
  119. * 获取唯一名称
  120. */
  121. public function getUniqueName(Menu $menu)
  122. {
  123. if (!$menu->parent_id)return $menu->name;
  124. $menus = $this->getMenu();
  125. $menuMap = [];
  126. foreach ($menus as $m)$menuMap[$m->id] = $m;
  127. $names = [];
  128. while ($menu){
  129. $names[] = $menu->name;
  130. if ($menu->parent_id)$menu = $menuMap[$menu->parent_id] ?? null;
  131. else $menu = null;
  132. }
  133. $name = "";
  134. for ($i=count($names)-1;$i>=0;$i--){
  135. $name .= $names[$i];
  136. if ($i!=0)$name .= "-";
  137. }
  138. return $name;
  139. }
  140. }