2025-07-08 09:59:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\console\controller;
|
|
|
|
|
|
|
|
|
|
use support\Request;
|
|
|
|
|
use support\Response;
|
|
|
|
|
use app\console\model\AdModel;
|
|
|
|
|
use app\console\service\AdService;
|
|
|
|
|
use app\console\validate\AdValidator;
|
|
|
|
|
use app\mzl\common\controller\BaseController;
|
|
|
|
|
|
|
|
|
|
class AdController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 不需要验证token的接口
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $noNeedJwt = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证器
|
|
|
|
|
*/
|
|
|
|
|
protected mixed $validator = AdValidator::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回业务 Service 实例
|
|
|
|
|
*/
|
|
|
|
|
protected function getService()
|
|
|
|
|
{
|
|
|
|
|
return new AdService(new AdModel());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 额外字段处理 (根据场景处理)
|
|
|
|
|
* @param array $item
|
|
|
|
|
* @param string $scene
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function postProcessItem(array $item, string $scene): array
|
|
|
|
|
{
|
|
|
|
|
|
2025-07-08 15:38:43 +08:00
|
|
|
if ($scene === 'default' || $scene === 'show') {
|
|
|
|
|
$item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
|
|
|
|
|
$item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
|
2025-07-08 10:05:34 +08:00
|
|
|
$item['alt'] = isset($item['translation']) ? $item['translation']['alt'] : '';
|
|
|
|
|
$item['title'] = isset($item['translation']) ? $item['translation']['title'] : '';
|
|
|
|
|
unset($item['translation']);
|
2025-07-08 15:38:43 +08:00
|
|
|
$item['full_img'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['img']);
|
2025-07-08 09:59:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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') {
|
|
|
|
|
$params['filters'] = [];
|
2025-07-08 14:18:33 +08:00
|
|
|
$params['filters'][] = ['field' => 'adp_id', 'operator' => '=', 'value' => $params['adp_id']];
|
2025-07-08 09:59:28 +08:00
|
|
|
}
|
2025-07-08 15:38:43 +08:00
|
|
|
if ($scene === 'show') {
|
|
|
|
|
$params['filters'] = [];
|
|
|
|
|
$params['filters'][] = ['field' => 'id', 'operator' => '=', 'value' => $params['id']];
|
|
|
|
|
}
|
2025-07-08 09:59:28 +08:00
|
|
|
|
|
|
|
|
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) {
|
2025-07-08 15:38:43 +08:00
|
|
|
return $this->errorResponse($th->getMessage().$th->getLine().$th->getFile());
|
2025-07-08 09:59:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function add(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$request->setPost('creater', $request->user['id']);
|
|
|
|
|
$id = $this->getService()->add($request->all());
|
|
|
|
|
return $id ? $this->successResponse(['id' => $id])
|
|
|
|
|
: $this->errorResponse();
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(Request $request, int $id): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$request->setPost('id',$id);
|
|
|
|
|
$request->setPost('creater', $request->user['id']);
|
|
|
|
|
return $this->getService()->edit($request->all(), $id)
|
|
|
|
|
? $this->successResponse()
|
|
|
|
|
: $this->errorResponse();
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage().$th->getFile().$th->getLine());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-08 15:38:43 +08:00
|
|
|
|
|
|
|
|
public function detail(Request $request, int $id, string $scene = 'show'): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$request->setGet(['id'=>$id]);
|
|
|
|
|
return parent::one($request, $scene);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
return $this->errorResponse($th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-08 09:59:28 +08:00
|
|
|
}
|