PostTrait.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/6
  6. * Time: 18:05
  7. */
  8. namespace App\Traits;
  9. use App\Models\Post;
  10. use Illuminate\Support\Facades\Redis;
  11. trait PostTrait
  12. {
  13. //预计可获得U米数
  14. public function availableBean()
  15. {
  16. $bean = Redis::get('yesterday_post_create_bean');
  17. $count = Redis::get('yesterday_post_count');
  18. $num = 1000;
  19. if($bean && $count){
  20. $num = $bean / $count;
  21. }
  22. $h = date('h');
  23. $H = date('H');
  24. $t = $h * 60 / 720 + 2;
  25. if(in_array(intval($H), [9,10,11,12,17.18,19,20,21])){
  26. $t += 0.5;
  27. }
  28. return intval($num * $t);
  29. }
  30. //详情
  31. public function detail($id)
  32. {
  33. return Post::join('post_data', 'post_data.post_id', '=', 'post.id')
  34. ->select('post.*')
  35. ->find($id);
  36. }
  37. //获取内容话题
  38. public function getTopic($topic_ids)
  39. {
  40. $ids = explode(',', $topic_ids);
  41. $topic = [];
  42. foreach($ids as $id){
  43. $name = $topicNameArray = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
  44. if($name && isset($name[0])){
  45. $topic[] = [
  46. 'id' => intval($id),
  47. 'name' => $name[0],
  48. ];
  49. }
  50. }
  51. return $topic;
  52. }
  53. //获取内容详情
  54. public function getPostInfo($id)
  55. {
  56. $data = Redis::HGETALL('post_info_'.$id);
  57. if($data){
  58. $data['praise_count'] = intval($data['praise_count']);
  59. $data['comment_count'] = intval($data['comment_count']);
  60. $data['collect_count'] = intval($data['collect_count']);
  61. $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
  62. $data['imgs'] = json_decode($data['imgs'], true);
  63. }
  64. return $data;
  65. }
  66. }