Browse Source

提交文档并微调接口的返回值

tangs 5 years ago
parent
commit
bf70f0df96

+ 1 - 1
app/Http/Controllers/PermissionController.php

@@ -52,7 +52,7 @@ class PermissionController extends Controller
         $userActionBar = new Models\UserActionBar();
         $result = $userActionBar->AssignUserActionBar($ownerType, $ownerId, $barIds);
         if ($result["code"] == 0) {
-            return $this->success($result["data"]);
+            return $this->success(SUCCESS);
         }
         return $this->fail($result["code"], $this->error[$result["code"]]);
     }

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

@@ -61,9 +61,10 @@ class RoleController extends Controller
         $params["description"] = $request->input("description");
 
         $role = new Models\Role();
-        $code = $role->ModifyRole($params);
+        $result = $role->ModifyRole($params);
+        $code = $result["code"];
         if ($code == 0) {
-            return $this->success("success");
+            return $this->success($result["data"]);
         }
         return $this->fail($code, $this->error[$code]);
     }
@@ -145,6 +146,29 @@ class RoleController extends Controller
         return $this->fail($data["code"], $this->error[$data["code"]]);
     }
 
+    /**
+     * 管理员加载用户的角色信息
+     *
+     * @param Request $request
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function AdminLoadRole(Request $request)
+    {
+        $uid = (int)$request->input("userId");
+        if ($uid < 1) {
+            $this->fail(INVALID_USER_ID, $this->error[INVALID_USER_ID]);
+        }
+
+        $role = new Models\UserRole();
+        $data = $role->LoadRole($uid);
+
+        if ($data["code"] == 0){
+            return $this->success($data["data"]);
+        }
+
+        return $this->fail($data["code"], $this->error[$data["code"]]);
+    }
+
     /**
      * 列出角色下的用户
      *

+ 7 - 4
app/Models/Role.php

@@ -51,19 +51,19 @@ class Role extends Model
      * 更新用户角色的名字,描述等信息
      *
      * @param array $params
-     * @return int
+     * @return array
      */
     public function ModifyRole(array $params)
     {
         $update = [];
         $id = $params["id"];
         if ($id == "") {
-            return EMPTY_ROLE_ID;
+            return ["code" => EMPTY_ROLE_ID];
         }
         // check if role exist
         $role = $this->where("id", $id)->where("is_del", false)->first();
         if (!$role) {
-            return INVALID_ROLE_ID;
+            return ["code" => INVALID_ROLE_ID];
         }
 
         if ($params["name"] != "") {
@@ -76,7 +76,10 @@ class Role extends Model
             return NOTHING_UPDATE;
         }
         $this->where("id", $id)->where("is_del", false)->update($update);
-        return SUCCESS;
+
+        $role = $this->select("id", "name", "description", "status")
+            ->where("id", $id)->where("is_del", false)->first();
+        return ["code" => SUCCESS, "data" => $role];
     }
 
     /**

+ 2 - 2
app/Models/User.php

@@ -53,7 +53,7 @@ class User extends Model
         // check if user exist
         $user = $this->where("id", $uid)->where("is_del", false)->first();
         if (!$user) {
-            return 110;
+            return INVALID_USER_ID;
         }
 
         if ($params["username"] != "") {
@@ -75,7 +75,7 @@ class User extends Model
             return "nothing to update";
         }
         $this->where("id", $uid)->where("is_del", false)->update($update);
-        return 0;
+        return SUCCESS;
     }
 
     public function DeleteUser($uid)

+ 14 - 5
app/Models/UserActionBar.php

@@ -45,11 +45,20 @@ class UserActionBar extends Model
         }
         $this->insert($data);
 
-        // 查出批量插入的数据,并返回给前端
-        $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
-            ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
-
-        return ["code" => SUCCESS, "data" => $new_data];
+//        // 查出批量插入的数据,并返回给前端
+//        $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
+//            ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
+//
+//        $newBarIds = [];
+//        foreach ($new_data as $item) {
+//            array_push($newBarIds, $item["bar_id"]);
+//        }
+//        $barModel = new ActionBar();
+//        $barData = $barModel->LoadActionBarByIds_Format($barIds, false, ["id", "name"]);
+////        $barData = $barModel->ListActionBar_KV($barIds, false, ["id", "name"]);
+
+//        return ["code" => SUCCESS, "data" => ["bars" => $new_data, "barInfo" => $barData]];
+        return ["code" => SUCCESS];
     }
 
     /**

+ 21 - 5
app/Models/UserRole.php

@@ -30,13 +30,21 @@ class UserRole extends Model
         // 删除当前用户所有的角色
         $this->where("user_id", $uid)->where("is_del", false)->update(["is_del" => true]);
 
-        if (count($roles) < 1) {
+        // 过滤掉无效的roleId
+        $roleModel = new Role();
+        $validRoles = $roleModel->select("id")->whereIn("id", $roles)->where("is_del", false)->get();
+        $validRoleIds = [];
+        foreach ($validRoles as $validRole) {
+            array_push($validRoleIds, $validRole["id"]);
+        }
+
+        if (count($validRoleIds) < 1) {
             return ["code" => SUCCESS, "data" => ["roles" => []]];
         }
 
         // 重新添加用户角色新配置的角色数据
         $data = [];
-        foreach ($roles as $role) {
+        foreach ($validRoleIds as $role) {
             array_push($data, ["user_id" => $uid, "role_id" => $role]);
         }
         $this->insert($data);
@@ -75,13 +83,21 @@ class UserRole extends Model
 
         // 删除当前角色所有的用户
         $this->where("role_id", $rid)->where("is_del", false)->update(["is_del" => true]);
-        if (count($userIds) < 1) {
+
+        // 过滤掉无效的userId
+        $userModel = new User();
+        $validUsers = $userModel->select("id")->whereIn("id", $userIds)->where("is_del", false)->get();
+        $validUids = [];
+        foreach ($validUsers as $validUser) {
+            array_push($validUids, $validUser["id"]);
+        }
+        if (count($validUids) < 1) {
             return ["code" => SUCCESS, "data" => ["users" => []]];
         }
 
         // 重新添加指定角色下的用户数据
         $data = [];
-        foreach ($userIds as $userId) {
+        foreach ($validUids as $userId) {
             array_push($data, ["user_id" => $userId, "role_id" => $rid]);
         }
         $this->insert($data);
@@ -135,7 +151,7 @@ class UserRole extends Model
         $role = new Role();
         $roleInfo = $role->LoadRoleByIds_KV($roleIds);
 
-        $result = ["roles" => $roles, "roleInfo" => $roleInfo];
+        $result = ["roleUsers" => $roles, "roleInfo" => $roleInfo];
 
         return ["code" => SUCCESS, "data" => $result];
     }

+ 443 - 34
docs/router.md

@@ -1,7 +1,7 @@
 ### 创建用户
 - 请求方法 `POST`
 
-- 请求url:~/p/createUser
+- 请求url:~/admin/createUser
 
 - 请求参数
 
@@ -43,7 +43,7 @@
 ### 修改用户信息
 - 请求方法 `POST` 需要登录
 
-- 请求url:~/p/modifyUser
+- 请求url:~/admin/modifyUser
 
 - 请求参数
 
@@ -77,7 +77,7 @@
 ### 删除用户
 - 请求方法 `GET` 需要登录
 
-- 请求url:~/p/deleteUser
+- 请求url:~/admin/deleteUser
 
 - 请求参数
 
@@ -283,7 +283,7 @@
 
 - 请求方法 `POST` 需要登录
 
-- 请求url:~/p/createRole
+- 请求url:~/admin/createRole
 
 - 请求参数
 
@@ -320,7 +320,7 @@
 
 - 请求方法 `POST` 需要登录
 
-- 请求url:~/p/modifyRole
+- 请求url:~/admin/modifyRole
 
 - 请求参数
 
@@ -343,7 +343,12 @@
 {
     "code": 0,
     "message": "success",
-    "data": "success"
+    "data": {
+        "id": 11,
+        "name": "admin",
+        "description": "",
+        "status": "normal"
+    }
 }
 ```
 
@@ -353,7 +358,7 @@
 
 - 请求方法 `GET` 需要登录
 
-- 请求url:~/p/deleteRole
+- 请求url:~/admin/deleteRole
 
 - 请求参数
 
@@ -384,7 +389,7 @@
 
 - 请求方法 `GET` 需要登录
 
-- 请求url:~/p/listRole
+- 请求url:~/admin/listRole
 
 - 请求参数
 
@@ -405,39 +410,36 @@
 
 ```json
 {
-    "current_page":1,
-    "data":[
-        {
-            "name":"hal",
-            "description":"haloteacher"
-        }
-    ],
-    "first_page_url":"http://local.pc:8888/repair_lite/p/listRole?page=1",
-    "from":1,
-    "last_page":7,
-    "last_page_url":"http://local.pc:8888/repair_lite/p/listRole?page=7",
-    "next_page_url":"http://local.pc:8888/repair_lite/p/listRole?page=2",
-    "path":"http://local.pc:8888/repair_lite/p/listRole",
-    "per_page":1,
-    "prev_page_url":null,
-    "to":1,
-    "total":7
+    "code": 0,
+    "message": "success",
+    "data": {
+        "current_page": 1,
+        "per_page": 1,
+        "total": 10,
+        "roles": [
+            {
+                "id": 1,
+                "name": "a",
+                "description": null
+            }
+        ]
+    }
 }
 ```
 
 --- 
 
-### 配置某个用户为某角色,该方法未更新
+### 配置某个用户的角色
 - 请求方法 `GET` 需要登录
 
-- 请求url:~/p/assignUserRole
+- 请求url:~/admin/assignUserRole?userId=1&roleIds=2,3
 
 - 请求参数
 
 |字段名|描述|必选|
 |:-:|:-:|:-:|
-| uid | 配置角色的用户id | 是 |
-| barIds | 配置的菜单id,多个用逗号隔开 | 是 |
+| userId | 配置角色的用户id | 是 |
+| roleIds | 配置的角色id,多个用逗号隔开 | 是 |
 
 - 返回结果
 
@@ -445,7 +447,6 @@
 |:-:|:-:|
 | code | 错误码,0: success, 非0:异常 |
 | message | 错误码不为0时,该字段为错误码的描述 |
-| data.id | 菜单的id |
 
 - 实例
 
@@ -453,19 +454,427 @@
 {
     "code": 0,
     "message": "success",
-    "data": "success"
+    "data": {
+        "roles": [
+            {
+                "id": 69,
+                "user_id": 1,
+                "role_id": 2,
+                "status": "normal"
+            },
+            {
+                "id": 70,
+                "user_id": 1,
+                "role_id": 3,
+                "status": "normal"
+            }
+        ],
+        "roleInfo": {
+            "2": {
+                "id": 2,
+                "name": "b",
+                "description": null,
+                "status": null
+            },
+            "3": {
+                "id": 3,
+                "name": "c",
+                "description": null,
+                "status": null
+            }
+        }
+    }
 }
 ```
 
 --- 
 
 ### 配置某个角色下的用户
-暂无
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/admin/assignRoleUser?roleId=1&userIds=1,2
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+| roleId | 配置角色的角色id | 是 |
+| userIds | 配置的用户id,多个用逗号隔开 | 是 |
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+
+- 实例
+
+```json
+{
+    "code": 0,
+    "message": "success",
+    "data": {
+        "users": [
+            {
+                "id": 82,
+                "user_id": 1,
+                "role_id": 1,
+                "status": "normal"
+            },
+            {
+                "id": 83,
+                "user_id": 2,
+                "role_id": 1,
+                "status": "normal"
+            }
+        ],
+        "userInfo": {
+            "1": {
+                "id": 1,
+                "username": "11",
+                "nickname": "11",
+                "icon": null
+            },
+            "2": {
+                "id": 2,
+                "username": "123",
+                "nickname": "123",
+                "icon": null
+            }
+        }
+    }
+}
+```
+
+--- 
+
+
+### 列出角色下的用户
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/admin/loadRoleUsers?page=1&pageCount=1
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+| page | 角色的页数 | 否 |
+| pageCount | 角色的每页条数 | 否 |
+| keyword | 角色的关键字搜索 | 否 |
+
+> 这里是否需要对角色下的用户分页,待定。
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+| roles | 角色的信息 |
+| roleUsers | 角色下的用户,用角色id当做key |
+| total | 角色的总数 |
+
+- 实例
+
+```json
+{
+    "code": 0,
+    "message": "success",
+    "data": {
+        "roles": [
+            {
+            "id": 1,
+            "name": "a",
+            "description": null
+            }
+        ],
+        "roleUser": {
+            "1": [
+                {
+                    "id": 1,
+                    "username": "11",
+                    "nickname": "11",
+                    "icon": null
+                },
+                {
+                    "id": 2,
+                    "username": "123",
+                    "nickname": "123",
+                    "icon": null
+                },
+                {
+                    "id": 3,
+                    "username": "1234",
+                    "nickname": "1234",
+                    "icon": null
+                }
+            ]
+        },
+        "total": 11
+    }
+}
+```
+
+---
+
+### 用户加载自己的角色信息
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/loadRole
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+
+> 这个接口只要登录了即可获取角色信息
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+| roleUsers | 角色用户的数据 |
+| roleUsers.user_id | 用户id |
+| roleUsers.role_id | 角色id |
+| roleInfo | 角色信息,用角色id当做key |
+
+- 实例
+
+```json
+{
+    "code":0,
+    "message":"success",
+    "data":{
+        "roleUsers":[
+            {
+                "id":70,
+                "user_id":1,
+                "role_id":3,
+                "status":"normal"
+            },
+            {
+                "id":91,
+                "user_id":1,
+                "role_id":1,
+                "status":"normal"
+            }
+        ],
+        "roleInfo":{
+            "1":{
+                "id":1,
+                "name":"a",
+                "description":null,
+                "status":null
+            },
+            "3":{
+                "id":3,
+                "name":"c",
+                "description":null,
+                "status":null
+            }
+        }
+    }
+}
+```
+
+---
+
+### 管理员加载用户的角色信息
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/admin/loadRole?userId=1
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+| userId | 用户id| 是 |
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+| roleUsers | 角色用户的数据 |
+| roleUsers.user_id | 用户id |
+| roleUsers.role_id | 角色id |
+| roleInfo | 角色信息,用角色id当做key |
+
+- 实例
+
+```json
+{
+    "code":0,
+    "message":"success",
+    "data":{
+        "roleUsers":[
+            {
+                "id":70,
+                "user_id":1,
+                "role_id":3,
+                "status":"normal"
+            },
+            {
+                "id":91,
+                "user_id":1,
+                "role_id":1,
+                "status":"normal"
+            }
+        ],
+        "roleInfo":{
+            "1":{
+                "id":1,
+                "name":"a",
+                "description":null,
+                "status":null
+            },
+            "3":{
+                "id":3,
+                "name":"c",
+                "description":null,
+                "status":null
+            }
+        }
+    }
+}
+```
+
+---
+
+
+### 管理员分配用户菜单,分配角色菜单
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/admin/assignActionBars?ownerType=user&ownerId=1&barIds=1,2,3,4,5
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+| ownerType | 拥有者类型,可选值user/role | 是 |
+| ownerId | 拥有者id | 是 |
+| barIds | 菜单id,多个用逗号隔开 | 是 |
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+- 实例
+
+```json
+{
+    "code": 0,
+    "message": "success",
+    "data": "success"
+}
+```
+
+---
+
+
+### 加載角色下的菜单
+- 请求方法 `GET` 需要登录
+
+- 请求url:~/admin/assignActionBars?ownerType=user&ownerId=1&barIds=1,2,3,4,5
+
+- 请求参数
+
+|字段名|描述|必选|
+|:-:|:-:|:-:|
+| keyword | 关键字搜索 | 否 |
+
+- 返回结果
+
+|字段名|描述|
+|:-:|:-:|
+| code | 错误码,0: success, 非0:异常 |
+| message | 错误码不为0时,该字段为错误码的描述 |
+- 实例
+
+```json
+{
+    "code":0,
+    "message":"success",
+    "data":{
+        "roles":[
+            {
+                "id":2,
+                "name":"f",
+                "description":null
+            }
+        ],
+        "roleActionBar":{
+            "2":{
+                "0":[
+                    {
+                        "id":1,
+                        "parent":0,
+                        "name":"abdfdf",
+                        "description":null,
+                        "icon":"hhhe",
+                        "link_type":null,
+                        "link":null
+                    },
+                    {
+                        "id":2,
+                        "parent":0,
+                        "name":"abc",
+                        "description":null,
+                        "icon":"hhhe",
+                        "link_type":null,
+                        "link":null
+                    }
+                ],
+                "1":[
+                    {
+                        "id":3,
+                        "parent":1,
+                        "name":"second1",
+                        "description":null,
+                        "icon":null,
+                        "link_type":null,
+                        "link":null
+                    },
+                    {
+                        "id":4,
+                        "parent":1,
+                        "name":"second2",
+                        "description":null,
+                        "icon":null,
+                        "link_type":null,
+                        "link":null
+                    }
+                ],
+                "3":[
+                    {
+                        "id":5,
+                        "parent":3,
+                        "name":"third1",
+                        "description":null,
+                        "icon":null,
+                        "link_type":null,
+                        "link":null
+                    }
+                ]
+            }
+        }
+    }
+}
+```
+
+---
+
 
 ### 创建菜单
 - 请求方法 `POST` 需要登录
 
-- 请求url:~/p/addActionBar
+- 请求url:~/admin/addActionBar
 
 - 请求参数
 
@@ -624,4 +1033,4 @@
 }
 ```
 
---- 
+--- 

+ 5 - 4
routes/web.php

@@ -18,9 +18,9 @@ Route::get('/', function () {
 });
 
 //user
-Route::get("createUser", "UserController@CreateUser");
-Route::get("modifyUser", "UserController@ModifyUser");
-Route::get("deleteUser", "UserController@DeleteUser");
+Route::get("admin/createUser", "UserController@CreateUser");
+Route::get("admin/modifyUser", "UserController@ModifyUser");
+Route::get("admin/deleteUser", "UserController@DeleteUser");
 Route::get("listUser", "UserController@ListUser");
 
 // role
@@ -32,7 +32,8 @@ Route::get("admin/listRole", "RoleController@ListRole");
 Route::get("admin/loadRoleActionBars", "PermissionController@LoadRoleActionBars");
 
 
-Route::get("loadRole", "RoleController@LoadRole");
+Route::get("loadRole", "RoleController@LoadRole");// 用户加载自己的角色
+Route::get("admin/loadRole", "RoleController@AdminLoadRole"); // 管理员加载指定用户的角色
 
 
 // user role