Browse Source

视频列表

wzq 5 years ago
parent
commit
e8a18f653d
2 changed files with 42 additions and 0 deletions
  1. 30 0
      app/Repositories/PostRepositories.php
  2. 12 0
      app/Traits/CmsTrait.php

+ 30 - 0
app/Repositories/PostRepositories.php

@@ -21,6 +21,7 @@ use App\Models\PostShare;
 use App\Models\Topic;
 use App\Service\AliYunVodService;
 use App\Service\DetectionService;
+use App\Traits\CmsTrait;
 use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Illuminate\Database\QueryException;
@@ -35,6 +36,7 @@ class PostRepositories
 {
     use UserTrait;
     use PostTrait;
+    use CmsTrait;
     public function __construct(Post $post,
                                 PostData $postData,
                                 PostImgs $postImgs,
@@ -319,6 +321,34 @@ class PostRepositories
             $id = $request['id'];
         }
         $where[] = ['type', 'video'];
+        if(isset($request['type'])){
+            if($request['type'] == 'hot'){
+                $ids = Redis::get('hotVideoIds');
+                if(!$ids){
+                    $ids = $this->hotVideoIds();
+                    if(!$ids){
+                        $ids = '';
+                    }
+                }
+                Log::debug('热门视频ids'.$ids);
+                return $this->post
+                    ->join('post_data', 'post_data.post_id', '=', 'post.id')
+                    ->select('post.*', DB::raw("IF (post.id = {$id},1,0) as sort"))
+                    ->where($where)
+                    ->whereIn('post.id', explode(',', $ids))
+                    ->orderBy('sort', 'desc')
+                    ->orderByRaw(DB::raw("FIELD(post.id,{$ids})"))
+                    ->paginate($perPage);
+            }elseif($request['type'] == 'one'){
+                $where[] = ['post.id', $id];
+                return $this->post
+                    ->join('post_data', 'post_data.post_id', '=', 'post.id')
+                    ->select('post.*')
+                    ->where($where)
+                    ->paginate($perPage);
+            }
+
+        }
         return $this->post
             ->join('post_data', 'post_data.post_id', '=', 'post.id')
             ->select('post.*', DB::raw("IF (post.id = {$id},1,0) as sort"))

+ 12 - 0
app/Traits/CmsTrait.php

@@ -22,6 +22,18 @@ trait CmsTrait
         } catch (\Exception $e) {
             return [];
         }
+    }
 
+    public function hotVideoIds() {
+        try {
+            $sign = generateSign([], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/cms/v2/floor/hotVideoIds';
+            $array = [
+                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array,'get');
+        } catch (\Exception $e) {
+            return [];
+        }
     }
 }