LiveForm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\wxlive\models\client;
  8. use app\models\Level;
  9. use app\models\LiveRoomPurview;
  10. use app\models\User;
  11. use app\plugins\wxlive\models\Wechat;
  12. class LiveForm
  13. {
  14. /*
  15. * 直播列表缓存key
  16. */
  17. const LIVE_LIST_KEY = 'live_list';
  18. /**
  19. * 缓存直播间列表时间
  20. */
  21. const CACHE_TIME = 1800;
  22. /**
  23. * store_id
  24. */
  25. public $store_id;
  26. /*
  27. * 每页数量
  28. */
  29. public $limit = 20;
  30. /*
  31. * 分页
  32. */
  33. public $page = 1;
  34. public function getList()
  35. {
  36. $cacheKey = static::LIVE_LIST_KEY . '_' . $this->page . '_' . $this->store_id;
  37. $cache = cache()->get($cacheKey);
  38. if ($cache) {
  39. return $cache;
  40. }
  41. $start = ($this->page - 1) * $this->limit;
  42. $wechat = Wechat::getWechat();
  43. $res = $wechat->broadcast->getRooms($start, $this->limit);
  44. if ($res['errcode'] === 0) {
  45. $res['room_info'] = $this->eachList($res['room_info']);
  46. cache()->set(static::LIVE_LIST_KEY, $res, static::CACHE_TIME);
  47. }
  48. return $res;
  49. }
  50. /**
  51. * 处理数据
  52. * @param array $list
  53. * @return array
  54. */
  55. protected function eachList(array $list)
  56. {
  57. foreach ($list as $key => &$value) {
  58. /* id958新增需求:增加直播间可见范围 */
  59. $purview = LiveRoomPurview::findOne(['store_id' => $this->store_id, 'room_id' => $value['roomid']]);
  60. $user_info = User::findOne(get_user_id());
  61. if($purview->type == 1) {//指定用户
  62. if(!in_array($user_info->id ,explode(',', $purview->user_id))) {
  63. unset($list[$key]);
  64. continue;
  65. }
  66. } elseif($purview->type == 2) {//指定会员等级
  67. $level_info = Level::findOne(['level' => $user_info->level, 'store_id' => $this->store_id, 'is_delete' => 0]);
  68. if(!in_array($level_info->id ,explode(',', $purview->level_id))) {
  69. unset($list[$key]);
  70. continue;
  71. }
  72. }
  73. $list = array_values($list);
  74. /* end */
  75. if ($value['live_status'] == 103) {
  76. $value['time'] = $this->formatDateByInterval($value['start_time'], $value['end_time']);
  77. $value['time2'] = $this->formatDate($value['start_time'], $value['end_time']);
  78. } else {
  79. $value['time'] = $this->formatDate($value['start_time'], $value['end_time']);
  80. $value['time2'] = $this->formatDateByInterval($value['start_time'], $value['end_time']);
  81. }
  82. foreach ($value['goods'] as $k => &$v) {
  83. $v['price'] = $v['price'] / 100;
  84. }
  85. }
  86. return $list;
  87. }
  88. /**
  89. * 格式化时间差
  90. * @param $start
  91. * @param $end
  92. * @return string
  93. */
  94. protected function formatDateByInterval($start, $end)
  95. {
  96. $start = date_create(date('Y-m-d H:i:s', $start));
  97. $end = date_create(date('Y-m-d H:i:s', $end));
  98. $interval = date_diff($start, $end);
  99. $res = $interval->format('%h');
  100. if ($res > 0) {
  101. return $res . ' 小时';
  102. }
  103. $res = $interval->format('%i');
  104. if ($res > 0) {
  105. return $res . ' 分钟';
  106. }
  107. $res = $interval->format('%s');
  108. return $res . ' 秒';
  109. }
  110. /**
  111. * 格式化时间
  112. * @param $start
  113. * @param $end
  114. * @return string
  115. */
  116. protected function formatDate($start, $end)
  117. {
  118. $start = date('y/m/d H:i', $start);
  119. $end = date('H:i', $end);
  120. return $start . ' - ' . $end;
  121. }
  122. }