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

+ 3 - 1
app/Http/Controllers/AuthorityController.php

@@ -21,8 +21,10 @@ class AuthorityController extends Controller
     public function index()
     {
 
-        if(!Gate::allows('权限-查询')){ return redirect(url('/'));  }
+        /*if(!Gate::allows('权限-查询')){ return redirect(url('/'));  }
         $authorities=Authority::orderBy('id','asc')->paginate(100);
+        return view('maintenance.authority.index',['authorities'=>$authorities]);*/
+        $authorities = app("AuthorityService")->format(Authority::query()->get());
         return view('maintenance.authority.index',['authorities'=>$authorities]);
     }
 

+ 53 - 0
app/Services/AuthorityService.php

@@ -72,4 +72,57 @@ class AuthorityService
         Cache::tags("authorities:admin")->flush();
         Cache::tags("authorities:user")->flush();
     }
+
+    /**
+     * 格式化为树状结构
+     *
+     * @param Collection $authorities
+     * @return array|bool
+     */
+    public function format($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]);
+            }
+        }
+        return $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;
+    }
 }

+ 15 - 44
app/Services/MenuService.php

@@ -49,20 +49,20 @@ class MenuService
     {
         /** @var Collection $authorities */
         if (!$authorities)$authorities = app("AuthorityService")->getUserAuthority();
-        $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();
+        $authMap = app("AuthorityService")->format($authorities);
+        $menus = $this->format($this->getMenu());
+        return array_values($this->formatMenu($menus,$authMap,$mapping));
+    }
+
+
+    /**
+     * 格式化为树状结构
+     *
+     * @param Collection $menus
+     * @return array
+     */
+    public function format($menus)
+    {
         $menuMap = [];
         foreach ($menus as $menu){
             $menuMap[$menu->id] = [
@@ -80,37 +80,8 @@ class MenuService
                 unset($menuMap[$menu->id]);
             }
         }
-        return array_values($this->formatMenu($menuMap,$authMap,$mapping));
+        return $menuMap;
     }
-
-    /**
-     * 递归格式化权限组
-     *
-     * @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;
-    }
-
     /**
      * 递归格式化菜单组
      *