diff --git a/catch/permissions/controller/Permission.php b/catch/permissions/controller/Permission.php index 84f5721..b249efd 100644 --- a/catch/permissions/controller/Permission.php +++ b/catch/permissions/controller/Permission.php @@ -175,19 +175,7 @@ class Permission extends CatchController */ public function show($id) { - $permission = $this->permissions->findBy($id); - - $hidden = $permission->hidden == Permissions::ENABLE ? Permissions::DISABLE : Permissions::ENABLE; - - if ($this->permissions->where('id', $id)->update([ - 'hidden' => $hidden, - 'updated_at' => time() - ])) { - $this->permissions->where('parent_id', $id)->update([ - 'hidden' => $hidden, - 'updated_at' => time(), - ]); - } + $this->permissions->show($id); return CatchResponse::success(); } diff --git a/catch/permissions/model/Permissions.php b/catch/permissions/model/Permissions.php index 3652c90..add09a2 100644 --- a/catch/permissions/model/Permissions.php +++ b/catch/permissions/model/Permissions.php @@ -99,4 +99,44 @@ class Permissions extends CatchModel return true; } + + + public function show($id) + { + $permission = $this->findBy($id); + + // 不能使用改属性判断,模型有该属性,使用数组方式 + // $permission->hidden + $hidden = $permission['hidden'] == Permissions::ENABLE ? Permissions::DISABLE : Permissions::ENABLE; + + $nextLevelIds = $this->getNextLevel([$id]); + + $nextLevelIds[] = $id; + + return $this->whereIn('id', $nextLevelIds)->update([ + 'hidden' => $hidden, + 'updated_at' => time(), + ]); + } + + /** + * 获取 level ids + * + * @time 2020年09月06日 + * @param array $id + * @param array $ids + * @return array + */ + protected function getNextLevel(array $id, &$ids = []) + { + $_ids = $this->whereIn('parent_id', $id) + ->where('type', self::MENU_TYPE) + ->column('id'); + + if (count($_ids)) { + $ids = array_merge($_ids, $this->getNextLevel($_ids, $ids)); + } + + return $ids; + } }