100 lines
2.6 KiB
PHP
Raw Normal View History

2019-12-09 09:58:52 +08:00
<?php
2019-12-11 21:00:14 +08:00
namespace catchAdmin\permissions\controller;
2020-01-09 08:21:44 +08:00
use catcher\base\CatchRequest as Request;
2019-12-11 21:00:14 +08:00
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\Tree;
2020-01-17 15:17:51 +08:00
use catchAdmin\permissions\model\Permissions;
use think\response\Json;
2019-12-11 21:00:14 +08:00
2019-12-12 18:52:11 +08:00
class Permission extends CatchController
2019-12-11 21:00:14 +08:00
{
protected $permissions;
2019-12-12 18:52:33 +08:00
public function __construct(Permissions $permissions)
2019-12-11 21:00:14 +08:00
{
$this->permissions = $permissions;
}
/**
*
* @time 2019年12月11日
* @param Request $request
2020-01-17 15:17:51 +08:00
* @return Json
2019-12-11 21:00:14 +08:00
*/
2020-01-17 15:17:51 +08:00
public function index(): Json
2019-12-11 21:00:14 +08:00
{
2020-01-13 21:23:24 +08:00
return CatchResponse::success(Tree::done($this->permissions->getList()));
2019-12-11 21:00:14 +08:00
}
2020-01-17 15:17:51 +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
2019-12-11 21:00:14 +08:00
{
2020-01-17 15:17:51 +08:00
$params = $request->param();
2019-12-11 21:00:14 +08:00
2020-01-17 15:17:51 +08:00
// 如果是子分类 自动写入父类模块
$parentId = $params['parent_id'] ?? 0;
if ($parentId) {
$parent = $this->permissions->findBy($parentId);
2020-01-17 15:49:29 +08:00
$params['module'] = $parent->module;
2020-01-17 15:17:51 +08:00
}
2019-12-11 21:00:14 +08:00
2020-01-17 15:17:51 +08:00
return CatchResponse::success($this->permissions->storeBy($request->param()));
}
2019-12-11 21:00:14 +08:00
/**
*
* @time 2019年12月11日
* @param $id
* @param Request $request
2020-01-17 15:17:51 +08:00
* @return Json
2019-12-11 21:00:14 +08:00
*/
2020-01-17 15:17:51 +08:00
public function update($id, Request $request): Json
2019-12-11 21:00:14 +08:00
{
2020-01-17 15:17:51 +08:00
$permission = $this->permissions->findBy($id);
// 如果是父分类需要更新所有子分类的模块
if (!$permission->parent_id) {
$this->permissions->updateBy($permission->parent_id, [
'module' => $permission->module,
], 'parent_id');
}
2019-12-11 21:00:14 +08:00
return CatchResponse::success($this->permissions->updateBy($id, $request->param()));
}
/**
*
* @time 2019年12月11日
* @param $id
* @throws FailedException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
2020-01-17 15:17:51 +08:00
* @return Json
2019-12-11 21:00:14 +08:00
*/
2020-01-17 15:17:51 +08:00
public function delete($id): Json
2019-12-11 21:00:14 +08:00
{
if ($this->permissions->where('parent_id', $id)->find()) {
throw new FailedException('存在子菜单,无法删除');
}
2019-12-12 09:13:29 +08:00
$this->permissions->findBy($id)->roles()->detach();
2019-12-11 21:00:14 +08:00
return CatchResponse::success($this->permissions->deleteBy($id));
}
}