diff --git a/catch/permissions/controller/Role.php b/catch/permissions/controller/Role.php index ebc2c52..53f34ed 100644 --- a/catch/permissions/controller/Role.php +++ b/catch/permissions/controller/Role.php @@ -50,7 +50,9 @@ class Role extends CatchController if (!empty($permissions)) { $this->role->attachPermissions(array_unique($permissions)); } - + if (!empty($params['departments'])) { + $this->role->attachDepartments($params['departments']); + } // 添加角色 return CatchResponse::success(); } @@ -59,6 +61,7 @@ class Role extends CatchController { $role = $this->role->findBy($id); $role->permissions = $role->getPermissions(); + $role->departments = $role->getDepartments(); return CatchResponse::success($role); } @@ -101,6 +104,28 @@ class Role extends CatchController $role->attachPermissions(array_unique($attachIds)); } + // 更新department + $hasDepartmentIds = $role->getDepartments()->column('id'); + $departmentIds = $request->param('departments',[]); + + // 已存在部门 IDS + $existedDepartmentIds = []; + foreach ($hasDepartmentIds as $hasDepartmentId) { + if (in_array($hasDepartmentId, $departmentIds)) { + $existedDepartmentIds[] = $hasDepartmentId; + } + } + + $attachDepartmentIds = array_diff($departmentIds, $existedDepartmentIds); + $detachDepartmentIds = array_diff($hasDepartmentIds, $existedDepartmentIds); + + if (!empty($detachDepartmentIds)) { + $role->detachDepartments($detachDepartmentIds); + } + if (!empty($attachDepartmentIds)) { + $role->attachDepartments(array_unique($attachDepartmentIds)); + } + return CatchResponse::success(); } @@ -122,6 +147,8 @@ class Role extends CatchController $role = $this->role->findBy($id); // 删除权限 $role->detachPermissions(); + // 删除部门关联 + $role->detachDepartments(); // 删除用户关联 $role->users()->detach(); // 删除 @@ -147,7 +174,7 @@ class Role extends CatchController } } - $permissions = Tree::done(Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toArray()); + $permissions = Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toTree(); $permissionIds = []; if ($request->param('role_id')) { diff --git a/composer.json b/composer.json index 26915c8..18871b4 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": ">=7.1.0", "topthink/framework": "6.0.6", - "topthink/think-orm": "2.0.33", + "topthink/think-orm": "2.0.36", "topthink/think-migration": "^3.0", "thans/tp-jwt-auth": "1.1", "overtrue/wechat": "^4.2",