Browse Source

Merge branch 'develop' of http://git.caihongxingqiu.net/rainbow/community-service into develop

xielin 5 years ago
parent
commit
51c18aaaaa

+ 5 - 0
app/Repositories/BeanRepository.php

@@ -2,6 +2,7 @@
 namespace App\Repositories;
 use App\Models\Post;
 use App\Traits\PostTrait;
+use App\Traits\UserTrait;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Log;
@@ -10,6 +11,7 @@ use Illuminate\Support\Facades\Redis;
 class BeanRepository
 {
     use PostTrait;
+    use UserTrait;
     public function beanDetail($request)
     {
         try {
@@ -249,9 +251,12 @@ class BeanRepository
         if (isset($request['post_id']) && $request['post_id']){
             $post = $this->getPostInfo($request['post_id'], 1);
             if ($post){
+                $user = $this->userInfo($post['uid']);
                 $topic = $this->getTopic($post['topic_ids']);
                 $post['created_time'] = Carbon::parse($post['created_at'])->diffForHumans();
                 $post['topic'] = array_column($topic, 'name');
+                $post['username'] = $user['username'];
+                $post['avatar'] = $user['avatar'];
                 $star_detail['post'] = $post;
             }else{
                 Log::debug($request['post_id']);

+ 28 - 8
app/Repositories/MemberFollowTopicRepository.php

@@ -9,6 +9,7 @@
 namespace App\Repositories;
 
 use App\Models\MemberFollowTopic;
+use App\Models\Topic;
 use Illuminate\Database\QueryException;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
@@ -18,8 +19,11 @@ use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Log;
 
 class MemberFollowTopicRepository {
-    public function __construct(MemberFollowTopic $memberFollowTopic,CategoryRepository $categoryRepository) {
+    public function __construct(MemberFollowTopic $memberFollowTopic,
+                                Topic $topic,
+                                CategoryRepository $categoryRepository) {
         $this->memberFollowTopic = $memberFollowTopic;
+        $this->topic = $topic;
         $this->categoryRepository = $categoryRepository;
     }
     //关注话题
@@ -31,7 +35,9 @@ class MemberFollowTopicRepository {
         if($data){
             $category_topic_data = [];
             $date = Carbon::now()->toDateTimeString();
+            $topicIds = [];
             foreach($data as $value){
+                $topicIds[] =  $value['id'];
                 $category_topic_data[] = [
                     'uid' => $token['user']->uid,
                     'topic_id' => $value['id'],
@@ -42,6 +48,7 @@ class MemberFollowTopicRepository {
             DB::beginTransaction();
             try{
                 $res = $this->memberFollowTopic->insert($category_topic_data);
+                $this->topic->whereIn('id', $topicIds)->increment('follow_count');
                 $topic = $this->updataMemberFollowSuggestTopic();
                 DB::commit();
                 if($res){
@@ -88,14 +95,20 @@ class MemberFollowTopicRepository {
             return jsonError('您已关注该话题');
         }
         $data = ['uid'=>$token['user']->uid,'topic_id'=>$topic_id];
-        $res = $this->memberFollowTopic->create($data);
-        if($res){
+
+        DB::beginTransaction();
+        try{
+            $this->memberFollowTopic->create($data);
+            $this->topic->where('id', $topic_id)->increment('follow_count');
+            DB::commit();
             $key = 'topic.user_uid'.$token['user']->uid;
             if(!Redis::zscore($key, $topic_id)){
                 Redis::zincrby($key, 0, $topic_id);
             }
             return jsonSuccess();
-        }else{
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::error('关注失败:'.$exception->getMessage());
             return jsonError('关注失败');
         }
     }
@@ -106,11 +119,18 @@ class MemberFollowTopicRepository {
         if(!$info){
             return jsonError('您没有关注该话题');
         }
-        $res = $this->memberFollowTopic->where('id',$info->id)->delete();
-        if($res){
+
+        DB::beginTransaction();
+        try{
+            $this->memberFollowTopic->where('id',$info->id)->delete();
+            $this->topic->where('id', $topic_id)->decrement('follow_count');
+            DB::commit();
+            
             return jsonSuccess();
-        }else{
-            return jsonError('取关失败');
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::error('取消关注失败:'.$exception->getMessage());
+            return jsonError('取消关注失败');
         }
     }
     public function list($request){

+ 10 - 1
app/Repositories/PostRepositories.php

@@ -196,6 +196,7 @@ class PostRepositories
             Redis::zadd('post_trigger_type', $isValid, $post->id);
             foreach($topicIds as $id){
                 Redis::zincrby('topic.user_uid'.$userInfo['uid'], 1, $id);
+                Redis::zincrby('topic.just_use_count', 1, $id);
             }
             Redis::HSET('post_info_'.$post->id,
                 'id', $post->id,
@@ -610,6 +611,14 @@ class PostRepositories
             $post->pv += 1;
             $post->pv_real += 1;
             Redis::HINCRBY($postInfoKey,'pv',1);
+            $topicIdStr = Redis::HGET($postInfoKey,'topic_ids');
+            if($topicIdStr){
+                Log::debug('话题增加浏览量'.$topicIdStr);
+                $topicIds = explode(',', $topicIdStr);
+                foreach($topicIds as $id){
+                    $this->topic->where('id', $id)->increment('pv');
+                }
+            }
             Log::debug("帖子:".$postId."被阅读,pv +1");
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
             //用户不喜欢帖子key
@@ -712,7 +721,7 @@ class PostRepositories
                 'id' => $topic->id,
                 'name' => $topic->name,
                 'img' => $topic->img,
-                'follow_count' => getNumber($topic->follow->count() + $topic->base_count),
+                'follow_count' => getNumber($topic->use_count + $topic->base_count),
             ];
         }
         return $data;

+ 1 - 1
app/Transformers/Topic/MyTopicListTransformer.php

@@ -18,7 +18,7 @@ class MyTopicListTransformer extends TransformerAbstract
             'id' => $memberFollowTopic['topic_id'],
             'name' => $memberFollowTopic->topic->name,
             'img' => $memberFollowTopic->topic->img,
-            'follow_count' => getNumber($memberFollowTopic->topic->follow->count() + $memberFollowTopic->topic->base_count),
+            'follow_count' => getNumber($memberFollowTopic->topic->use_count + $memberFollowTopic->topic->base_count),
         ];
     }
 }

+ 1 - 1
app/Transformers/Topic/TopicDetailTransformer.php

@@ -27,7 +27,7 @@ class TopicDetailTransformer extends TransformerAbstract
             'id' => $topic['id'],
             'name' => $topic['name'],
             'img' => $topic['img'],
-            'follow_count' => getNumber($topic->follow->count() + $topic['base_count']),
+            'follow_count' => getNumber($topic['use_count'] + $topic['base_count']),
             'is_follow' => $isFollow,
         ];
     }

+ 1 - 1
app/Transformers/Topic/TopicListTransformer.php

@@ -18,7 +18,7 @@ class TopicListTransformer extends TransformerAbstract
             'id' => $topic['id'],
             'name' => $topic['name'],
             'img' => $topic['img'],
-            'follow_count' => getNumber($topic->follow->count() + $topic['base_count']),
+            'follow_count' => getNumber($topic['use_count'] + $topic['base_count']),
         ];
     }
 }