119 lines
2.8 KiB
PHP
119 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace app\applet\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use app\applet\model\DraftsDataModel;
|
|
use app\applet\service\DraftsDataService;
|
|
use app\applet\validate\DraftsValidator;
|
|
use app\mzl\common\controller\BaseController;
|
|
|
|
class DraftsDataController extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 不需要验证token的接口
|
|
* @var array
|
|
*/
|
|
protected $noNeedJwt = [];
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected mixed $validator = DraftsValidator::class;
|
|
|
|
/**
|
|
* 返回业务 Service 实例
|
|
*/
|
|
protected function getService()
|
|
{
|
|
return new DraftsDataService(new DraftsDataModel());
|
|
}
|
|
|
|
/**
|
|
* 额外字段处理 (根据场景处理)
|
|
* @param array $item
|
|
* @param string $scene
|
|
* @return array
|
|
*/
|
|
protected function postProcessItem(array $item, string $scene): array
|
|
{
|
|
|
|
if ($scene === 'default') {
|
|
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
/**
|
|
* 参数预处理 (根据场景处理)
|
|
* @param Request $request
|
|
* @param string $scene
|
|
* @return array
|
|
*/
|
|
protected function prepareParams(Request $request, string $scene): array
|
|
{
|
|
$params = parent::prepareParams($request, $scene);
|
|
|
|
if ($scene === 'default') {
|
|
|
|
}
|
|
|
|
return $params;
|
|
}
|
|
|
|
/**
|
|
* 分页
|
|
*
|
|
* @param Request $request 请求对象
|
|
* @return Response 响应对象
|
|
*/
|
|
public function index(Request $request, string $scene = 'default'): Response
|
|
{
|
|
try {
|
|
return parent::index($request, $scene);
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 新增草稿
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
|
|
public function add(Request $request): Response
|
|
{
|
|
try {
|
|
$data = $this->injectAdd($request->all());
|
|
return $this->getService()->add($data)
|
|
? $this->successResponse()
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function show(Request $request, int $id, string $scene = 'show'): Response
|
|
{
|
|
try {
|
|
return parent::show($request, $id, $scene);
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(Request $request, int $id): Response
|
|
{
|
|
try {
|
|
return $this->getService()->edit($request->all(), $id)
|
|
? $this->successResponse()
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
} |