crud调整
This commit is contained in:
@@ -2,97 +2,116 @@
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\validate\RoleValidate;
|
||||
use app\common\service\RoleService;
|
||||
use support\Request;
|
||||
use app\common\controller\BaseController;
|
||||
use app\admin\service\RoleService;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
|
||||
class RoleController extends \app\common\controller\BaseController
|
||||
class RoleController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取角色列表
|
||||
*/
|
||||
public function list(Request $request)
|
||||
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
$params = $request->all();
|
||||
$result = (new RoleService())->getRoleList($params);
|
||||
// 定义后处理器
|
||||
$postProcessor = function($formattedData) {
|
||||
if (isset($formattedData['created_at']))
|
||||
$formattedData['created_at'] = date('Y-m-d H:i:s', $formattedData['created_at']);
|
||||
if (isset($formattedData['updated_at']))
|
||||
$formattedData['updated_at'] = date('Y-m-d H:i:s', $formattedData['updated_at']);
|
||||
return $formattedData;
|
||||
};
|
||||
return $this->success($this->transformData($result, $postProcessor), '角色列表获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
$this->service = new RoleService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色详情
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
$result = (new RoleService())->getRoleDetail($roleId);
|
||||
return $this->success($result, '角色详情获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加角色
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_create');
|
||||
$result = (new RoleService())->createRole($data);
|
||||
return $this->success($result, '角色创建成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 获取角色列表
|
||||
// */
|
||||
// public function list(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $params = $request->all();
|
||||
// $result = (new RoleService())->getRoleList($params);
|
||||
// // 定义后处理器
|
||||
// $postProcessor = function($formattedData) {
|
||||
// if (isset($formattedData['created_at']))
|
||||
// $formattedData['created_at'] = date('Y-m-d H:i:s', $formattedData['created_at']);
|
||||
// if (isset($formattedData['updated_at']))
|
||||
// $formattedData['updated_at'] = date('Y-m-d H:i:s', $formattedData['updated_at']);
|
||||
// return $formattedData;
|
||||
// };
|
||||
// return $this->success($this->transformData($result, $postProcessor), '角色列表获取成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 修改角色
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_update');
|
||||
$result = (new RoleService())->updateRole($data);
|
||||
return $this->success($result, '角色更新成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 获取角色详情
|
||||
// */
|
||||
// public function detail(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $roleId = $request->get('role_id');
|
||||
// if (!$roleId) {
|
||||
// return $this->error('缺少角色ID', 422);
|
||||
// }
|
||||
// $result = (new RoleService())->getRoleDetail($roleId);
|
||||
// return $this->success($result, '角色详情获取成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
(new RoleService())->deleteRole($roleId);
|
||||
return $this->success([], '角色删除成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 增加角色
|
||||
// */
|
||||
// public function create(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $data = $this->validate(new RoleValidate(), $request->post(), 'role_create');
|
||||
// $result = (new RoleService())->createRole($data);
|
||||
// return $this->success($result, '角色创建成功');
|
||||
// } catch (\think\exception\ValidateException $e) {
|
||||
// return $this->error($e->getError(), 422);
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 修改角色
|
||||
// */
|
||||
// public function update(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $data = $this->validate(new RoleValidate(), $request->post(), 'role_update');
|
||||
// $result = (new RoleService())->updateRole($data);
|
||||
// return $this->success($result, '角色更新成功');
|
||||
// } catch (\think\exception\ValidateException $e) {
|
||||
// return $this->error($e->getError(), 422);
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 删除角色
|
||||
// */
|
||||
// public function delete(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $roleId = $request->get('role_id');
|
||||
// if (!$roleId) {
|
||||
// return $this->error('缺少角色ID', 422);
|
||||
// }
|
||||
// (new RoleService())->deleteRole($roleId);
|
||||
// return $this->success([], '角色删除成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user