瀏覽代碼

Merge branch 'develop'

xielin 5 年之前
父節點
當前提交
aeee446d60
共有 2 個文件被更改,包括 41 次插入22 次删除
  1. 16 0
      app/Helper/helper.php
  2. 25 22
      app/Traits/PostTrait.php

+ 16 - 0
app/Helper/helper.php

@@ -209,4 +209,20 @@ function getClientIp()
     }
 
     return $ip;
+}
+
+/**
+ * 多维数组按单字段排序
+ * @param $array
+ * @param $field
+ * @param bool $desc
+ */
+function sortArrByField(&$array, $field, $desc = false)
+{
+    $fieldArr = array();
+    foreach ($array as $k => $v) {
+        $fieldArr[$k] = $v[$field];
+    }
+    $sort = $desc == false ? SORT_ASC : SORT_DESC;
+    array_multisort($fieldArr, $sort, $array);
 }

+ 25 - 22
app/Traits/PostTrait.php

@@ -6,6 +6,7 @@
  * Date: 2019/6/6
  * Time: 18:05
  */
+
 namespace App\Traits;
 
 use App\Models\Post;
@@ -21,7 +22,7 @@ trait PostTrait
         $bean = Redis::get('yesterday_post_create_bean');
         $count = Redis::get('yesterday_post_count');
         $num = 1000;
-        if($bean && $count){
+        if ($bean && $count) {
             $num = $bean / $count;
         }
         $h = date('h');
@@ -29,11 +30,12 @@ trait PostTrait
         $t = $h * 60 / 720 + 2;
 
 
-        if(in_array(intval($H), [9,10,11,12,17.18,19,20,21])){
+        if (in_array(intval($H), [9, 10, 11, 12, 17.18, 19, 20, 21])) {
             $t += 0.5;
         }
         return intval($num * $t);
     }
+
     //详情
     public function detail($id)
     {
@@ -47,9 +49,9 @@ trait PostTrait
     {
         $ids = explode(',', $topic_ids);
         $topic = [];
-        foreach($ids as $id){
+        foreach ($ids as $id) {
             $name = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
-            if($name && isset($name[0])){
+            if ($name && isset($name[0])) {
                 $topic[] = [
                     'id' => intval($id),
                     'name' => $name[0],
@@ -62,14 +64,14 @@ trait PostTrait
     //获取内容详情
     public function getPostInfo($id, $type = 0)
     {
-        if($type){
+        if ($type) {
             $res = Redis::SISMEMBER('delete_post_ids', $id);
-            if($res){
+            if ($res) {
                 return [];
             }
         }
-        $data =  Redis::HGETALL('post_info_'.$id);
-        if($data){
+        $data = Redis::HGETALL('post_info_' . $id);
+        if ($data) {
             $data['praise_count'] = intval($data['praise_count']);
             $data['comment_count'] = intval($data['comment_count']);
             $data['collect_count'] = intval($data['collect_count']);
@@ -85,15 +87,15 @@ trait PostTrait
     public function getNewComment($id, $uid = 0)
     {
         $comment = [];
-        $key = 'comment_like_'.$id;
-        $commentKey = 'post_new_comment_'.$id;
+        $key = 'comment_like_' . $id;
+        $commentKey = 'post_new_comment_' . $id;
         $commentData = Redis::GET($commentKey);
-        if($commentData){
+        if ($commentData) {
             $comment = json_decode($commentData);
-            foreach($comment as &$item){
+            foreach ($comment as &$item) {
                 $isLike = 0;
-                if($uid){
-                    $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
+                if ($uid) {
+                    $isLike = Redis::ZSCORE($key, $uid . '_' . $item->id) ? 1 : 0;
                 }
                 $item->uid = intval($item->uid);
                 $item->is_like = $isLike;
@@ -101,20 +103,20 @@ trait PostTrait
                 $item->reply_count = 0;
                 $item->reply = [];
             }
-        }else{
+        } else {
             $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('like_count', 'desc')->orderBy('id', 'desc')->limit(2)->get();
-            foreach($comments as $item){
+            foreach ($comments as $item) {
                 $userComment = $this->userInfo($item->uid);
                 $isLike = 0;
-                if($uid){
-                    $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
+                if ($uid) {
+                    $isLike = Redis::ZSCORE($key, $uid . '_' . $item->id) ? 1 : 0;
                 }
                 $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
                 $comment[] = [
                     'id' => $item->id,
                     'uid' => intval($userComment['uid']),
                     'username' => $userComment['username'],
-                    'content' => $item->is_delete?'该评论已被删除':$item->content,
+                    'content' => $item->is_delete ? '该评论已被删除' : $item->content,
                     'is_delete' => $item->is_delete,
                     'is_like' => $isLike,
                     'like_count' => $likeCount,
@@ -125,16 +127,17 @@ trait PostTrait
             Redis::SET($commentKey, json_encode($comment));
             Redis::EXPIRE($commentKey, 300);
         }
+        sortArrByField($comment, 'like_count', true);
         return $comment;
     }
 
     //获取评论点赞数、点赞状态
     public function getCommentLike($postId, $commentId, $uid = 0)
     {
-        $key = 'comment_like_'.$postId;
+        $key = 'comment_like_' . $postId;
         $isLike = 0;
-        if($uid){
-            $isLike = Redis::ZSCORE($key, $uid.'_'.$commentId) ? 1 : 0;
+        if ($uid) {
+            $isLike = Redis::ZSCORE($key, $uid . '_' . $commentId) ? 1 : 0;
         }
         $likeCount = Redis::ZCOUNT($key, $commentId, $commentId);
         return [