model = new \app\admin\model\live\User; } /** * 查看 */ public function index() { $messageModel = new \app\admin\model\live\Message; $goodsActionModel = new \app\admin\model\live\GoodsAction; $isInstallLiveGift = \app\admin\library\project\App::isInstall('live_gift'); if($isInstallLiveGift){ $giftLogModel = new \app\admin\model\live\GiftLog; } $type = input('type','online'); $liveId = input('live_id'); //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $onlineUserIds = $this->model->getOnlineUserIds($liveId,time()); $query = $this->model->with(['binduser']); if($liveId){ $query->where(['live_id'=>$liveId]); } switch ($type){ case 'online': $query->where('user_id','in',$onlineUserIds); break; case 'offline': $query->where('user_id','not in',$onlineUserIds); break; case 'black': $query->where('black',1); break; case 'bantalk': $query->where('bantalk',1); break; } $list = $query ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as &$row) { $row->visible(['id','black','bantalk','createtime','comment_count','goods_click_count','gift_count','user_id']); $row->visible(['binduser']); $row->getRelation('binduser')->visible(['id','username','nickname']); //发言次数 $row->comment_count = $messageModel->where([ 'course_id'=>$row->live_id, 'user_id'=>$row->user_id ])->count(); //点击次数 $row->goods_click_count = $goodsActionModel->where([ 'live_id'=>$row->live_id, 'user_id'=>$row->user_id ])->count(); //送礼次数 $row->gift_count = 0; if($isInstallLiveGift){ $row->gift_count = $giftLogModel->where([ 'live_id'=>$row->live_id, 'user_id'=>$row->user_id ])->count(); } } $result = array("total" => $list->total(), "rows" => $list->items()); return $this->success("获取成功",$result); } /** * 趋势统计 * @return void */ public function trend(){ $courseId = input('live_id'); $unit = input('unit'); $studyModel = new \app\admin\model\data\Study; // 获取当前时间戳 $now = time(); // 计算两小时前的时间戳 if($unit == 2){ $timeScope = $now - 2 * 3600; // 定义5分钟的时间间隔 $interval = 300; $timeFormat = 'H:i'; }else{ $timeScope = $now - 24 * 3600; // 定义10分钟的时间间隔 $interval = 1800; $timeFormat = 'm-d H:i'; } // 准备时间间隔 $timeIntervals = []; for ($start = $timeScope; $start < $now; $start += $interval) { $end = $start + $interval; $timeIntervals[] = ['start' => $start, 'end' => $end]; } $result = []; foreach ($timeIntervals as $interval) { // 查询每个时间段的在线人数 $onlineUserCount = $studyModel ->setUniacid(false) ->where(['course_id'=>$courseId]) ->where('start_time', '<=', $interval['end']) ->where('end_time', '>=', $interval['start']) ->where('uniacid',UNIACID) ->group('user_id') ->count(); $temp = [ 'online_users'=>$onlineUserCount ]; $result[date($timeFormat, $interval['end'])] = $temp; } $this->success("获取成功",$result); } /** * 切换状态 * @param $ids * @param $status * @return void */ public function control($ids=null,$type=1,$val=1){ if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ?: $this->request->post("ids"); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $type = $type ?: $this->request->post("type"); $val = $val ?: $this->request->post("val"); $live_id = $this->request->post("live_id"); $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where('user_id', 'in', $ids)->where('live_id',$live_id)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $item) { $row = [ $type=>$val ]; $this->model->where([ 'id'=>$item['id'] ])->cache(\app\common\constant\cache\Keys::LIVEUSER($item['user_id'],$item['live_id']))->update($row); $count++; } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success("操作成功"); } $this->error(__('未操作任何数据')); } }