Merge branch 'develop'
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\admin\service\CorpService;
|
||||
|
||||
class CorpController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new CorpService();
|
||||
}
|
||||
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果想对某些接口做额外校验或改写,
|
||||
* 也可以在这里覆盖 trait 提供的方法:
|
||||
*
|
||||
* public function store(Request $request)
|
||||
* {
|
||||
* // 自定义验证
|
||||
* $this->validate(...);
|
||||
* // 然后再调用标准逻辑
|
||||
* return parent::store($request);
|
||||
* }
|
||||
*/
|
||||
}
|
||||
@@ -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());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\admin\service\SystemsService;
|
||||
|
||||
class SystemsController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new SystemsService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
// protected function indexFormat($data)
|
||||
// {
|
||||
// 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;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\validate\CorpValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Corp;
|
||||
|
||||
class CorpService
|
||||
{
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Corp::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = CorpValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 如果需要自定义,比如强制加一个 where['status']=1,
|
||||
* 只要在这里重写:
|
||||
*/
|
||||
// protected function buildQueryConfig(Request $request): array
|
||||
// {
|
||||
// $config = [];
|
||||
// $config['where']['id'] = 2;
|
||||
// return $config;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 复杂查询示例:
|
||||
// * 只要在 URL 里添加 ?complex_query=1,
|
||||
// * 并传入你需要的任何参数,
|
||||
// * `serviceList()` 就会走到这里来:
|
||||
// */
|
||||
// protected function complexQuery(Request $request): array
|
||||
// {
|
||||
// // 假设前端传了 date_from、date_to
|
||||
// $from = $request->get('date_from');
|
||||
// $to = $request->get('date_to');
|
||||
|
||||
// // 交给 CrudUtil 执行一个自定义 query
|
||||
// $pager = $this->logicComplex(function($q) use ($from, $to) {
|
||||
// $q->where('status', 1);
|
||||
|
||||
// if ($from && $to) {
|
||||
// $q->whereBetween('created_at', [$from, $to]);
|
||||
// }
|
||||
|
||||
// // 再加个排序
|
||||
// $q->orderBy('created_at','desc');
|
||||
|
||||
// // 用 paginate 而不是 listAll
|
||||
// return $q->paginate(
|
||||
// /* perPage */ (int)request()->get('limit',15),
|
||||
// ['*'],
|
||||
// 'page',
|
||||
// (int)request()->get('page',1)
|
||||
// );
|
||||
// });
|
||||
|
||||
// // 把 Paginator 转成数组格式
|
||||
// return [
|
||||
// 'total' => $pager->total(),
|
||||
// 'data' => $pager->items(),
|
||||
// ];
|
||||
// }
|
||||
|
||||
|
||||
// protected function afterServiceList(array $res): array
|
||||
// {
|
||||
// // 对列表的 data 做处理
|
||||
// $res['data'] = array_map(fn($i)=>[...], $res['data']);
|
||||
// return $res;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\validate\RoleValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Role;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Role::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = RoleValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\validate\SystemsValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Systems;
|
||||
|
||||
class SystemsService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Systems::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = SystemsValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user