218 lines
6.1 KiB
PHP
218 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace app\admin\model\live;
|
|
|
|
use think\Model;
|
|
|
|
|
|
class User extends Model
|
|
{
|
|
|
|
// 表名
|
|
protected $name = 'live_user';
|
|
|
|
protected $onlineUserTime = 30;
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = false;
|
|
protected $deleteTime = false;
|
|
|
|
// 追加属性
|
|
protected $append = [
|
|
'black_text',
|
|
'bantalk_text'
|
|
];
|
|
|
|
public function getBlackList()
|
|
{
|
|
\think\Lang::load(APP_PATH.'/admin/lang/zh-cn/live/user.php');
|
|
return ['1' => __('Black 1'), '0' => __('Black 0')];
|
|
}
|
|
|
|
|
|
public function getBlackTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['black']) ? $data['black'] : '');
|
|
$list = $this->getBlackList();
|
|
return isset($list[$value]) ? $list[$value] : '';
|
|
}
|
|
|
|
public function getBantalkList()
|
|
{
|
|
\think\Lang::load(APP_PATH.'/admin/lang/zh-cn/live/user.php');
|
|
return ['1' => __('Bantalk 1'), '0' => __('Bantalk 0')];
|
|
}
|
|
|
|
|
|
public function getBantalkTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['bantalk']) ? $data['bantalk'] : '');
|
|
$list = $this->getBantalkList();
|
|
return isset($list[$value]) ? $list[$value] : '';
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取在线的用户 ID
|
|
* @param $liveId 直播间
|
|
* @param $time 时间节点
|
|
* @return void
|
|
*/
|
|
public function getOnlineUserIds($liveId,$time = null){
|
|
$serviceUserIds = $this->getMessageProviderOnlineUserIds($liveId);
|
|
if(!empty($serviceUserIds)){
|
|
return $serviceUserIds;
|
|
}
|
|
|
|
if(!$time){
|
|
$time = time();
|
|
}
|
|
|
|
$studyModel = new \app\common\model\user\Study;
|
|
$onlineUserIds = $studyModel->where([
|
|
'course_id'=>$liveId
|
|
])->where('end_time','between',[($time - $this->onlineUserTime),($time + $this->onlineUserTime)])->group('user_id')->column('user_id');
|
|
|
|
return $onlineUserIds;
|
|
}
|
|
|
|
/**
|
|
* 获取在线的用户数量
|
|
* @param $liveId 直播间
|
|
* @param $time 时间节点
|
|
* @return void
|
|
*/
|
|
public function getOnlineUserCount($liveId){
|
|
$serviceUserIds = $this->getMessageProviderOnlineUserIds($liveId);
|
|
if(!empty($serviceUserIds)){
|
|
return count($serviceUserIds);
|
|
}
|
|
|
|
$time = time();
|
|
$studyModel = new \app\common\model\user\Study;
|
|
$onlineUserIds = $studyModel->where([
|
|
'course_id'=>$liveId
|
|
])->where('end_time','between',[($time - $studyModel->timeStep) - 15,($time + $studyModel->timeStep) + 15])->group('user_id')->column('user_id');
|
|
|
|
return count($onlineUserIds);
|
|
}
|
|
|
|
/**
|
|
* 获取互动消息服务中的在线用户 ID。
|
|
* @param int $liveId
|
|
* @return array
|
|
*/
|
|
protected function getMessageProviderOnlineUserIds($liveId)
|
|
{
|
|
if(!$liveId){
|
|
return [];
|
|
}
|
|
|
|
$room = \app\common\model\live\Room::where(['course_id'=>$liveId])->field('message_topic')->find();
|
|
if(!$room || empty($room['message_topic'])){
|
|
return [];
|
|
}
|
|
|
|
$provider = \app\common\library\live\message\Manager::getGlobalProviderName();
|
|
|
|
// 阿里云模式下,确保群组存在(兼容历史直播间未建群场景)
|
|
if ($provider === 'aliyun') {
|
|
$courseModel = new \app\admin\model\Course;
|
|
$course = $courseModel->where(['id' => $liveId])->field('name')->find();
|
|
$groupName = ($course && $course['name']) ? $course['name'] : '';
|
|
\app\common\library\live\message\Manager::ensureAliyunGroup($room['message_topic'], $groupName);
|
|
}
|
|
|
|
$data = \app\common\library\live\Message::getOnlineUserByProvider($provider, $room['message_topic'], 0, 200);
|
|
if(!$data || empty($data['list'])){
|
|
return [];
|
|
}
|
|
|
|
$userIds = [];
|
|
foreach ($data['list'] as $item) {
|
|
$userId = $this->parseOnlineUserId($item, $provider);
|
|
if($userId){
|
|
$userIds[] = $userId;
|
|
}
|
|
}
|
|
|
|
return array_values(array_unique($userIds));
|
|
}
|
|
|
|
/**
|
|
* 将服务商在线用户标识转换为业务用户 ID。
|
|
* @param string $item
|
|
* @param string $provider
|
|
* @return int
|
|
*/
|
|
protected function parseOnlineUserId($item, $provider)
|
|
{
|
|
$item = (string)$item;
|
|
if($item === '' || $item === 'admin' || strpos($item, 'admin-') === 0 || $item === 'system'){
|
|
return 0;
|
|
}
|
|
|
|
if($provider === 'aliyun'){
|
|
if(strpos($item, 'u-') === 0){
|
|
$id = substr($item, 2);
|
|
return ctype_digit($id) ? (int)$id : 0;
|
|
}
|
|
return ctype_digit($item) ? (int)$item : 0;
|
|
}
|
|
|
|
$userData = \app\common\library\Token::get($item);
|
|
if($userData && isset($userData['user_id'])){
|
|
return (int)$userData['user_id'];
|
|
}
|
|
|
|
return ctype_digit($item) ? (int)$item : 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* 加入直播
|
|
* @param $userId 用户
|
|
* @param $liveId 直播间
|
|
* @return array 成员信息
|
|
*/
|
|
public function joinLive($userId,$liveId){
|
|
|
|
$isJoin = self::where([
|
|
'user_id'=>$userId,
|
|
'live_id'=>$liveId
|
|
])->cache(\app\common\constant\cache\Keys::LIVEUSER($userId,$liveId))->find();
|
|
|
|
if(!$isJoin){
|
|
$data = [
|
|
'user_id'=>$userId,
|
|
'live_id'=>$liveId,
|
|
'uniacid'=>UNIACID,
|
|
'bantalk'=>0,
|
|
'black'=>0,
|
|
'createtime'=>time()
|
|
];
|
|
self::insert($data);
|
|
|
|
return $data;
|
|
}
|
|
|
|
return $isJoin;
|
|
}
|
|
|
|
|
|
public function course()
|
|
{
|
|
return $this->belongsTo('app\admin\model\Course', 'live_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
|
}
|
|
|
|
|
|
public function binduser()
|
|
{
|
|
return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
|
}
|
|
}
|