158 lines
4.7 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\permissions\controller;
use catcher\base\CatchRequest as Request;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
2020-06-05 15:20:22 +08:00
use catcher\library\ParseClass;
2020-04-29 17:37:45 +08:00
use catcher\Tree;
use catchAdmin\permissions\model\Permissions;
2020-09-04 20:29:38 +08:00
use think\helper\Str;
2020-04-29 17:37:45 +08:00
use think\response\Json;
class Permission extends CatchController
{
protected $permissions;
public function __construct(Permissions $permissions)
{
$this->permissions = $permissions;
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function index(Request $request): Json
{
// 获取菜单类型
$menuList = $this->permissions->getList(true);
// 获取按钮类型并且重新排列
$buttonList = [];
$this->permissions
->whereIn('parent_id', array_unique($menuList->column('id')))
->where('type', Permissions::BTN_TYPE)
->select()->each(function ($item) use (&$buttonList){
$buttonList[$item['parent_id']][] = $item->toArray();
});
// 子节点的 key
$children = $request->param('actionList') ?? 'children';
// 返回树结构
2020-10-21 08:12:07 +08:00
return CatchResponse::success($menuList->each(function (&$item) use ($buttonList, $children){
2020-04-29 17:37:45 +08:00
$item[$children] = $buttonList[$item['id']] ?? [];
2020-10-21 08:12:07 +08:00
})->toTree());
2020-04-29 17:37:45 +08:00
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function save(Request $request): Json
{
$params = $request->param();
// 如果是子分类 自动写入父类模块
$parentId = $params['parent_id'] ?? 0;
2020-09-05 16:01:18 +08:00
// 按钮类型寻找上级
if ($params['type'] == Permissions::BTN_TYPE && $parentId) {
$permissionMark = $params['permission_mark'];
// 查找父级
$parentPermission = $this->permissions->findBy($parentId);
// 如果父级是顶级 parent_id = 0
if ($parentPermission->parent_id) {
if (Str::contains($parentPermission->permission_mark, '@')) {
list($controller, $action) = explode('@', $parentPermission->permission_mark);
$permissionMark = $controller . '@' . $permissionMark;
} else {
$permissionMark = $parentPermission->permission_mark .'@'. $permissionMark;
}
}
$params['permission_mark'] = $permissionMark;
$params['module'] = $parentPermission->module;
2020-04-29 17:37:45 +08:00
}
return CatchResponse::success($this->permissions->storeBy($params));
}
/**
*
* @time 2019年12月11日
* @param $id
* @param Request $request
* @return Json
*/
public function update($id, Request $request): Json
{
$permission = $this->permissions->findBy($id);
2020-09-06 10:58:13 +08:00
$params = $request->param();
2021-03-31 20:20:21 +08:00
2020-09-06 10:58:13 +08:00
// 按钮类型
if ($params['type'] == Permissions::BTN_TYPE && $permission->parent_id) {
2021-05-16 17:24:02 +08:00
return CatchResponse::success($this->permissions->updateButton($params, $permission));
2020-09-04 20:29:38 +08:00
}
2021-05-16 17:24:02 +08:00
$params = array_merge($params, [
2020-04-29 17:37:45 +08:00
'parent_id' => $permission->parent_id,
'level' => $permission->level
]);
2021-05-16 17:24:02 +08:00
if ($this->permissions->updateMenu($id, $params)) {
return CatchResponse::success();
2020-04-29 17:37:45 +08:00
}
2021-05-16 17:24:02 +08:00
throw new FailedException('更新失败');
2020-04-29 17:37:45 +08:00
}
/**
*
* @time 2019年12月11日
* @param $id
* @throws FailedException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Json
*/
public function delete($id): Json
{
if ($this->permissions->where('parent_id', $id)->find()) {
throw new FailedException('存在子菜单,无法删除');
}
$this->permissions->findBy($id)->roles()->detach();
return CatchResponse::success($this->permissions->deleteBy($id));
}
2020-05-20 11:31:04 +08:00
/**
* 显示/隐藏
*
* @author JaguarJack
* @email njphper@gmail.com
* @time 2020/5/19
* @param $id
* @return Json
*/
public function show($id)
{
2021-04-03 10:35:53 +08:00
return CatchResponse::success($this->permissions->show($id));
2020-06-05 15:20:22 +08:00
}
2020-04-29 17:37:45 +08:00
}