TdwyController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 3/17/22
  6. * Time: 7:01 PM
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use App\Http\Controllers\Api\BaseController;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\Input;
  12. use Modules\Camera\Enum\CameraEnum;
  13. class TdwyController extends BaseController
  14. {
  15. protected $pre_url; // 请求地址
  16. protected $username; // 请求username
  17. protected $password; // 请求password
  18. protected $sysId; // 请求sysId
  19. protected $token; // 请求sysId
  20. public function __construct()
  21. {
  22. $this->pre_url = Input::get('url');
  23. $this->username = Input::get('username');
  24. $this->password = Input::get('password');
  25. $this->sysId = Input::get('sysId');
  26. //先获取token
  27. $this->token = $this->loginUser();
  28. }
  29. //登录获取token
  30. protected function loginUser()
  31. {
  32. $url = '/pangu/sdkServer/user/loginUser';
  33. $params = [
  34. 'username' => $this->username,
  35. 'password' => $this->password,
  36. 'sysId' => $this->sysId,
  37. ];
  38. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  39. $result = json_decode($result, true);
  40. $token = '';
  41. if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) {
  42. $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE);
  43. return $token;
  44. }
  45. if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) {
  46. $token = $result['content']['token'];
  47. Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440);
  48. }
  49. return $token;
  50. }
  51. //查询设备列表
  52. public function queryDeviceList()
  53. {
  54. $url = '/pangu/sdkServer/device/queryDeviceList';
  55. $sOrgId = Input::get('sOrgId', '');
  56. $iDeviceTypes = Input::get('iDeviceTypes', [5]);
  57. $rootCodes = Input::get('rootCodes', ["BH-0001"]);
  58. $isOrgTree = Input::get('isOrgTree', true);
  59. $orgTypeIds = Input::get('orgTypeIds', []);
  60. $needPage = Input::get('needPage', true);
  61. $pageSize = Input::get('pageSize', 50);
  62. $currentPage = Input::get('currentPage', 1);
  63. $params = [
  64. 'sOrgId' => $sOrgId,
  65. 'iDeviceTypes' => $iDeviceTypes,
  66. 'rootCodes' => $rootCodes,
  67. 'isOrgTree' => $isOrgTree,
  68. 'orgTypeIds' => $orgTypeIds,
  69. 'needPage' => $needPage,
  70. 'pageSize' => $pageSize,
  71. 'currentPage' => $currentPage,
  72. ];
  73. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  74. $result = json_decode($result, true);
  75. return $result;
  76. }
  77. //查询设备rtsp流
  78. public function getRtspById()
  79. {
  80. $url = '/pangu/sdkServer/videoStreaming/getRtspById';
  81. $sId = Input::get('sId', []);
  82. $ip = Input::get('ip', '');
  83. $port = Input::get('port', '');
  84. $type = Input::get('type', 0);
  85. $params = [
  86. 'sId' => $sId,
  87. 'ip' => $ip,
  88. 'port' => $port,
  89. 'type' => $type
  90. ];
  91. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  92. $result = json_decode($result, true);
  93. return $result;
  94. }
  95. /**
  96. * 发送请求
  97. * @param string $url
  98. * @param string $postData
  99. * @return bool|string
  100. */
  101. public function curlPost($url = '', $postData = '')
  102. {
  103. if (is_array($postData)) {
  104. $postData = http_build_query($postData);
  105. }
  106. $ch = curl_init();
  107. curl_setopt($ch, CURLOPT_URL, $url);
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  109. curl_setopt($ch, CURLOPT_POST, 1);
  110. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  111. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  112. curl_setopt_array($ch, array(
  113. CURLOPT_HTTPHEADER => array(
  114. "Accept:" . '*/*',
  115. "Content-Type:" . 'application/json',
  116. "token:" . $this->token,
  117. )
  118. ));
  119. //https请求 不验证证书和host
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  122. $data = curl_exec($ch);
  123. curl_close($ch);
  124. return $data;
  125. }
  126. }