浏览代码

用户昵称、头像

wzq 5 年之前
父节点
当前提交
d64e4014f7

+ 1 - 4
app/Repositories/FeedRepositories.php

@@ -164,10 +164,7 @@ class FeedRepositories
                 }
                 }
                 if ($value['type'] == 5) {
                 if ($value['type'] == 5) {
                     $value['content'] = null;
                     $value['content'] = null;
-                    $user = Redis::HGETALL('userInfo:' . $value['relate_id']);
-                    if (!$user) {
-                        $user = $this->userInfo($value['relate_id']);
-                    }
+                    $user = $this->userInfo($value['relate_id']);
                     if ($user) {
                     if ($user) {
                         Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}");
                         Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}");
                         $value['relate_data'] = [
                         $value['relate_data'] = [

+ 8 - 2
app/Repositories/PostRepositories.php

@@ -617,11 +617,17 @@ class PostRepositories
      */
      */
     public function getPostVideo($ids)
     public function getPostVideo($ids)
     {
     {
-        return $this->post
-            ->select('id', 'img', 'uid', 'username', 'avatar')
+        $posts = $this->post
+            ->select('id', 'img', 'uid')
             ->whereIn('id', explode(',',$ids))
             ->whereIn('id', explode(',',$ids))
             ->where('type', 'video')
             ->where('type', 'video')
             ->get();
             ->get();
+        foreach($posts as &$post){
+            $user = $this->userInfo($post->uid);
+            $post->username = $user['username'];
+            $post->avatar = $user['avatar'];
+        }
+        return $posts;
     }
     }
     //用户内容数,转发数,收藏数统计
     //用户内容数,转发数,收藏数统计
     public function memberPostStatistics($uid){
     public function memberPostStatistics($uid){

+ 21 - 9
app/Traits/UserTrait.php

@@ -30,17 +30,29 @@ trait UserTrait
 
 
     //获取用户信息
     //获取用户信息
     public function userInfo($uid) {
     public function userInfo($uid) {
-        try {
-            $sign = generateSign(['uid' => $uid], config('customer.app_secret'));
-            $url = config("customer.app_service_url").'/user/getUserInfo';
-            $array = [
-                'json' => ['sign' => $sign,'uid' => $uid], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+        $user = Redis::HGETALL('userInfo:' . $uid);
+        if(!$user){
+            try {
+                $sign = generateSign(['uid' => $uid], config('customer.app_secret'));
+                $url = config("customer.app_service_url").'/user/getUserInfo';
+                $array = [
+                    'json' => ['sign' => $sign,'uid' => $uid], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+                ];
+                $user =  http($url,$array,'get');
+            } catch (\Exception $e) {
+                $user =  [];
+            }
+        }
+        if(!$user){
+            $user = [
+                'uid' => $uid,
+                'username' => '老板',
+                'avatar' => 'http://thirdwx.qlogo.cn/mmopen/vi_32/xib9XJbYEbrbYolcMteJnibzVWuLWZdV15icPQehibHv2DLRlqyxureia73m7YFrTXYfUVlNvQoBbrkZWBAribXCCiaTg/132',
+                'gender' => 1,
+                'invite_code' => ''
             ];
             ];
-            return http($url,$array,'get');
-        } catch (\Exception $e) {
-            return [];
         }
         }
-
+        return $user;
     }
     }
 
 
     //获取关注状态
     //获取关注状态

+ 14 - 6
app/Transformers/Post/CommentTransformer.php

@@ -8,31 +8,39 @@
 namespace  App\Transformers\Post;
 namespace  App\Transformers\Post;
 
 
 use App\Models\PostComment;
 use App\Models\PostComment;
+use App\Traits\UserTrait;
 use Carbon\Carbon;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
 use League\Fractal\TransformerAbstract;
 
 
 class CommentTransformer extends TransformerAbstract
 class CommentTransformer extends TransformerAbstract
 {
 {
-    
+    use UserTrait;
     public function transform(PostComment $postComment)
     public function transform(PostComment $postComment)
     {
     {
         $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
         $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
         $reply = [];
         $reply = [];
         foreach($replies as $val){
         foreach($replies as $val){
+            $userComment = $this->userInfo($val->uid);
+            $replyUsername = '';
+            if($val->reply_uid){
+                $userReply = $this->userInfo($val->reply_uid);
+                $replyUsername = $userReply['username'];
+            }
             $reply[] = [
             $reply[] = [
                 'uid' => $val->uid,
                 'uid' => $val->uid,
-                'username' => $val->username,
-                'avatar' => $val->avatar,
-                'reply_username' => $val->reply_username,
+                'username' => $userComment['username'],
+                'avatar' => $userComment['avatar'],
+                'reply_username' => $replyUsername,
                 'content' => $val->is_delete?'该评论已被删除':$val->content,
                 'content' => $val->is_delete?'该评论已被删除':$val->content,
                 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
                 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
             ];
             ];
         }
         }
+        $user = $this->userInfo($postComment['uid']);
         return [
         return [
             'id' => $postComment['id'],
             'id' => $postComment['id'],
             'uid' => $postComment['uid'],
             'uid' => $postComment['uid'],
-            'username' => $postComment['username'],
-            'avatar' => $postComment['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
             'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'reply_count' => $postComment->reply->count(),
             'reply_count' => $postComment->reply->count(),

+ 3 - 2
app/Transformers/Post/DetailTransformer.php

@@ -52,13 +52,14 @@ class DetailTransformer extends TransformerAbstract
                 $isFollow = $followStatus;
                 $isFollow = $followStatus;
             }
             }
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'topic' => $topic,
             'topic' => $topic,
             'topic_ids' => $post['topic_ids'],
             'topic_ids' => $post['topic_ids'],
             'title' => $post['title'],
             'title' => $post['title'],

+ 5 - 2
app/Transformers/Post/ListTransformer.php

@@ -9,10 +9,12 @@ namespace  App\Transformers\Post;
 
 
 use App\Models\Post;
 use App\Models\Post;
 use App\Models\PostLike;
 use App\Models\PostLike;
+use App\Traits\UserTrait;
 use League\Fractal\TransformerAbstract;
 use League\Fractal\TransformerAbstract;
 
 
 class ListTransformer extends TransformerAbstract
 class ListTransformer extends TransformerAbstract
 {
 {
+    use UserTrait;
     public function __construct($uid, $invite_code)
     public function __construct($uid, $invite_code)
     {
     {
         $this->uid = $uid;
         $this->uid = $uid;
@@ -24,12 +26,13 @@ class ListTransformer extends TransformerAbstract
         if($this->uid){
         if($this->uid){
             $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
             $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'title' => $post['title'],
             'title' => $post['title'],
             'content' => subtext($post['content'], 100),
             'content' => subtext($post['content'], 100),
             'img' => $post['img'],
             'img' => $post['img'],

+ 4 - 2
app/Transformers/Post/PostTransformer.php

@@ -18,6 +18,7 @@ use League\Fractal\TransformerAbstract;
 
 
 class PostTransformer extends TransformerAbstract
 class PostTransformer extends TransformerAbstract
 {
 {
+    use UserTrait;
     public function __construct()
     public function __construct()
     {
     {
     }
     }
@@ -35,13 +36,14 @@ class PostTransformer extends TransformerAbstract
                 'name' => $val
                 'name' => $val
             ];
             ];
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'created_at' => $post['created_at'],
             'created_at' => $post['created_at'],
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'topic' => $topic,
             'topic' => $topic,
             'title' => $post['title'],
             'title' => $post['title'],
             'content' => $post['content'],
             'content' => $post['content'],

+ 11 - 4
app/Transformers/Post/ReplyTransformer.php

@@ -8,20 +8,27 @@
 namespace  App\Transformers\Post;
 namespace  App\Transformers\Post;
 
 
 use App\Models\PostComment;
 use App\Models\PostComment;
+use App\Traits\UserTrait;
 use Carbon\Carbon;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
 use League\Fractal\TransformerAbstract;
 
 
 class ReplyTransformer extends TransformerAbstract
 class ReplyTransformer extends TransformerAbstract
 {
 {
-
+    use UserTrait;
     public function transform(PostComment $postComment)
     public function transform(PostComment $postComment)
     {
     {
+        $user = $this->userInfo($postComment['uid']);
+        $replyUsername = '';
+        if($postComment['reply_uid']){
+            $userReply = $this->userInfo($postComment['reply_uid']);
+            $replyUsername = $userReply['username'];
+        }
         return [
         return [
             'id' => $postComment['id'],
             'id' => $postComment['id'],
             'uid' => $postComment['uid'],
             'uid' => $postComment['uid'],
-            'username' => $postComment['username'],
-            'reply_username' => $postComment['reply_username'],
-            'avatar' => $postComment['avatar'],
+            'username' => $user['username'],
+            'reply_username' => $replyUsername,
+            'avatar' => $user['avatar'],
             'content' => $postComment['is_delete']?'该回复已被删除':$postComment['content'],
             'content' => $postComment['is_delete']?'该回复已被删除':$postComment['content'],
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
         ];
         ];

+ 5 - 3
app/Transformers/Post/SuggestTransformer.php

@@ -34,9 +34,10 @@ class SuggestTransformer extends TransformerAbstract
         $comment = [];
         $comment = [];
         $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
         $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
         foreach($comments as $item){
         foreach($comments as $item){
+            $userComment = $this->userInfo($item->uid);
             $comment[] = [
             $comment[] = [
                 'id' => $item->id,
                 'id' => $item->id,
-                'username' => $item->username,
+                'username' => $userComment['username'],
                 'content' => $item->is_delete?'该评论已被删除':$item->content,
                 'content' => $item->is_delete?'该评论已被删除':$item->content,
             ];
             ];
         }
         }
@@ -55,14 +56,15 @@ class SuggestTransformer extends TransformerAbstract
             $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
             $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
             $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
             $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'show_type' => 'post',
             'show_type' => 'post',
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'topic' => $topic,
             'topic' => $topic,
             'title' => $post['title'],
             'title' => $post['title'],
             'content' => $post['content'],
             'content' => $post['content'],

+ 3 - 2
app/Transformers/Post/VideoTransformer.php

@@ -41,12 +41,13 @@ class VideoTransformer extends TransformerAbstract
                 'name' => $val
                 'name' => $val
             ];
             ];
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'title' => $post['title'],
             'title' => $post['title'],
             'content' => $post['content'],
             'content' => $post['content'],
             'img' => $post['img'],
             'img' => $post['img'],

+ 13 - 5
app/Transformers/Topic/TopicPostTransformer.php

@@ -37,15 +37,22 @@ class TopicPostTransformer extends TransformerAbstract
             $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
             $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
             $reply = [];
             $reply = [];
             foreach($replies as $val){
             foreach($replies as $val){
+                $userComment = $this->userInfo($val->uid);
+                $replyUsername = '';
+                if($val->reply_uid){
+                    $userReply = $this->userInfo($val->reply_uid);
+                    $replyUsername = $userReply['username'];
+                }
                 $reply[] = [
                 $reply[] = [
-                    'username' => $val->username,
-                    'reply_username' => $val->reply_username,
+                    'username' => $userComment['username'],
+                    'reply_username' => $replyUsername,
                     'content' => $val->content,
                     'content' => $val->content,
                 ];
                 ];
             }
             }
+            $userComment = $this->userInfo($item->uid);
             $comment[] = [
             $comment[] = [
                 'id' => $item->id,
                 'id' => $item->id,
-                'username' => $item->username,
+                'username' => $userComment['username'],
                 'content' => $item->content,
                 'content' => $item->content,
                 'reply_count' => $replyCount,
                 'reply_count' => $replyCount,
                 'reply' => $reply,
                 'reply' => $reply,
@@ -71,13 +78,14 @@ class TopicPostTransformer extends TransformerAbstract
                 $isFollow = $followStatus;
                 $isFollow = $followStatus;
             }
             }
         }
         }
+        $user = $this->userInfo($post['uid']);
         return [
         return [
             'id' => $post['id'],
             'id' => $post['id'],
             'type' => $post['type'],
             'type' => $post['type'],
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'uid' => $post['uid'],
             'uid' => $post['uid'],
-            'username' => $post['username'],
-            'avatar' => $post['avatar'],
+            'username' => $user['username'],
+            'avatar' => $user['avatar'],
             'topic' => $topic,
             'topic' => $topic,
             'title' => $post['title'],
             'title' => $post['title'],
             'content' => $post['content'],
             'content' => $post['content'],