update:支持无限级的菜单隐藏

This commit is contained in:
JaguarJack 2020-09-06 11:18:12 +08:00
parent f301f149fb
commit e712f824f5
2 changed files with 41 additions and 13 deletions

View File

@ -175,19 +175,7 @@ class Permission extends CatchController
*/ */
public function show($id) public function show($id)
{ {
$permission = $this->permissions->findBy($id); $this->permissions->show($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(),
]);
}
return CatchResponse::success(); return CatchResponse::success();
} }

View File

@ -99,4 +99,44 @@ class Permissions extends CatchModel
return true; 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;
}
} }