Browse Source

trtc回调

zhangxiaobin 1 year ago
parent
commit
33a9c4d0a5
2 changed files with 149 additions and 1 deletions
  1. 118 1
      application/api/controller/Tenim.php
  2. 31 0
      application/common.php

+ 118 - 1
application/api/controller/Tenim.php

@@ -14,7 +14,7 @@ use think\Db;
  */
 class Tenim extends Api
 {
-    protected $noNeedLogin = ["callback",'test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom',
+    protected $noNeedLogin = ['trtc_callback',"callback",'test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom',
         'autoUserLine','getRoomUser','getGroupList','getGroupInfo'];
     protected $noNeedRight = ['*'];
 
@@ -584,6 +584,123 @@ exit;
 
     }
 
+    public function trtc_callback(){
+
+        $input = file_get_contents("php://input");
+        filePut('start:'.$input,'callback.txt');
+
+        /*$input = '{
+            "EventGroupId":	1,
+            "EventType":	104,
+            "CallbackTs":	1640331295244,
+            "EventInfo":	{
+                "RoomId":	1604,
+                "EventTs":	1640331295,
+                "EventMsTs":	1640331295239,
+                "UserId":	"26",
+                "Role":	20,
+                "Reason":	1
+            }
+        }';*/
+
+        $input = json_decode($input,true);
+        $roomId = $input['EventInfo']['RoomId'];
+        //房间事件组
+        if($input['EventGroupId'] == 1){
+            if($input['EventType'] == 101){
+                //创建房间,第一个进来的
+            }elseif($input['EventType'] == 102){
+                //解散房间,最后一个走的
+                //没有UserId
+            }elseif($input['EventType'] == 103){
+                //进入房间,主播role=20 观众role=21
+                $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']);
+                }
+                $userId = $input['EventInfo']['UserId'];
+                $redis->hSet("online_" . $roomId, $userId, $userId);
+
+                // 记录在线用户在房间情况
+                $redis->hSet("livingUser",$userId,$roomId);
+
+                // 更新房间在线状态
+                $partyInfo = $redis->get("party_" . $roomId);
+                if ($partyInfo) {
+                    $partyInfo = json_decode($partyInfo, true);
+                    if ($partyInfo["is_online"] != 1) {
+                        $partyInfo["is_online"] = 1;
+                        $redis->set("party_" . $roomId, json_encode($partyInfo));
+                        \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
+                    }
+                }
+
+                // 更新房间在线状态
+                $liveInfo = $redis->get("live_" . $roomId);
+                if ($liveInfo) {
+                    $liveInfo = json_decode($liveInfo, true);
+                    if ($input['EventInfo']['Role'] == 20) {
+                        $liveInfo["is_online"] = 1;
+                        $redis->set("live_" . $roomId, json_encode($liveInfo));
+                        \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
+                        $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>1]);
+                    }
+                }
+
+            }elseif($input['EventType'] == 104){
+                //退出房间,主播role=20 观众role=21
+                $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']);
+                }
+                $userId = $input['EventInfo']['UserId'];
+                $redis->HDel("online_" . $roomId, $userId);
+
+                // 扣除在线用户在房间情况
+                $redis->hDel("livingUser",$userId);
+
+                // 更新房间在线状态
+                $partyInfo = $redis->get("party_" . $roomId);
+                if ($partyInfo) {
+                    $partyInfo = json_decode($partyInfo, true);
+                    $memCount = count($redis->hGetAll("online_" . $roomId));
+                    if ($memCount <= 0) {
+                        $partyInfo["is_online"] = 0;
+                        $redis->set("party_" . $roomId, json_encode($partyInfo));
+                        \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
+                    }
+                }
+                //
+                $liveInfo = $redis->get("live_" . $roomId);
+                if ($liveInfo) {
+                    $liveInfo = json_decode($liveInfo, true);
+                    if($input['EventInfo']['Role'] == 20){
+                        $liveInfo["is_online"] = 0;
+                        $redis->set("live_" . $roomId, json_encode($liveInfo));
+                        \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
+                        $rs_user = Db::name('user')->where('id',$userId)->update(['is_livebc'=>0]);
+                    }
+                }
+
+            }
+        }elseif($input['EventGroupId'] == 2){
+
+        }
+
+        //结束
+        echo json_encode(['code' => 0]);exit;
+    }
+
     /**
      * 回调
      */

+ 31 - 0
application/common.php

@@ -773,4 +773,35 @@ function ip_to_address(){
         }
     }
     return $ipaddress;
+}
+if(!function_exists('filePut')) {
+    /**
+     * 在线支付日志
+     */
+    function filePut($info, $text = 'notify.txt')
+    {
+        if (is_array($info)) {
+            $info = json_encode($info, JSON_UNESCAPED_UNICODE);
+        }
+        if (!file_exist(RUNTIME_PATH . 'paylog/')) {
+            mk_dir(RUNTIME_PATH . 'paylog/');
+        }
+        $file = RUNTIME_PATH . 'paylog/' . $text;
+        touch_file($file);
+        file_put_contents($file, "\r\n" . date('Y-m-d H:i:s') . ' ' . $info, FILE_APPEND);
+    }
+}
+if(!function_exists('touch_file')) {
+    /**
+     * 新建文件
+     */
+    function touch_file($file = '')
+    {
+        if($file) {
+            if(!file_exists($file)) {
+                @touch($file);
+                @chmod($file, 0770);
+            }
+        }
+    }
 }