appKey = $easemob_config['appkey']; //redis $redis = new Redis(); $redisconfig = config("redis"); $redis->connect($redisconfig["host"], $redisconfig["port"]); if ($redisconfig['redis_pwd']) { $redis->auth($redisconfig['redis_pwd']); } if($redisconfig['redis_selectdb'] > 0){ $redis->select($redisconfig['redis_selectdb']); } $this->redis = $redis; } /** * 回调 * https://docs-im-beta.easemob.com/document/server-side/callback_configurations.html * * 群组和聊天室操作: * * muc:presence 有新成员加入了聊天室 *{"callId":"1101231101159883#demo_1215090885268604500","channel_channel":"1101231101159883#demo_230909515202562@admin.conference.easemob.com","eventType":"chat","channel_user":"1101231101159883#demo_9@easemob.com","chat_type":"muc","security":"845b22a4edcb4fde2185ff7f0374f051","is_downgrade":false,"content_type":"muc:presence","payload":{"muc_id":"1101231101159883#demo_230909515202562@conference.easemob.com","is_chatroom":true,"operation":"presence"},"group_id":"230909515202562","writed_channel":false,"host":"msync@ebs-ali-beijing-msync105","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"230909515202562","msg_id":"1215090885268604500","timestamp":1700475192937} * * muc:kick 踢出聊天室 * {"callId":"1101231101159883#demo_1215088856588617164","channel_channel":"1101231101159883#demo_9@easemob.com","eventType":"chat","channel_user":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","chat_type":"muc","security":"83f9462c7769df1c979fd9dea88e7400","is_downgrade":false,"content_type":"muc:kick","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","reason":"chatroom kick offline user","is_chatroom":true,"operation":"kick","status":{"description":"","error_code":"ok"}},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync71","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"9","msg_id":"1215088856588617164","timestamp":1700474720591} * * muc:absence 有成员离开了聊天室 * {"callId":"1101231101159883#demo_1215088856710252128","channel_channel":"1101231101159883#demo_231181574537218@admin.conference.easemob.com","eventType":"chat","channel_user":"admin@easemob.com","chat_type":"muc","security":"ec7d29ea415ea5930c2236089dff3bc2","is_downgrade":false,"content_type":"muc:absence","payload":{"muc_id":"1101231101159883#demo_231181574537218@conference.easemob.com","is_chatroom":true,"operation":"absence"},"group_id":"231181574537218","writed_channel":false,"host":"msync@ebs-ali-beijing-msync108","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"231181574537218","msg_id":"1215088856710252128","timestamp":1700474720619} * * muc:leave 成员主动退出聊天室 * {"callId":"1101231101159883#demo_1216580964256318924","channel_channel":"1101231101159883#demo_231346001739777@conference.easemob.com","eventType":"chat","channel_user":"1101231101159883#demo_231346001739777@admin.conference.easemob.com","chat_type":"muc","security":"059f0313db99c98cde55b42d28538f9a","is_downgrade":false,"content_type":"muc:leave","payload":{"muc_id":"1101231101159883#demo_231346001739777@conference.easemob.com","is_chatroom":true,"operation":"leave","status":{"description":"","error_code":"ok"}},"group_id":"231346001739777","writed_channel":false,"host":"msync@ebs-ali-beijing-msync71","appkey":"1101231101159883#demo","from":"1101231101159883#demo_9@easemob.com","to":"231346001739777","msg_id":"1216580964256318924","timestamp":1700822128988} * * 用户登入 * {"callId":"1101231101159883#demo_927e28f2-0e88-4a58-855b-c8d26949ea4f","reason":"login","security":"6b793f812b5ed74f62d3a95d9ea48c8f","os":"android","ip":"182.37.138.94:39768","host":"msync@ebs-ali-beijing-msync60","session_id":"1215076057950983584","appkey":"1101231101159883#demo","user":"1101231101159883#demo_2@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837","version":"4.1.2","timestamp":1700471740680,"status":"online"} * * 用户登出 * {"callId":"1101231101159883#demo_ee82832b-3455-472d-9024-3706b1e519a7","reason":"logout","security":"6266f2a31bc3b603d8458f3d39a49d47","os":"android","ip":"182.37.138.94:39768","host":"msync@ebs-ali-beijing-msync60","session_id":"1215076057950983584","appkey":"1101231101159883#demo","user":"1101231101159883#demo_2@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837","version":"4.1.2","timestamp":1700472505682,"status":"offline"} * * 用户登出(被其他设备踢掉) * {"callId":"1101231101159883#demo_b2cdc4d9-a499-4e46-8aff-4503fb83a5c5","reason":"replaced","security":"1f4bb2ebb99c2f5da20048c7ca87f352","os":"android","ip":"182.37.138.94:41044","host":"msync@ebs-ali-beijing-msync62","appkey":"1101231101159883#demo","user":"1101231101159883#demo_9@easemob.com/android_fb260fdf-3935-4499-a09d-b270fdd88a0e","version":"4.1.2","timestamp":1700473675582,"status":"offline"} */ //登入,登出,进入房间其实都不需要处理。登入不影响房间。只登出只要不影响房间,在退出房间时间会重复回调。登入房间有接口,这里没必要重复处理。就只有退出房间有用也简单 public function callback() { $this->notify_log_start(); $input = file_get_contents("php://input"); // 主题信息 $input = json_decode($input,true); //验证加密 //解析数据格式 //用户登入 if(isset($input['reason']) && $input['reason'] == 'login' && isset($input['status']) && $input['status'] == 'online'){ $user = $input['user']; $uid = $this->get_easemob_uid($user);//用户主键id //开始处理 $loginToday = Db::name('user')->where('id',$uid)->whereTime('onlinetime', 'today')->find(); if ($loginToday){ \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$uid]); }else{ \app\common\model\User::update(["is_online"=>1,"onlinetime"=>time()],["id"=>$uid]); } exit; } //用户登出 //用户登出(被其他设备踢掉) if(isset($input['reason']) && ( $input['reason'] == 'logout' || $input['status'] == 'replaced') && isset($input['status']) && $input['status'] == 'offline'){ $user = $input['user']; $uid = $this->get_easemob_uid($user);//用户主键id //开始处理 // 更新用户在线状态为离线 \app\common\model\User::update(["is_online"=>0],["id"=>$uid]); \app\common\model\User::update(["is_live"=>0],["id"=>$uid]); $livingUserPartyId = $this->redis->hGet("livingUser", $uid); if ($livingUserPartyId) { // 扣除在线用户在房间情况 $this->redis->hDel("livingUser", $uid); $this->redis->HDel("online_" . $livingUserPartyId, $uid); $this->redis->zRem("party_user_".$livingUserPartyId,$uid); //新加的 //退出房间获取房间自定义属性 //清理这个人的麦序 //重置房间在线人数 $party_info = Db::name('party')->field('id,user_id,easemob_room_id')->where('id',$livingUserPartyId)->find(); if(empty($party_info)){ exit; } $this->clean_room($party_info,$uid); } exit; } //群组和聊天室操作: //muc:presence 有新成员加入了聊天室 //muc:kick 踢出聊天室 //muc:absence 有成员离开了聊天室 //muc:leave 成员主动退出聊天室 if(isset($input['chat_type']) && $input['chat_type'] == 'muc' && isset($input['eventType']) && $input['eventType'] == 'chat'){ $room_id = ''; $is_chatroom = false; $operation = false; $error_code = false; if(isset($input['payload'])){ $payload = $input['payload']; if(isset($payload['muc_id'])){ $room_id = $this->get_easemob_uid($payload['muc_id']); } if(isset($payload['is_chatroom']) && ($payload['is_chatroom'] == true || $payload['is_chatroom'] == 1) ){ $is_chatroom = true; } if(isset($payload['operation']) && in_array($payload['operation'],['kick','absence','leave']) ){ $operation = 'out'; } if(isset($payload['operation']) && $payload['operation'] == 'presence' ){ $operation = 'in'; $operation = false;//进入房间走接口,不在这里处理 } if(isset($payload['status']['error_code']) && $payload['status']['error_code'] == 'ok'){ $error_code = true; } } //用户 $uid = 0; if(isset($input['from'])){ $uid = $this->get_easemob_uid($input['from']); } $party_info = Db::name('party')->field('id,user_id,easemob_room_id')->where('easemob_room_id',$room_id)->find(); if(empty($party_info)){ exit; } $partyid = $party_info['id']; //开始处理 //进入房间 if($uid && $partyid && $is_chatroom && $operation == 'in' && $error_code){ $userId = $uid; $this->redis->hSet("online_" . $partyid, $userId, $userId); $this->redis->zAdd("party_user_".$partyid,$userId,$userId); //新加的 // 记录在线用户在房间情况 $this->redis->hSet("livingUser",$userId,$partyid); // 更新房间在线状态 $partyInfo = $this->redis->get("party_" . $partyid); if ($partyInfo) { $partyInfo = json_decode($partyInfo, true); if ($partyInfo["is_online"] != 1) { $partyInfo["is_online"] = 1; $this->redis->set("party_" . $partyid, json_encode($partyInfo)); \app\common\model\Party::update(["is_online" => 1], ["id" => $partyid]); } } // 更新房间在线状态 $liveInfo = $this->redis->get("live_" . $partyid); if ($liveInfo) { $liveInfo = json_decode($liveInfo, true); if ($liveInfo["is_online"] != 1) { $liveInfo["is_online"] = 1; $this->redis->set("live_" . $partyid, json_encode($liveInfo)); \app\common\model\Party::update(["is_online" => 1], ["id" => $partyid]); $rs_user = Db::name('user')->where('id',$userId)->update(['is_live'=>1]); } } //[环信] 更新聊天室自定义属性 在线用户人数 $easemob = new Easemoblib(); $matedata = [ 'online_user_num' => count($this->redis->hGetAll("online_".$partyid)), ]; $easemob->room_setRoomCustomAttributeForced($room_id,$uid,$matedata); } //出去房间 if($uid && $partyid && $is_chatroom && $operation == 'out' && $error_code){ $userId = $uid; $this->redis->HDel("online_" . $partyid, $userId); $this->redis->zRem("party_user_".$partyid,$userId); //新加的 // 扣除在线用户在房间情况 $this->redis->hDel("livingUser",$userId); // 更新房间在线状态 $partyInfo = $this->redis->get("party_" . $partyid); if ($partyInfo) { $partyInfo = json_decode($partyInfo, true); $memCount = count($this->redis->hGetAll("online_" . $partyid)); if ($memCount <= 0) { $partyInfo["is_online"] = 0; $this->redis->set("party_" . $partyid, json_encode($partyInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $partyid]); } } // $liveInfo = $this->redis->get("live_" . $partyid); if ($liveInfo) { $liveInfo = json_decode($liveInfo, true); $memCount = count($this->redis->hGetAll("online_" . $partyid)); if ($memCount <= 0) { $liveInfo["is_online"] = 0; $this->redis->set("live_" . $partyid, json_encode($liveInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $partyid]); $rs_user = Db::name('user')->where('id',$userId)->update(['is_live'=>0]); } } //退出房间获取房间自定义属性 //清理这个人的麦序 //重置房间在线人数 $this->clean_room($party_info,$uid); } exit; } } //清除某个麦位 //重置房间在线人数 private function clean_room($party_info,$uid){ $easemob_room_id = $party_info['easemob_room_id']; //获取所有麦位,假设8个 $easemob = new Easemoblib(); $seatlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['seat0','seat1','seat2','seat3','seat4','seat5','seat6','seat7']); $newseat = [ 'charm' => 0, //红心,魅力值 'isMaster' => false, // 是否是房主 'headUrl' => '', // 头像 'userNo' => '', // 座位上用户no 'rtcUid' => '', // 座位上用户id,与rtc的userId一致 'name' => '', // 座位上用户昵称 'seatIndex' => -1, // 座位编号 'chorusSongCode' => '', // 是否合唱 'isAudioMuted' => 1, // 是否静音 'isVideoMuted' => 0, // 是否开启视频 'checked' => false, // 用于送礼物选择用户 'isUsed' => true, // 用于送礼物选择用户 'gender' => 1, //性别 ]; $matedata = []; if(!empty($seatlist)){ foreach($seatlist as $k => $seat){ $seat = json_decode($seat,true); //找到当前用户所在的麦位 if(isset($seat['rtcUid']) && !empty($seat['rtcUid']) && $seat['rtcUid'] == $uid){ //$newseat['seatIndex'] = substr($k,-1); //初始化这个麦位 $matedata = [ $k => json_encode($newseat), ]; //redis,删除占用麦位 $key = "party_seat_".$party_info['id']; $seat_num = substr($k,-1,1); $this->redis->HDel($key, $seat_num); } } } //重置房间在线人数 $matedata['online_user_num'] = count($this->redis->hGetAll("online_".$party_info['id'])); //删除他点的歌曲 $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']); if(isset($singlist['waitsing_list']) && !empty($singlist['waitsing_list'])){ $singlist = json_decode($singlist['waitsing_list'],true); //轮询,找到,删除 $new_singlist = []; foreach($singlist as $key => $sing){ if($sing['userNo'] == $uid){ unset($singlist[$key]); }else{ $new_singlist[] = $sing; } } $matedata['waitsing_list'] = json_encode($new_singlist); } //修改属性 if(!empty($matedata)){ $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata); } return true; } /** * 获取用户所加入的聊天室 */ private function getJoindGroupList($user_id) { $easemob = new Easemoblib(); $list = $easemob->room_listAllRoomsUserJoined($user_id); return $list; } /** * 获取群组信息 */ private function getGroupInfo($easemob_room_id = '230909515202562') { $easemob = new Easemoblib(); $info = $easemob->room_getRoom($easemob_room_id); return $info; } /** * 更新麦位前四 */ private function updatePosition($user_id,$party_id) { // 获取用户头像 $userInfo = \app\common\model\User::field("avatar")->where(["id"=>$user_id])->find(); $userAvatar = isset($userInfo["avatar"])?$userInfo["avatar"]:""; // 更新下麦时间 $update = []; $update["offsite_time"] = time(); $update["status"] = 2; \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]); // $room_type = Db::name('party')->where('id',$party_id)->value('room_type'); $redisData = $this->redis->get($room_type."_".$party_id); if(!$redisData){ return true; } $partyInfo = json_decode($redisData,true); // 遍历已有头像 $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:""; if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $userAvatar) unset($partyInfo["party_user"][$k]); $this->redis->set($room_type."_".$party_id,json_encode($partyInfo)); } /** * 取消排麦 */ private function cancelLineUp($user_id,$party_id) { return false; //因为没有排麦了,设计图就没这个功能,所以后面没了 } /** * 强制下线 */ private function downSite($user_id,$groupsites) { /*此处删减参考ggyuyin,api/tenim/downsite*/ // 先更新下麦时间 /*$update = []; $update["offsite_time"] = time(); $update["status"] = 2; \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);*/ // 循环房间 /*此处删减参考ggyuyin,api/tenim/downsite*/ } //输入: 1101231101159883#demo_9@easemob.com/android_f9f51032-3369-4f02-95bb-0cbf41b26837 //输出: 9 //输入: 1101231101159883#demo_231181574537218@admin.conference.easemob.com //输出: 231181574537218 private function get_easemob_uid($user = ''){ //去掉后半段 $easemob = '@'; $start = strpos($user,$easemob); $uid = substr($user,0,$start); //echo $uid; //echo '
'; //去掉前缀 $uid = substr($uid,strlen($this->appKey)+1); //echo $uid; return intval($uid); } //异步日志 private function notify_log_start($paytype = 'easemob'){ //记录支付回调数据 ignore_user_abort(); // run script in background set_time_limit(30); // 日志文件 start $log_base_dir = '../paylog/'.$paytype.'/'; if (!is_dir($log_base_dir)) { mkdir($log_base_dir, 0770, true); @chmod($log_base_dir, 0770); } $notify_file = $log_base_dir.'notify.txt'; if(!file_exists($notify_file)) { @touch($notify_file); @chmod($notify_file, 0770); } if(filesize($notify_file)>5242880)//大于5M自动切换 { rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt'); } if(!file_exists($notify_file)) { @touch($notify_file); @chmod($notify_file, 0770); } // 日志文件 end //开始写入 $xml = file_get_contents("php://input"); file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND); ini_set('display_errors','On'); return $notify_file; } }