Ken пре 2 недеља
родитељ
комит
af9f5ceda8
2 измењених фајлова са 24 додато и 6 уклоњено
  1. 17 0
      app/Helpers/helpers.php
  2. 7 6
      app/Http/Controllers/admin/Menu.php

+ 17 - 0
app/Helpers/helpers.php

@@ -2,6 +2,23 @@
 
 use Illuminate\Support\Facades\Lang;
 
+if (!function_exists('list_to_tree')) {
+    function list_to_tree($elements, $parentKey = "parent_id", $parentId = 0):array
+    {
+        $branch = [];
+        foreach ($elements as $element) {
+            if ($element['$parentKey'] == $parentId) {
+                $children = list_to_tree($elements, $element['id']);
+                if ($children) {
+                    $element['children'] = $children;
+                }
+                $branch[] = $element;
+            }
+        }
+        return $branch;
+    }
+}
+
 if (!function_exists('custom_sort')) {
     function custom_sort($a, $b)
     {

+ 7 - 6
app/Http/Controllers/admin/Menu.php

@@ -63,7 +63,7 @@ class Menu extends Controller
      * @apiUse header
      * @apiVersion 1.0.0
      *
-     * @apiParam {int} id 菜单ID 
+     * @apiParam {int} id 菜单ID
      * @apiParam {string} title 菜单名称
      * @apiParam {string} [uri] 菜单链接地址/路由
      * @apiParam {string} [parent_id] 父级菜单ID
@@ -148,6 +148,7 @@ class Menu extends Controller
     {
         $search = request()->all();
         $result = MenuService::findAll($search);
+        $result = list_to_tree($result);
         return $this->success($result);
     }
 
@@ -220,12 +221,12 @@ class Menu extends Controller
     public function check()
     {
         $userId = request()->user->id;
-        $uri = request()->post('uri','');
-        $result = MenuService::checkMenu($userId,$uri);
-        if($result){
+        $uri = request()->post('uri', '');
+        $result = MenuService::checkMenu($userId, $uri);
+        if ($result) {
             return $this->success($result, '有权限访问');
-        }else{
-            return $this->error(99,'没有权限访问',$result);
+        } else {
+            return $this->error(99, '没有权限访问', $result);
         }
     }