Zhouzhendong 5 лет назад
Родитель
Сommit
6b6599fcae

+ 58 - 17
app/Http/Controllers/TestController.php

@@ -188,6 +188,21 @@ class TestController extends Controller
 
     public function zzd()
     {
+        $aa = [];
+        $authorities = Authority::query()->where("name","like","订单管理%")->orWhere("name","like","运输管理%")->get();
+        //Auth::user()->authorities();
+        foreach ($authorities as $authority){
+            $item = $authority->toArray();
+            $item["child"] = [];
+            $aa[$authority->id] = $item;
+        }
+        foreach ($authorities as $authority){
+            if ($authority->id_parent){
+                if (isset($aa[$authority->id_parent]))$aa[$authority->id_parent]["child"][] = $aa[$authority->id];
+                else $aa = $this->tttt($aa,$aa[$authority->id]);
+                unset($aa[$authority->id]);
+            }
+        }
         $ser = new MenuService();
         Cache::flush();
         $menus = $ser->getMenu();
@@ -203,24 +218,50 @@ class TestController extends Controller
                 unset($result[$menu->id]);
             }
         }
+        $result = $this->ttt1(array_values($result),$aa);
         dd($result);
-        $menus = [
-            "A管理" => [
-                "A二级" => [
-                    "A三级"
-                ]
-            ],
-            "B管理" => []
-        ];
-        /*$authorities = [
-            "A管理"=>[
-                "A二级"=>[9
-                    "A三级"=>[
-                        "A四级"
-                    ]
-                ]
-            ]
-        ];*/
+    }
+    function ttt1($result,$authority){
+        foreach ($result as $index=>$item){
+            $mark = false;
+            foreach ($authority as $j=>$a){
+                if ($item["name"] == $a["name"]){
+                    $mark = true;
+                    $result[$index]["authority"] = $j;
+                    $authority[$j]["mark"] = true;
+                    if ($a["child"])$result[$index]["child"] = $this->ttt1($item["child"],$a["child"]);
+                }
+            }
+            if (!$mark)unset($result[$index]);
+        }
+        foreach ($authority as $a){
+            if (!isset($a["mark"])){
+                $result[] = [
+                    "name" => $a["name"],
+                    "route" => "",
+                ];
+            }
+        }
+        return $result;
+    }
+    function tttt(array $aa,$item)
+    {
+        foreach ($aa as $index=>$data){
+            if ($data["id"]==$item["id_parent"]){
+                $aa[$index]["child"][] = $item;
+                unset($aa[$item["id"]]);
+                return $aa;
+            }else{
+                if ($aa[$index]["child"]){
+                    $re = $this->tttt($aa[$index]["child"],$item);
+                    if ($re){
+                        $aa[$index]["child"] = $re;
+                        return $aa;
+                    }
+                }
+            }
+        }
+        return false;
     }
 
     public function updateLaborRemark()

+ 28 - 0
app/Services/AuthorityService.php

@@ -7,6 +7,8 @@ namespace App\Services;
 use App\Authority;
 use App\Role;
 use Exception;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Cache;
 use App\Traits\ServiceAppAop;
 
@@ -41,4 +43,30 @@ class AuthorityService
 //        }
 //        return $roles;
 //    }
+
+    public function getUserAuthority()
+    {
+        $key = "authorities:user_".Auth::id();
+        if (!Cache::has($key)){
+            if (array_search(Auth::user()["name"],config("users.superAdmin"))===false){
+                Cache::tags("authorities:user")->forever($key,Authority::query()->whereHas("roles",function (Builder $query){
+                    $query->whereHas("users",function (Builder $query){
+                        $query->where("id",Auth::id());
+                    });
+                })->get());
+            }else Cache::tags("authorities:admin")->forever($key,$authorities = Authority::query()->get());
+        }
+        return Cache::get($key);
+    }
+
+    public function removeAdminAuth()
+    {
+        Cache::tags("authorities:admin")->flush();
+    }
+
+    public function removeAllAuth()
+    {
+        Cache::tags("authorities:admin")->flush();
+        Cache::tags("authorities:user")->flush();
+    }
 }

+ 3 - 2
app/Services/MenuService.php

@@ -27,7 +27,7 @@ class MenuService
 
     public function setMenu()
     {
-        Cache::forever("menus",Menu::query()->select("id","name","parent_id","route","sequence")->orderByRaw("level DESC,sequence")->get());
+        Cache::forever("menus",Menu::query()->select("id","name","parent_id","route")->orderByRaw("level DESC,sequence")->get());
         Cache::tags("menu")->flush();
     }
 
@@ -40,8 +40,9 @@ class MenuService
         }
     }
 
-    public function getMenuList()
+    public function getVisibleFunctionList()
     {
+        $authorities = app("AuthorityService")->getUserAuthority();
 
     }
 }