| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\wxlive\models\client;
- use app\models\Level;
- use app\models\LiveRoomPurview;
- use app\models\User;
- use app\plugins\wxlive\models\Wechat;
- class LiveForm
- {
- /*
- * 直播列表缓存key
- */
- const LIVE_LIST_KEY = 'live_list';
- /**
- * 缓存直播间列表时间
- */
- const CACHE_TIME = 1800;
- /**
- * store_id
- */
- public $store_id;
- /*
- * 每页数量
- */
- public $limit = 20;
- /*
- * 分页
- */
- public $page = 1;
- public function getList()
- {
- $cacheKey = static::LIVE_LIST_KEY . '_' . $this->page . '_' . $this->store_id;
- $cache = cache()->get($cacheKey);
- if ($cache) {
- return $cache;
- }
- $start = ($this->page - 1) * $this->limit;
- $wechat = Wechat::getWechat();
- $res = $wechat->broadcast->getRooms($start, $this->limit);
- if ($res['errcode'] === 0) {
- $res['room_info'] = $this->eachList($res['room_info']);
- cache()->set(static::LIVE_LIST_KEY, $res, static::CACHE_TIME);
- }
- return $res;
- }
- /**
- * 处理数据
- * @param array $list
- * @return array
- */
- protected function eachList(array $list)
- {
- foreach ($list as $key => &$value) {
- /* id958新增需求:增加直播间可见范围 */
- $purview = LiveRoomPurview::findOne(['store_id' => $this->store_id, 'room_id' => $value['roomid']]);
- $user_info = User::findOne(get_user_id());
- if($purview->type == 1) {//指定用户
- if(!in_array($user_info->id ,explode(',', $purview->user_id))) {
- unset($list[$key]);
- continue;
- }
- } elseif($purview->type == 2) {//指定会员等级
- $level_info = Level::findOne(['level' => $user_info->level, 'store_id' => $this->store_id, 'is_delete' => 0]);
- if(!in_array($level_info->id ,explode(',', $purview->level_id))) {
- unset($list[$key]);
- continue;
- }
- }
- $list = array_values($list);
- /* end */
- if ($value['live_status'] == 103) {
- $value['time'] = $this->formatDateByInterval($value['start_time'], $value['end_time']);
- $value['time2'] = $this->formatDate($value['start_time'], $value['end_time']);
- } else {
- $value['time'] = $this->formatDate($value['start_time'], $value['end_time']);
- $value['time2'] = $this->formatDateByInterval($value['start_time'], $value['end_time']);
- }
- foreach ($value['goods'] as $k => &$v) {
- $v['price'] = $v['price'] / 100;
- }
- }
- return $list;
- }
- /**
- * 格式化时间差
- * @param $start
- * @param $end
- * @return string
- */
- protected function formatDateByInterval($start, $end)
- {
- $start = date_create(date('Y-m-d H:i:s', $start));
- $end = date_create(date('Y-m-d H:i:s', $end));
- $interval = date_diff($start, $end);
- $res = $interval->format('%h');
- if ($res > 0) {
- return $res . ' 小时';
- }
- $res = $interval->format('%i');
- if ($res > 0) {
- return $res . ' 分钟';
- }
- $res = $interval->format('%s');
- return $res . ' 秒';
- }
- /**
- * 格式化时间
- * @param $start
- * @param $end
- * @return string
- */
- protected function formatDate($start, $end)
- {
- $start = date('y/m/d H:i', $start);
- $end = date('H:i', $end);
- return $start . ' - ' . $end;
- }
- }
|