MenuService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $mark = true;
  93. $authorities[$j]["mark"] = true;
  94. if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"],$mapping));
  95. }
  96. }
  97. if (!$mark)unset($menus[$index]);
  98. }
  99. foreach ($authorities as $authority){
  100. if (!isset($authority["mark"])){
  101. $menus[] = [
  102. "name" => $authority["name"],
  103. "route" => "",
  104. ];
  105. }
  106. }
  107. return $menus;
  108. }
  109. /**
  110. * 获取菜单与权限映射
  111. */
  112. public function getMenuAndAuthorityMapping()
  113. {
  114. $this->getVisibleFunctionList(Authority::query()->get(),$mapping);
  115. return $mapping;
  116. }
  117. /**
  118. * 获取唯一名称
  119. */
  120. public function getUniqueName(Menu $menu)
  121. {
  122. if (!$menu->parent_id)return $menu->name;
  123. $menus = $this->getMenu();
  124. $menuMap = [];
  125. foreach ($menus as $m)$menuMap[$m->id] = $m;
  126. $names = [];
  127. while ($menu){
  128. $names[] = $menu->name;
  129. if ($menu->parent_id)$menu = $menuMap[$menu->parent_id] ?? null;
  130. else $menu = null;
  131. }
  132. $name = "";
  133. for ($i=count($names)-1;$i>=0;$i--){
  134. $name .= $names[$i];
  135. if ($i!=0)$name .= "-";
  136. }
  137. return $name;
  138. }
  139. }