Tentrtc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Exception;
  5. use think\Log;
  6. use Redis;
  7. /**
  8. * 实时音视频回调
  9. */
  10. class Tentrtc extends Api
  11. {
  12. protected $noNeedLogin = ['callback'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 支付回调
  16. */
  17. public function callback()
  18. {
  19. try {
  20. $header = $this->request->header();
  21. $body = file_get_contents('php://input');
  22. // 获取body参数
  23. $aRequest = json_decode($body, true);
  24. // 签名验证
  25. if ($this->verifyRoomApiSign($body, $header['sign']) == false) {
  26. Log::write(sprintf('%s,腾讯云回调,签名验证失败:%s', __METHOD__, var_export($body, true)), 'error');
  27. $this->error('签名验证失败');
  28. }
  29. $roomId = $aRequest['EventInfo']['RoomId'];
  30. switch ($aRequest['EventType']) {
  31. case 103:// 进入房间
  32. $redis = new Redis();
  33. $redisconfig = config("redis");
  34. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  35. if ($redisconfig['redis_pwd']) {
  36. $redis->auth($redisconfig['redis_pwd']);
  37. }
  38. if($redisconfig['redis_selectdb'] > 0){
  39. $redis->select($redisconfig['redis_selectdb']);
  40. }
  41. $userId = $aRequest['EventInfo']['UserId'];
  42. $redis->hSet("online_" . $roomId, $userId, $userId);
  43. // 记录在线用户在房间情况
  44. $redis->hSet("livingUser",$userId,$roomId);
  45. // 更新房间在线状态
  46. $partyInfo = $redis->get("party_" . $roomId);
  47. if ($partyInfo) {
  48. $partyInfo = json_decode($partyInfo, true);
  49. if ($partyInfo["is_online"] != 1) {
  50. $partyInfo["is_online"] = 1;
  51. $redis->set("party_" . $roomId, json_encode($partyInfo));
  52. \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
  53. }
  54. }
  55. // 更新房间在线状态
  56. $liveInfo = $redis->get("live_" . $roomId);
  57. if ($liveInfo) {
  58. $liveInfo = json_decode($liveInfo, true);
  59. if ($liveInfo["is_online"] != 1) {
  60. $liveInfo["is_online"] = 1;
  61. $redis->set("live_" . $roomId, json_encode($liveInfo));
  62. \app\common\model\Party::update(["is_online" => 1], ["id" => $roomId]);
  63. }
  64. }
  65. break;
  66. case 104: // 退出房间
  67. $redis = new Redis();
  68. $redisconfig = config("redis");
  69. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  70. if ($redisconfig['redis_pwd']) {
  71. $redis->auth($redisconfig['redis_pwd']);
  72. }
  73. if($redisconfig['redis_selectdb'] > 0){
  74. $redis->select($redisconfig['redis_selectdb']);
  75. }
  76. $userId = $aRequest['EventInfo']['UserId'];
  77. $redis->HDel("online_" . $roomId, $userId);
  78. // 扣除在线用户在房间情况
  79. $redis->hDel("livingUser",$userId);
  80. // 更新房间在线状态
  81. $partyInfo = $redis->get("party_" . $roomId);
  82. if ($partyInfo) {
  83. $partyInfo = json_decode($partyInfo, true);
  84. $memCount = count($redis->hGetAll("online_" . $roomId));
  85. if ($memCount <= 0) {
  86. $partyInfo["is_online"] = 0;
  87. $redis->set("party_" . $roomId, json_encode($partyInfo));
  88. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  89. }
  90. }
  91. $liveInfo = $redis->get("live_" . $roomId);
  92. if ($liveInfo) {
  93. $liveInfo = json_decode($liveInfo, true);
  94. $memCount = count($redis->hGetAll("online_" . $roomId));
  95. if ($memCount <= 0) {
  96. $liveInfo["is_online"] = 0;
  97. $redis->set("live_" . $roomId, json_encode($liveInfo));
  98. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  99. }
  100. }
  101. break;
  102. default:
  103. break;
  104. }
  105. json(0);
  106. exit;
  107. } catch (Exception $e) {
  108. Log::write(sprintf('%s:系统错误: %s IN: %s LINE: %s', __METHOD__, $e->getMessage(), $e->getFile(), $e->getLine()), 'error');
  109. $this->error(['code' => 9999, 'msg' => $e->getMessage()]);
  110. }
  111. }
  112. /**
  113. * 实时音视频验签
  114. */
  115. private function verifyRoomApiSign($aInput, $headerSign)
  116. {
  117. $callbackKey = 'c527c7078f47ba4d';
  118. $sign = hash_hmac('sha256', $aInput, $callbackKey, true);
  119. $sign = rtrim(base64_encode($sign));
  120. if ($sign == $headerSign) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. }