RoomTencent.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by KLXQ
  4. * user: youyi
  5. * Date:2024/6/6 10:35.
  6. * Tencent ROOM SDK.
  7. * 目前主要用于判断房间是否存在.
  8. */
  9. namespace App\Http\Helpers\Tencent;
  10. use App\Services\TencentRequestService;
  11. use Illuminate\Support\Facades\Log;
  12. use Random\RandomException;
  13. class RoomTencent
  14. {
  15. public const GET_ROOM_INFO = 'get_room_info';
  16. public string $endpoint = 'https://console.tim.qq.com/v4/room_engine_http_srv/';
  17. public int $sdkAppId;
  18. public string $identifier;
  19. public string $action;
  20. public array $resultArr = ['code' => 200, 'msg' => 'success', 'data' => []];
  21. /**
  22. * @param mixed $action
  23. *
  24. * @throws RandomException
  25. */
  26. public function __construct($action)
  27. {
  28. $this->sdkAppId = config('tencent.trtc.app_id');
  29. $this->identifier = config('tencent.trtc.admin');
  30. $this->action = $action;
  31. $sign = new SignTencent();
  32. $userSig = $sign->genUserSign($this->identifier);
  33. $query = [
  34. 'sdkappid' => $this->sdkAppId,
  35. 'identifier' => $this->identifier,
  36. 'usersig' => $userSig,
  37. 'random' => random_int(1, 9999),
  38. 'contenttype' => 'json',
  39. ];
  40. $this->endpoint .= $action.'?'.http_build_query($query);
  41. }
  42. public function getRoomInfo($roomId): void
  43. {
  44. $params = [
  45. 'RoomId' => (string) $roomId,
  46. ];
  47. $payload = json_encode($params);
  48. $this->httpRequest($this->endpoint, $payload);
  49. }
  50. public function httpRequest($url, $payload): void
  51. {
  52. try {
  53. $ch = curl_init();
  54. curl_setopt($ch, CURLOPT_URL, $url);
  55. curl_setopt($ch, CURLOPT_POST, true);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. $response = curl_exec($ch);
  59. curl_close($ch);
  60. $res = json_decode($response, true);
  61. Log::channel('tencent')->info('response: '.$response);
  62. if (0 !== $res['ErrorCode']) {
  63. $this->resultArr['code'] = $res['ErrorCode'];
  64. $this->resultArr['msg'] = $res['ErrorInfo'];
  65. } else {
  66. if (isset($res['Response'])) {
  67. $this->resultArr['data'] = $res['Response'];
  68. }
  69. }
  70. } catch (\Exception $err) {
  71. $this->resultArr['code'] = -1;
  72. $this->resultArr['msg'] = $err->getMessage();
  73. }
  74. // 记录日志
  75. // TencentRequestService::create('room', $this->action, $payload, $this->resultArr);
  76. }
  77. }