From d8496f9e629f8c26e6823fdcc1c6fdd2b1c788e2 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 7 Feb 2021 17:21:34 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Role.php | 31 +++++++++++++++++++++++++-- composer.json | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) 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",