| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Services;
- use App\Traits\ServiceAppAop;
- use App\Menu;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\Cache;
- class MenuService
- {
- use ServiceAppAop;
- protected $modelClass=Menu::class;
- public function getMenu()
- {
- if (!Cache::has("menus")){
- if (Cache::lock("menus",2)){
- $this->setMenu();
- Cache::lock("menus")->release();
- }else{
- sleep(1);
- return Cache::get("menus") ?? [];
- }
- }
- return Cache::get("menus");
- }
- public function setMenu()
- {
- //如果需要热更新 为所有用户个人菜单锁定缓存 标记TAGS 在此清除 Cache::tag("tags")->flush()
- Cache::forever("menus",Menu::query()
- ->select("id","name","parent_id","route","font","font_style")
- ->orderByRaw("level DESC,sequence")->get());
- }
- public function appendMenu(Menu $menu)
- {
- $menus = $this->getMenu();
- if ($menus){
- $menus->add($menu);
- Cache::forever("menus",$menus);
- }
- }
- public function getVisibleFunctionList()
- {
- Cache::flush();
- /** @var Collection $authorities */
- $authorities = app("AuthorityService")->getUserAuthority();
- if (!$authorities)$authorities = [];
- $authMap = [];
- foreach ($authorities as $authority){
- $item = $authority->toArray();
- $item["child"] = [];
- $authMap[$authority->id] = $item;
- }
- foreach ($authorities as $authority){
- if ($authority->id_parent){
- if (isset($authMap[$authority->id_parent]))$authMap[$authority->id_parent]["child"][] = $authMap[$authority->id];
- else $authMap = $this->formatAuthority($authMap,$authMap[$authority->id]);
- unset($authMap[$authority->id]);
- }
- }
- $menus = $this->getMenu();
- $menuMap = [];
- foreach ($menus as $menu){
- $menuMap[$menu->id] = [
- "child" => [],
- "name" => $menu->name,
- "font" => $menu->font,
- "fontStyle" => $menu->font_style,
- "route" => $menu->route,
- ];
- }
- foreach ($menus as $menu){
- if ($menu->parent_id){
- $menuMap[$menu->parent_id]["child"][] = $menuMap[$menu->id];
- unset($menuMap[$menu->id]);
- }
- }
- return array_values($this->formatMenu($menuMap,$authMap));
- }
- /**
- * 递归格式化权限组
- *
- * @param array $authMap
- * @param array $authorities
- *
- * @return array|bool
- */
- private function formatAuthority(array $authMap,array $authorities)
- {
- foreach ($authMap as $index=>$data){
- if ($data["id"]==$authorities["id_parent"]){
- $authMap[$index]["child"][] = $authorities;
- unset($authMap[$authorities["id"]]);
- return $authMap;
- }else{
- if ($authMap[$index]["child"]){
- $re = $this->formatAuthority($authMap[$index]["child"],$authorities);
- if ($re){
- $authMap[$index]["child"] = $re;
- return $authMap;
- }
- }
- }
- }
- return false;
- }
- /**
- * 递归格式化菜单组
- *
- * @param array $menus
- * @param array $authorities
- *
- * @return array|bool
- */
- private function formatMenu(array $menus,array $authorities)
- {
- foreach ($menus as $index=>$menu){
- $mark = false;
- foreach ($authorities as $j=>$authority){
- if ($menu["name"] == $authority["name"]){
- $mark = true;
- $menus[$index]["authority"] = $j;
- $authorities[$j]["mark"] = true;
- if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"]));
- }
- }
- if (!$mark)unset($menus[$index]);
- }
- foreach ($authorities as $authority){
- if (!isset($authority["mark"])){
- $menus[] = [
- "name" => $authority["name"],
- "route" => "",
- ];
- }
- }
- return $menus;
- }
- }
|