Browse Source

完善列出角色下的菜单数据

tangs 6 years ago
parent
commit
76d93f74a4

+ 17 - 15
app/Http/Controllers/ActionBarController.php

@@ -87,24 +87,26 @@ class ActionBarController extends Controller
         return $this->response($code, $this->error[$code]);
     }
 
+    /**
+     * 管理员加载所有的菜单数据,根据层次返回数据给前端
+     *
+     * @param Request $request
+     * @return \Illuminate\Http\JsonResponse
+     */
     public function LoadActionBar(Request $request)
     {
-        $validator = Validator::make($request->all(), [
-            "uid" => "required|integer|min:1",
-        ]);
-        if ($validator->fails()) {
-            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
-        }
-
-        $params = [];
-        $uid = $request->input("uid");
-        $params["uid"] = $uid;
+//        $validator = Validator::make($request->all(), [
+//            "uid" => "required|integer|min:1",
+//        ]);
+//        if ($validator->fails()) {
+//            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+//        }
 
+//        $params = [];
+//        $uid = $request->input("uid");
+//        $params["uid"] = $uid;
         $bar = new Models\ActionBar();
-        $result = $bar->ListActionBar($params);
-        if ($result["code"] == 0){
-            return $this->success($result["data"]);
-        }
-        return $this->fail($result["code"], $this->error[$result["code"]]);
+        $result = $bar->LoadActionBarByIds_Format([], true);
+        return $this->success($result);
     }
 }

+ 9 - 3
app/Http/Controllers/PermissionController.php

@@ -164,6 +164,13 @@ class PermissionController extends Controller
     }
 
 // 管理员列出角色下的actionBars
+
+    /**
+     * 列出角色下的菜单数据
+     *
+     * @param Request $request
+     * @return array|\Illuminate\Http\JsonResponse
+     */
     public function LoadRoleActionBars(Request $request)
     {
         $validator = Validator::make($request->all(), [
@@ -193,9 +200,8 @@ class PermissionController extends Controller
         // 加载角色下的菜单数据
         $userBars = new Models\UserActionBar();
         $bars = $userBars->ListActionBarIds_Format(["role"], $roleIds);
-        $bars["roles"] = $roles;
-
-        return $bars;
+        $result = ["roles" => $roles, "roleActionBar" => $bars];
 
+        return $this->success($result);
     }
 }

+ 126 - 23
app/Models/ActionBar.php

@@ -117,45 +117,148 @@ class ActionBar extends Model
     }
 
     /**
-     * 查出当前用户可以访问的所有action_bar的信息
+     * 列出所有的菜单数据
      *
-     * @param array $params
+     * @param array $ids
+     * @param bool $isAll
+     * @param array $selector
      * @return array
      */
-    public function ListActionBar(array $params)
+    public function ListActionBar(array $ids, bool $isAll, array $selector)
     {
-        $uid = $params["uid"];
+        if (count($ids) < 1 && !$isAll) {
+            return [];
+        }
+        $opera = $this->select($selector);
+        if (!$isAll) {
+            $opera = $opera->whereIn("id", $ids);
+        }
+        $data = $opera->where("is_del", false)->orderBy("id", "asc")->get();
+        return $data;
+    }
 
-        // 收集当前用户有权限的bar_id
-        $userActionBar = new UserActionBar();
-        $bars = $userActionBar->ListActionBarIds([""], [$uid]);
+    public function ListActionBar_KV(array $ids, bool $isAll, array $selector)
+    {
+        $bars = $this->ListActionBar($ids, $isAll, $selector);
         if (count($bars) == 0) {
-            return ["code" => SUCCESS, "data" => []];
+            return [];
+        }
+        $result = [];
+        foreach ($bars as $bar) {
+            $result[$bar["id"]] = $bar;
         }
+        return $result;
+    }
+
 
-        $barIds=[];
-        foreach ($bars as $bar){
-            array_push($barIds, $bar["bar_id"]);
+    /**
+     * 将菜单数据重新组合成有层级的数据,将不同等级的菜单放进多维数组里。
+     * parent=0的放在第一级。
+     *
+     * @param array $ids
+     * @param bool $isAll
+     * @return array
+     */
+    public function LoadActionBarByIds_Format(array $ids, bool $isAll)
+    {
+        if (count($ids) < 1 && !$isAll) {
+            return [];
+        }
+        $opera = $this->select("id", "parent", "name", "description", "icon", "link_type", "link", "status");
+        if (!$isAll) {
+            $opera = $opera->whereIn("id", $ids);
         }
+        $bars = $opera->orderBy("parent", "asc")->orderBy("id", "asc")->where("is_del", false)->get();
 
-        // 查出bars的信息
-        $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
-            ->whereIn("id", $bars)
-            ->where("status", "normal")->where("is_del", false)->get();
-        return ["code" => SUCCESS, "data" => $result];
+        $result = $this->AssembleBars(json_decode(json_encode($bars), true));
+        return $result;
     }
 
     /**
-     * 获取菜单栏的数据信息
+     * 把actionBar根据parent组成成有上下级层次的结构,起始 parent 为 0。
+     * 这里存在的问题是,返回出去的数据,如果只有一个key,并且key为0时,
+     * 数据会是一个array,所以外层调用的地方有必要时可以强制转为object,如:
+     * 当渴望的数据是:
+    {
+        "1":{
+            "0":[
+                {
+                    "id":1,
+                    "parent":0,
+                    "name":"abdfdf",
+                    "description":null,
+                    "icon":"hhhe",
+                    "link_type":null,
+                    "link":null
+                }
+            ]
+        }
+    }
+     * 得到的却是:
+    {
+        "1":[
+                [
+                    {
+                    "id":1,
+                    "parent":0,
+                    "name":"abdfdf",
+                    "description":null,
+                    "icon":"hhhe",
+                    "link_type":null,
+                    "link":null
+                    }
+                ]
+        ]
+    }
      *
-     * @param $ids
-     * @return mixed
+     * @param array $bars
+     * @return array
      */
-    public function LoadActionBarByIds_Format($ids)
+    public function AssembleBars(array $bars)
     {
-        $bars = $this->select("id","parent", "name","description", "icon", "link_type", "link", "status")
-            ->whereIn("id", $ids)->where("is_del", false)->get();
-        return $bars;
+        // 开始将菜单数据重新组合成有层级的数据
+        $tmpRes = [];
+        foreach ($bars as $bar) {
+            $parent = $bar["parent"];
+            Log::debug($parent);
+            if (!key_exists($parent, $tmpRes)) {
+                $tmpRes[$parent] = [];
+            }
+            $arr = $tmpRes[$parent];
+            array_push($arr, $bar);
+            $tmpRes[$parent] = $arr;
+        }
+        // 消除没有上层的菜单,或者消除上层菜单无效的菜单数据
+        $result = [];
+        $this->ClearInvalidActionBars(["0"], $tmpRes, $result);
+        return $result;
+    }
+
+    /**
+     * 消除没有上层的菜单,或者消除上层菜单无效的菜单数据
+     *
+     * @param array $ids
+     * @param array $data
+     * @param $newData
+     */
+    private function ClearInvalidActionBars(array $ids, array $data, &$newData)
+    {
+        if (count($ids) == 0) {
+            return;
+        }
+        $newIds = [];
+        foreach ($ids as $id) {
+            if (!key_exists($id, $data)) {
+                continue;
+            }
+            $newData[$id] = $data[$id];
+            $bars = $data[$id];
+            foreach ($bars as $bar) {
+                array_push($newIds, $bar["id"]);
+            }
+        }
+        $this->ClearInvalidActionBars($newIds, $data, $newData);
+        return;
     }
 }
 

+ 37 - 25
app/Models/UserActionBar.php

@@ -70,44 +70,56 @@ class UserActionBar extends Model
         }
         $userBars = $opera->where("is_del", false)->get();
         if (count($userBars) == 0) {
-            return ["useBars" => []];
+            return [];
         }
-        $barIds = [];
-        foreach ($userBars as $bar) {
-            array_push($barIds, $bar["bar_id"]);
-        }
-
-        // 加载actionBar 的信息
-        $bar = new ActionBar();
-        $barInfo = $bar->LoadActionBarByIds_Format($barIds);
-
-        return ["userBars" => $userBars, "barInfo" => $barInfo];
+        return $userBars;
     }
 
+    /**
+     *  列出actionBars,并且根据parent组合成分层次的数据结构
+     *
+     * @param array $ownerTypes
+     * @param array $ownerIds
+     * @return array
+     */
     public function ListActionBarIds_Format(array $ownerTypes, array $ownerIds)
     {
-        $data = $this->ListActionBarIds($ownerTypes, $ownerIds);
-
-        $userBars = $data["userBars"];
+        // 先收集所有的actionBars,并且根据角色分类
+        $userBars = $this->ListActionBarIds($ownerTypes, $ownerIds);
         if (count($userBars) < 1) {
             return ["userBars" => (object)[]];
         }
 
-        $userBarFormat = [];
+        // 收集所有的bar id
+        $barIds = [];
+        foreach ($userBars as $bar) {
+            array_push($barIds, $bar["bar_id"]);
+        }
+        // 加载actionBar 的信息
+        $bar = new ActionBar();
+        $barInfo = $bar->ListActionBar_KV([], true, ["id", "parent", "name", "description", "icon", "link_type", "link"]);
+
+        // 根据角色分类收集各自的action bar
+        $roleActionBars = [];
         foreach ($userBars as $userBar) {
-            $key = $userBar["owner_id"];
-            if (!key_exists($userBar["owner_id"], $userBarFormat)) {
-                $userBarFormat[$key] = [];
-            }
+            $key = strval($userBar["owner_id"]);
+            $barId = $userBar["bar_id"];
 
-            $tempArr = $userBarFormat[$key];
-            array_push($tempArr, $userBar);
-            $userBarFormat[$key] = $tempArr;
+            if (!key_exists($key, $roleActionBars)) {
+                $roleActionBars[$key] = [];
+            }
+            $actionBars = $roleActionBars[$key];
+            if (key_exists($barId, $barInfo)) {
+                array_push($actionBars, $barInfo[$barId]);
+            }
+            $roleActionBars[$key] = $actionBars;
         }
 
-        $result = ["userBars" => $userBarFormat, "barInfo" => (object)[]];
-        if (key_exists("barInfo", $data)) {
-            $result["barInfo"] = $data["barInfo"];
+        // 处理每个角色有用的actionBar,并分层
+        $result = [];
+        foreach ($roleActionBars as $key => $value) {
+            $assembleValue = $bar->AssembleBars($value);
+            $result[$key] = (object)$assembleValue;
         }
 
         return $result;

+ 4 - 4
routes/web.php

@@ -58,7 +58,7 @@ Route::get("groupRemoveUser", "GroupUserController@GroupRemoveUser");
 Route::get("grantPrivilege", "PermissionController@GrantPrivilege");
 
 // action bar
-Route::get("addActionBar", "ActionBarController@AddActionBar");
-Route::get("modifyActionBar", "ActionBarController@ModifyActionBar");
-Route::get("deleteActionBar", "ActionBarController@DeleteActionBar");
-Route::get("loadActionBar", "ActionBarController@LoadActionBar");
+Route::get("admin/addActionBar", "ActionBarController@AddActionBar");
+Route::get("admin/modifyActionBar", "ActionBarController@ModifyActionBar");
+Route::get("admin/deleteActionBar", "ActionBarController@DeleteActionBar");
+Route::get("admin/loadActionBar", "ActionBarController@LoadActionBar");