wesmiler 1 年之前
父節點
當前提交
7d567c195e
共有 3 個文件被更改,包括 123 次插入6 次删除
  1. 73 0
      app/Services/Api/MemberCollectService.php
  2. 2 0
      app/Services/Api/VideoService.php
  3. 48 6
      app/Services/LiveService.php

+ 73 - 0
app/Services/Api/MemberCollectService.php

@@ -188,9 +188,13 @@ class MemberCollectService extends BaseService
 
         $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id');
         $isFansId = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
+        $liveId = isset($params['live_id'])? intval($params['live_id']) : 0;
+        $videoId = isset($params['video_id'])? intval($params['video_id']) : 0;
         $data = [
             'user_id'=> $userId,
             'type'=> $type,
+            'live_id'=> $liveId,
+            'video_id'=> $videoId,
             'collect_uid'=> $collectUid,
             'update_time'=> time(),
             'is_return'=> $isFansId? ($status==1? 1 : 0) : 0, // 互关
@@ -210,13 +214,82 @@ class MemberCollectService extends BaseService
         }
 
         $this->error = 1002;
+        $sourceType = $liveId? 2 : 1;
+        $sourceId = $liveId? $liveId : $videoId;
         RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
+        RedisService::clear("caches:member:fans:{$userId}_{$sourceId}_{$sourceType}");
         $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId])->update(['is_return'=> $data['is_return'],'update_time'=>time()]);
 
         return ['is_fans'=>$isFansId? 1:0];
     }
 
     /**
+     * 获取播放/观看直播的粉丝数
+     * @param $userId 用户
+     * @param $sourceId 视频/直播ID
+     * @param int $sourceType 来源:1-视频,2-直播
+     * @return array|mixed
+     */
+    public function getViewFansCountByType($userId, $sourceId, $sourceType=1)
+    {
+        $cacheKey = "caches:member:fans:{$userId}_{$sourceId}_{$sourceType}";
+        $count = RedisService::get($cacheKey);
+        if($count || RedisService::exists($cacheKey)){
+            return $count;
+        }
+
+        $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
+        $count = $this->model->from('member_collect as a')
+            ->leftJoin('video_collect as b',function($join) use($sourceType){
+                $join->on('b.user_id','=','a.user_id')
+                    ->where(['b.source_type'=>$sourceType,'b.type'=>1,'b.status'=>1,'b.mark'=>1]);
+            })
+            ->where($where)
+            ->where('b.id','>', 0)
+            ->count('a.id');
+        if($count){
+            RedisService::set($cacheKey, $count, rand(5, 10));
+        }
+
+        return $count;
+    }
+
+    /**
+     * 获取新增的粉丝数
+     * @param $userId 用户
+     * @param $sourceId 视频/直播ID
+     * @param int $sourceType 来源:1-视频,2-直播
+     * @return array|mixed
+     */
+    public function getNewFansCount($userId, $sourceId, $sourceType=1, $time=0)
+    {
+        $cacheKey = "caches:member:newFans:{$userId}_{$sourceId}_{$sourceType}_{$time}";
+        $count = RedisService::get($cacheKey);
+        if($count || RedisService::exists($cacheKey)){
+            return $count;
+        }
+
+        $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
+        if($sourceType == 1){
+            // 视频
+            $where['video_id'] = $sourceId;
+        }else if($sourceType == 2){
+            // 直播
+            $where['live_id'] = $sourceId;
+        }
+
+        $count = $this->model->from('member_collect as a')
+            ->where($where)
+            ->where('a.create_time','>', $time)
+            ->count('a.id');
+        if($count){
+            RedisService::set($cacheKey, $count, rand(3, 5));
+        }
+
+        return $count;
+    }
+
+    /**
      * 获取(被)收藏/关注/点赞数量
      * @param $userId 用户ID
      * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢

+ 2 - 0
app/Services/Api/VideoService.php

@@ -328,6 +328,7 @@ class VideoService extends BaseService
                 VideoCollectModel::insert($data);
                 RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
                 RedisService::clear("caches:videos:recommend:{$userId}_1");
+                RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
             }
 
             // 浏览量
@@ -368,6 +369,7 @@ class VideoService extends BaseService
             VideoCollectModel::insert($data);
             RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
             RedisService::clear("caches:videos:recommend:{$userId}_1");
+            RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
         }
 
         // 浏览量

+ 48 - 6
app/Services/LiveService.php

@@ -199,6 +199,7 @@ class LiveService extends BaseService
                 ];
                 VideoCollectModel::insert($data);
                 RedisService::clear("caches:videos:recommend:{$userId}_2");
+                RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
             }
 
             // 更新播放量
@@ -250,18 +251,48 @@ class LiveService extends BaseService
             VideoCollectModel::insert($data);
             RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_2", $data, rand(10,30));
             RedisService::clear("caches:videos:recommend:{$userId}_1_2");
+            RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
         }
 
         // 更新播放量
-        if(!RedisService::get("caches:live:player:{$userId}_{$id}")){
+        if(!RedisService::get("caches:player:live:{$userId}_{$id}")){
             $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
-            RedisService::set("caches:live:player:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(600, 1800));
+            RedisService::set("caches:player:live:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(6*3600, 86400));
         }
         $this->error = 1010;
         return true;
     }
 
     /**
+     * 缓存数据
+     * @param $id
+     * @param int $status
+     * @return array|mixed
+     */
+    public function getCacheInfo($id, $status=0)
+    {
+        $cacheKey = "caches:live:info:{$id}_{$status}";
+        $info = RedisService::get($cacheKey);
+        if($info){
+            return $info;
+        }
+
+        $where = ['id' => $id, 'mark' => 1];
+        if($status){
+            $where['status'] = $status;
+        }
+        $info = $this->model->where($where)
+            ->select(['id','user_id','create_time','end_time','views','reward_num','reward_total','like_num'])
+            ->first();
+        $info = $info? $info->toArray() : [];
+        if($info){
+            RedisService::set($cacheKey, $info, rand(3, 5));
+        }
+
+        return $info;
+    }
+
+    /**
      * 状态设置
      * @return bool
      */
@@ -269,13 +300,24 @@ class LiveService extends BaseService
     {
         $id = request()->post('id', 0);
         $status = request()->post('status', 2);
-        if ($id && !$info = $this->model->where(['id' => $id, 'mark' => 1])->select(['id','views','reward_total','like_num'])->first()) {
+        if ($id && !$info = $this->getCacheInfo($id)) {
             $this->error = 2981;
             return false;
         }
-
-        if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
+        $updateData = ['status'=>$status, 'update_time'=> time()];
+        if($status == 2){
+            $updateData['end_time'] = time();
+        }
+        if($this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
             $this->error = 1002;
+            $time = $info['end_time']? intval($info['end_time'] - $info['create_time']) : intval(time() - $info['create_time']);
+            $info['status'] = $status;
+            $info['live_hour'] = $time > 3600 ? intval($time/3600) : 0;
+            $info['live_minute'] = $time%3600? intval($time%3600/60) : 0;
+            $info['time_text'] = date('H:i', $info['create_time']);
+            $info['end_time_text'] = date('H:i', $info['end_time']? $info['end_time'] : time());
+            $info['fans_num'] = MemberCollectService::make()->getViewFansCountByType($info['user_id'], $id, 2);
+            $info['new_fans'] = MemberCollectService::make()->getNewFansCount($info['user_id'], $id,2, $info['create_time']);
             return $info;
         }
 
@@ -372,7 +414,7 @@ class LiveService extends BaseService
             'type' => isset($params['type']) ? intval($params['type']) : 1,
             'user_id' => $userId,
             'title' => isset($params['title']) && $params['title'] ? trim($params['title']) : $nickname.'正在直播',
-            'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : '我正在直播,快来看看吧',
+            'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : lang('我正在直播,快来看看吧'),
             'category' => isset($params['category']) ? intval($params['category']) : 0,
             'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 0,
             'visible_users' => isset($params['visible_users']) ? trim($params['visible_users']) : '',