修改权限

This commit is contained in:
JaguarJack 2020-04-22 20:17:19 +08:00
parent ef64b8a8cb
commit e7491555e8
2 changed files with 38 additions and 4 deletions

View File

@ -22,12 +22,13 @@ class Permission extends CatchController
/** /**
* *
* @time 2019年12月11日 * @time 2019年12月11日
* @param Request $request
* @return Json * @return Json
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
*/ */
public function index(): Json public function index(Request $request): Json
{ {
// 获取菜单类型 // 获取菜单类型
$menuList = $this->permissions->getList(true); $menuList = $this->permissions->getList(true);
@ -41,9 +42,11 @@ class Permission extends CatchController
$buttonList[$item['parent_id']][] = $item->toArray(); $buttonList[$item['parent_id']][] = $item->toArray();
}); });
// 子节点的 key
$children = $request->param('actionList') ?? 'children';
// 返回树结构 // 返回树结构
return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList){ return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList, $children){
$item['children'] = $buttonList[$item['id']] ?? []; $item[$children] = $buttonList[$item['id']] ?? [];
})->toArray())); })->toArray()));
} }
@ -81,6 +84,11 @@ class Permission extends CatchController
{ {
$permission = $this->permissions->findBy($id); $permission = $this->permissions->findBy($id);
$params = array_merge($request->param(), [
'parent_id' => $permission->parent_id,
'level' => $permission->level
]);
// 如果是父分类需要更新所有子分类的模块 // 如果是父分类需要更新所有子分类的模块
if (!$permission->parent_id) { if (!$permission->parent_id) {
$this->permissions->updateBy($permission->parent_id, [ $this->permissions->updateBy($permission->parent_id, [
@ -88,7 +96,7 @@ class Permission extends CatchController
], 'parent_id'); ], 'parent_id');
} }
return CatchResponse::success($this->permissions->updateBy($id, $request->param())); return CatchResponse::success($this->permissions->updateBy($id, $params));
} }
/** /**

View File

@ -3,6 +3,7 @@ namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\PermissionsSearch; use catchAdmin\permissions\model\search\PermissionsSearch;
use catcher\base\CatchModel; use catcher\base\CatchModel;
use think\Model;
class Permissions extends CatchModel class Permissions extends CatchModel
{ {
@ -74,4 +75,29 @@ class Permissions extends CatchModel
]) ])
->select(); ->select();
} }
/**
* 插入后回调 更新 level
*
* @time 2020年04月22日
* @param Model $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|bool|Model|void|null
*/
public static function onAfterInsert(Model $model)
{
$model = self::where('id', $model->id)->find();
if ($model && $model->parent_id) {
$parent = self::where('id', $model->parent_id)->find();
$level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id;
return $model->where('id', $model->id)->update([
'level' => $level
]);
}
return true;
}
} }