140 lines
4.2 KiB
PHP
140 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace app\console\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use app\console\model\ArticleModel;
|
|
use app\console\service\ArticleService;
|
|
use app\console\validate\ArticleValidator;
|
|
use app\mzl\common\controller\BaseController;
|
|
|
|
class ArticleController extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 不需要验证token的接口
|
|
* @var array
|
|
*/
|
|
protected $noNeedJwt = [];
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected mixed $validator = ArticleValidator::class;
|
|
|
|
/**
|
|
* 返回业务 Service 实例
|
|
*/
|
|
protected function getService()
|
|
{
|
|
return new ArticleService(new ArticleModel());
|
|
}
|
|
|
|
/**
|
|
* 额外字段处理 (根据场景处理)
|
|
* @param array $item
|
|
* @param string $scene
|
|
* @return array
|
|
*/
|
|
protected function postProcessItem(array $item, string $scene): array
|
|
{
|
|
if ($scene === 'default' || $scene === 'show') {
|
|
$item['publish_time'] = date('Y-m-d H:i:s', $item['publish_time']);
|
|
|
|
$item['cover_full'] = '';
|
|
if ($item['cover']) {
|
|
$item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']);
|
|
}
|
|
foreach ($item['comment'] as &$comment) {
|
|
|
|
if ($comment['avatar']) {
|
|
$comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']);
|
|
}
|
|
}
|
|
unset($comment);
|
|
}
|
|
|
|
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'] = [];
|
|
// $params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1];
|
|
$params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()];
|
|
// $params['filters'][] = ['field' => 'publish_time', 'operator' => '<', 'value' => time()];
|
|
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
public function add(Request $request): Response
|
|
{
|
|
try {
|
|
$request->setPost('user_id', $request->user['id']);
|
|
|
|
$protocol = $request->header('x-forwarded-proto',
|
|
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http');
|
|
$domain = $protocol . '://' . $request->host();
|
|
$request->setPost('domain', $domain);
|
|
return $this->getService()->add($request->all())
|
|
? $this->successResponse()
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(Request $request, int $id): Response
|
|
{
|
|
$request->setPost('user_id', $request->user['id']);
|
|
|
|
$protocol = $request->header('x-forwarded-proto',
|
|
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http');
|
|
$domain = $protocol . '://' . $request->host();
|
|
$request->setPost('domain', $domain);
|
|
try {
|
|
return $this->getService()->edit($request->all(), $id)
|
|
? $this->successResponse()
|
|
: $this->errorResponse();
|
|
} catch (\Throwable $th) {
|
|
return $this->errorResponse($th->getMessage());
|
|
}
|
|
}
|
|
} |