TdwyController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. public function getRegionByOrgId(){
  97. $url = '/pangu/sdkServer/OrgManager/queryOrgList';
  98. $parentOrgIds = Input::get('parentOrgIds', []);
  99. $params = [
  100. 'querySingleType' => true,
  101. 'needPage' => true,
  102. 'currentPage' => 1,
  103. 'pageSize' => 10,
  104. 'parentOrgIds' => $parentOrgIds,
  105. 'queryType' => 0,
  106. ];
  107. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  108. $result = json_decode($result, true);
  109. return $result;
  110. }
  111. /**
  112. * 发送请求
  113. * @param string $url
  114. * @param string $postData
  115. * @return bool|string
  116. */
  117. public function curlPost($url = '', $postData = '')
  118. {
  119. if (is_array($postData)) {
  120. $postData = http_build_query($postData);
  121. }
  122. $ch = curl_init();
  123. curl_setopt($ch, CURLOPT_URL, $url);
  124. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  125. curl_setopt($ch, CURLOPT_POST, 1);
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  127. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  128. curl_setopt_array($ch, array(
  129. CURLOPT_HTTPHEADER => array(
  130. "Accept:" . '*/*',
  131. "Content-Type:" . 'application/json',
  132. "token:" . $this->token,
  133. )
  134. ));
  135. //https请求 不验证证书和host
  136. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  137. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  138. $data = curl_exec($ch);
  139. curl_close($ch);
  140. return $data;
  141. }
  142. }