120 lines
3.4 KiB
PHP
120 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace app\apis\controller;
|
|
|
|
use app\apis\model\CommentModel;
|
|
use support\Request;
|
|
use support\Response;
|
|
use app\apis\model\ArticleModel;
|
|
use app\apis\service\ArticleService;
|
|
use app\apis\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' || $scene === 'home') {
|
|
$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']);
|
|
}
|
|
$item['comment'] = CommentModel::where(['article_id' => $item['id'], ['created_at', '<=', time()]])->get();
|
|
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' || $scene === 'home') {
|
|
$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 home(Request $request, string $scene = 'home'): Response
|
|
{
|
|
try {
|
|
$result = parent::index($request, $scene);
|
|
$result = json_decode($result->rawBody(), true);
|
|
return $this->successResponse($result['data']['list']);
|
|
} 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());
|
|
}
|
|
}
|
|
} |