|
|
@@ -0,0 +1,67 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Modules\Api\Controllers;
|
|
|
+
|
|
|
+use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
|
|
+use App\Constants\CommonConstant;
|
|
|
+use App\Model\PostsModel;
|
|
|
+use App\Repositories\Utils\M3u8CacheRepository;
|
|
|
+
|
|
|
+class PostsController extends BaseController
|
|
|
+{
|
|
|
+ public function lists(ValidatorFactoryInterface $validatorFactory)
|
|
|
+ {
|
|
|
+ $inputData = $this->getJsonData();
|
|
|
+ $rules = [
|
|
|
+ 'is_hot' => 'nullable|int',
|
|
|
+ 'page' => 'nullable|int',
|
|
|
+ 'page_size' => 'nullable|int',
|
|
|
+ ];
|
|
|
+
|
|
|
+ $validator = $validatorFactory->make($inputData, $rules);
|
|
|
+ if ($validator->fails()) {
|
|
|
+ // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
|
|
|
+ return json_fail($validator->errors()->first());
|
|
|
+ }
|
|
|
+
|
|
|
+ $page = $inputData['page'] ?? 1;
|
|
|
+ $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
|
|
|
+ if ($pageSize > 100) {
|
|
|
+ $pageSize = CommonConstant::PAGE_SIZE;
|
|
|
+ }
|
|
|
+ $page = format_page($page);
|
|
|
+ $pageSize = intval($pageSize);
|
|
|
+ if ($pageSize <= 0) {
|
|
|
+ $pageSize = CommonConstant::PAGE_SIZE;
|
|
|
+ }
|
|
|
+
|
|
|
+ $columns = ['uid','title', 'content', 'image', 'video_cover_url', 'views','created_at', 'id'];
|
|
|
+ $query = PostsModel::query()->where('status', 1);
|
|
|
+ if (isset($inputData['is_hot']) && $inputData['is_hot'] == 1) {
|
|
|
+ $query = $query->where('is_hot', 1)->orderByDesc('views');
|
|
|
+ }
|
|
|
+
|
|
|
+ $arrData = $query->orderByDesc('created_at')->paginate($pageSize, $columns, 'page', $page);
|
|
|
+
|
|
|
+ $imgInt = 3;
|
|
|
+ foreach($arrData->items() as &$v) {
|
|
|
+ $aImage = explode(',',$v->image);
|
|
|
+ $sImage = '';
|
|
|
+ if (!empty($v->video_cover_url)) {
|
|
|
+ $v->video_cover_url = M3u8CacheRepository::getVideoUrl($v['video_cover_url']);
|
|
|
+ $imgInt = 2;
|
|
|
+ }
|
|
|
+ $v->user->avatar = M3u8CacheRepository::getImageUrl($v->user->avatar);
|
|
|
+ $v['user'] = $v->user;
|
|
|
+ for ($i = 0; $i < $imgInt; $i++) {
|
|
|
+ $sImage = M3u8CacheRepository::getImageUrl($aImage[$i]);
|
|
|
+ }
|
|
|
+ $v->is_like = $v->likes($this->getUserId())->count();
|
|
|
+ $v->is_favorites = $v->favorites($this->getUserId())->count();
|
|
|
+ $v->comment_count = $v->comments()->count();
|
|
|
+ $v->content = truncateText($v->content, 30);
|
|
|
+ $v->image = $sImage;
|
|
|
+ }
|
|
|
+ return json_success($arrData);
|
|
|
+ }
|
|
|
+}
|