123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/6
- * Time: 18:05
- */
- namespace App\Traits;
- use App\Models\Post;
- use App\Models\PostComment;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- trait PostTrait
- {
- //预计可获得U米数
- public function availableBean()
- {
- $bean = Redis::get('yesterday_post_create_bean');
- $count = Redis::get('yesterday_post_count');
- $num = 1000;
- if($bean && $count){
- $num = $bean / $count;
- }
- $h = date('h');
- $H = date('H');
- $t = $h * 60 / 720 + 2;
- 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)
- {
- return Post::join('post_data', 'post_data.post_id', '=', 'post.id')
- ->select('post.*')
- ->find($id);
- }
- //获取内容话题
- public function getTopic($topic_ids)
- {
- $ids = explode(',', $topic_ids);
- $topic = [];
- foreach($ids as $id){
- $name = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
- if($name && isset($name[0])){
- $topic[] = [
- 'id' => intval($id),
- 'name' => $name[0],
- ];
- }
- }
- return $topic;
- }
- //获取内容详情
- public function getPostInfo($id, $type = 0)
- {
- if($type){
- $res = Redis::SISMEMBER('delete_post_ids', $id);
- if($res){
- return [];
- }
- }
- $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']);
- $data['available_bean'] = intval($data['available_bean']);
- $data['collect_bean'] = intval($data['collect_bean']);
- $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
- $data['imgs'] = json_decode($data['imgs'], true);
- }
- return $data;
- }
- //获取内容最新评论
- public function getNewComment($id, $uid = 0)
- {
- $comment = [];
- $key = 'comment_like_'.$id;
- $commentKey = 'post_new_comment_'.$id;
- $commentData = Redis::GET($commentKey);
- if($commentData){
- $comment = json_decode($commentData);
- foreach($comment as &$item){
- $isLike = 0;
- if($uid){
- $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
- }
- $item->is_like = $isLike;
- $item->like_count = Redis::ZCOUNT($key, $item->id, $item->id);
- }
- }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){
- $userComment = $this->userInfo($item->uid);
- $isLike = 0;
- if($uid){
- $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
- }
- $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
- $comment[] = [
- 'id' => $item->id,
- 'username' => $userComment['username'],
- 'content' => $item->is_delete?'该评论已被删除':$item->content,
- 'is_delete' => $item->is_delete,
- 'is_like' => $isLike,
- 'like_count' => $likeCount,
- ];
- }
- Redis::SET($commentKey, json_encode($comment));
- Redis::EXPIRE($commentKey, 3);
- }
- return $comment;
- }
- //获取评论点赞数、点赞状态
- public function getCommentLike($postId, $commentId, $uid = 0)
- {
- $key = 'comment_like_'.$postId;
- $isLike = 0;
- if($uid){
- $isLike = Redis::ZSCORE($key, $uid.'_'.$commentId) ? 1 : 0;
- }
- $likeCount = Redis::ZCOUNT($key, $commentId, $commentId);
- return [
- 'is_like' => $isLike,
- 'like_count' => $likeCount
- ];
- }
- }
|