Browse Source

完善列出角色下的菜单

tangs 6 years ago
parent
commit
b17f9c83f1

+ 36 - 0
app/Http/Controllers/PermissionController.php

@@ -162,4 +162,40 @@ class PermissionController extends Controller
         }
         return $intArray;
     }
+
+// 管理员列出角色下的actionBars
+    public function LoadRoleActionBars(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            "page" => "integer|min:1",
+            "pageCount" => "integer|min:1",
+            "keyword" => "max:255",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
+        $params = [];
+        $params["page"] = (int)$request->input("page");
+        $params["pageCount"] = (int)$request->input("pageCount");
+        $params["keyword"] = $request->input("keyword");
+
+        // 先加载所有的角色信息
+        $role = new Models\Role();
+        $roleData = $role->ListRole($params);
+
+        $roles = $roleData["roles"];
+        $roleIds = [];
+        if (count($roles) < 1) {
+            return $this->success(SUCCESS, ["roles"=>[]]);
+        }
+
+        // 加载角色下的菜单数据
+        $userBars = new Models\UserActionBar();
+        $bars = $userBars->ListActionBarIds_Format(["role"], $roleIds);
+        $bars["roles"] = $roles;
+
+        return $bars;
+
+    }
 }

+ 5 - 2
app/Http/Controllers/RoleController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
 
 use App\Models;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Validator;
 
 class RoleController extends Controller
@@ -113,10 +114,12 @@ class RoleController extends Controller
         $params = [];
         $params["page"] = (int)$request->input("page");
         $params["pageCount"] = (int)$request->input("pageCount");
+        $params["keyword"] = $request->input("keyword");
 
         $role = new Models\Role();
-        $result = $role->ListRole($params);
-        return $result;
+        $roleData = $role->ListRole($params);
+
+        return $this->success($roleData);
     }
 
     /**

+ 22 - 3
app/Models/ActionBar.php

@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use foo\bar;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -127,17 +128,35 @@ class ActionBar extends Model
 
         // 收集当前用户有权限的bar_id
         $userActionBar = new UserActionBar();
-        $bar_ids = $userActionBar->ListActionBarIds("", $uid);
-        if (count($bar_ids) == 0) {
+        $bars = $userActionBar->ListActionBarIds([""], [$uid]);
+        if (count($bars) == 0) {
             return ["code" => SUCCESS, "data" => []];
         }
 
+        $barIds=[];
+        foreach ($bars as $bar){
+            array_push($barIds, $bar["bar_id"]);
+        }
+
         // 查出bars的信息
         $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
-            ->where("id", $bar_ids)
+            ->whereIn("id", $bars)
             ->where("status", "normal")->where("is_del", false)->get();
         return ["code" => SUCCESS, "data" => $result];
     }
+
+    /**
+     * 获取菜单栏的数据信息
+     *
+     * @param $ids
+     * @return mixed
+     */
+    public function LoadActionBarByIds_Format($ids)
+    {
+        $bars = $this->select("id","parent", "name","description", "icon", "link_type", "link", "status")
+            ->whereIn("id", $ids)->where("is_del", false)->get();
+        return $bars;
+    }
 }
 
 

+ 19 - 3
app/Models/Role.php

@@ -117,10 +117,26 @@ class Role extends Model
             $pageCount = 15;
         }
 
-        $data = $this->select("name", "description")
-            ->where("is_del", false)->orderBy("created_at", "asc")
+        $opera = $this->select("id", "name", "description");
+        if ($params["keyword"] != "") {
+            $opera = $opera->where("name", "like", "%" . $params["keyword"] . "%");
+        }
+        $data = $opera->where("is_del", false)->orderBy("created_at", "asc")
             ->paginate($pageCount, ["*"], "page", $page);
-        return $data;
+
+        $roleDataArr = json_decode(json_encode($data), true);
+        $roleDataArr["roles"] = $roleDataArr["data"];
+
+        unset($roleDataArr["first_page_url"]);
+        unset($roleDataArr["from"]);
+        unset($roleDataArr["last_page"]);
+        unset($roleDataArr["last_page_url"]);
+        unset($roleDataArr["next_page_url"]);
+        unset($roleDataArr["path"]);
+        unset($roleDataArr["prev_page_url"]);
+        unset($roleDataArr["to"]);
+        unset($roleDataArr["data"]);
+        return $roleDataArr;
     }
 
     public function LoadRoleByIds($rids)

+ 50 - 40
app/Models/UserActionBar.php

@@ -52,54 +52,64 @@ class UserActionBar extends Model
         return ["code" => SUCCESS, "data" => $new_data];
     }
 
-    private function checkActionBar($barId)
-    {
-        return SUCCESS;
-    }
-
-    private function checkOwner($ownerType, $ownerId)
-    {
-        return SUCCESS;
-    }
-
-    public function RemoveUserActionBar($id)
+    /**
+     * 根据多个类型,和多个id,加载菜单栏信息
+     *
+     * @param array $ownerTypes
+     * @param array $ownerIds
+     * @return array
+     */
+    public function ListActionBarIds(array $ownerTypes, array $ownerIds)
     {
-        if ($id < 1) {
-            return INVALID_U_A_ID;
+        $opera = $this->select("owner_type", "owner_id", "bar_id");
+        if (count($ownerTypes) > 0) {
+            $opera = $opera->whereIn("owner_type", $ownerTypes);
+        }
+        if (count($ownerIds) > 0) {
+            $opera = $opera->whereIn("owner_id", $ownerIds);
         }
-        // check the data if exist in system.
-//        $item = $this->where("id", $id)->where("is_del", false)->first();
-//        if (!$item) {
-//            return INVALID_U_A_ID;
-//        }
-
-        $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
-        if ($row == 0) {
-            return INVALID_GROUP_ID;
+        $userBars = $opera->where("is_del", false)->get();
+        if (count($userBars) == 0) {
+            return ["useBars" => []];
         }
-        return SUCCESS;
+        $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];
     }
 
-    /**
-     * @param $ownerType
-     * @param $ownerId
-     * @return array
-     */
-    public function ListActionBarIds($ownerType, $ownerId)
+    public function ListActionBarIds_Format(array $ownerTypes, array $ownerIds)
     {
-        $opera = $this->select("bar_id");
-        if ($ownerType != "") {
-            $opera = $opera->where("owner_type", $ownerType);
+        $data = $this->ListActionBarIds($ownerTypes, $ownerIds);
+
+        $userBars = $data["userBars"];
+        if (count($userBars) < 1) {
+            return ["userBars" => (object)[]];
         }
-        $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
-        if (count($bars) == 0) {
-            return [];
+
+        $userBarFormat = [];
+        foreach ($userBars as $userBar) {
+            $key = $userBar["owner_id"];
+            if (!key_exists($userBar["owner_id"], $userBarFormat)) {
+                $userBarFormat[$key] = [];
+            }
+
+            $tempArr = $userBarFormat[$key];
+            array_push($tempArr, $userBar);
+            $userBarFormat[$key] = $tempArr;
         }
-        $bar_ids = [];
-        foreach ($bars as $bar) {
-            array_push($bar_ids, $bar["bar_id"]);
+
+        $result = ["userBars" => $userBarFormat, "barInfo" => (object)[]];
+        if (key_exists("barInfo", $data)) {
+            $result["barInfo"] = $data["barInfo"];
         }
-        Log::debug("========" . json_encode($bar_ids));
-        return $bar_ids;
+
+        return $result;
     }
 }

+ 3 - 0
routes/web.php

@@ -29,6 +29,9 @@ Route::get("admin/modifyRole", "RoleController@ModifyRole");
 Route::get("admin/deleteRole", "RoleController@DeleteRole");
 Route::get("admin/listRole", "RoleController@ListRole");
 
+Route::get("admin/loadRoleActionBars", "PermissionController@LoadRoleActionBars");
+
+
 Route::get("loadRole", "RoleController@LoadRole");