TdwyController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. use Modules\Mine\Entities\MineList;
  14. use Modules\Mine\Services\MineServices;
  15. class TdwyController extends BaseController
  16. {
  17. protected $pre_url; // 请求地址
  18. protected $username; // 请求username
  19. protected $password; // 请求password
  20. protected $sysId; // 请求sysId
  21. protected $token; // 请求sysId
  22. public function __construct()
  23. {
  24. $this->pre_url = Input::get('url');
  25. $this->username = Input::get('username');
  26. $this->password = Input::get('password');
  27. $this->sysId = Input::get('sysId');
  28. //先获取token
  29. $this->token = $this->loginUser();
  30. }
  31. //登录获取token
  32. protected function loginUser()
  33. {
  34. $url = '/pangu/sdkServer/user/loginUser';
  35. $params = [
  36. 'username' => $this->username,
  37. 'password' => $this->password,
  38. 'sysId' => $this->sysId,
  39. ];
  40. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  41. $result = json_decode($result, true);
  42. $token = '';
  43. if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) {
  44. $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE);
  45. return $token;
  46. }
  47. if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) {
  48. $token = $result['content']['token'];
  49. Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440);
  50. }
  51. return $token;
  52. }
  53. //查询设备列表
  54. public function queryDeviceList()
  55. {
  56. $url = '/pangu/sdkServer/device/queryDeviceList';
  57. $sOrgId = Input::get('sOrgId', '');
  58. $iDeviceTypes = Input::get('iDeviceTypes', [5]);
  59. $rootCodes = Input::get('rootCodes', ["BH-0001"]);
  60. $isOrgTree = Input::get('isOrgTree', true);
  61. $orgTypeIds = Input::get('orgTypeIds', []);
  62. $needPage = Input::get('needPage', true);
  63. $pageSize = Input::get('pageSize', 50);
  64. $currentPage = Input::get('currentPage', 1);
  65. $params = [
  66. 'sOrgId' => $sOrgId,
  67. 'iDeviceTypes' => $iDeviceTypes,
  68. 'rootCodes' => $rootCodes,
  69. 'isOrgTree' => $isOrgTree,
  70. 'orgTypeIds' => $orgTypeIds,
  71. 'needPage' => $needPage,
  72. 'pageSize' => $pageSize,
  73. 'currentPage' => $currentPage,
  74. ];
  75. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  76. $result = json_decode($result, true);
  77. return $result;
  78. }
  79. //查询设备rtsp流
  80. public function getRtspById()
  81. {
  82. $url = '/pangu/sdkServer/videoStreaming/getRtspById';
  83. $sId = Input::get('sId', []);
  84. $ip = Input::get('ip', '');
  85. $port = Input::get('port', '');
  86. $type = Input::get('type', 0);
  87. $params = [
  88. 'sId' => $sId,
  89. 'ip' => $ip,
  90. 'port' => $port,
  91. 'type' => $type
  92. ];
  93. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  94. $result = json_decode($result, true);
  95. return $result;
  96. }
  97. //查询区域列表
  98. public function getRegionByOrgId(){
  99. $url = '/pangu/sdkServer/OrgManager/queryOrgList';
  100. $parentOrgIds = Input::get('parentOrgIds', []);
  101. $params = [
  102. 'querySingleType' => true,
  103. 'needPage' => false,
  104. 'currentPage' => 1,
  105. 'pageSize' => 10,
  106. 'parentOrgIds' => $parentOrgIds,
  107. 'queryType' => 0,
  108. ];
  109. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  110. $result = json_decode($result, true);
  111. return $result;
  112. }
  113. //天地伟业获取区域列表
  114. public function saveTrees($regions, $parent_id)
  115. {
  116. $arr = [];
  117. if (empty($regions['content'])) {
  118. return [];
  119. }
  120. $mineService = new MineServices();
  121. $mineService->initMineList();
  122. $arr_count = $regions['count'];
  123. foreach ($regions['content'][0]['data'] as $key => $value) {
  124. $arr[$key]['parentOrgId'] = $value['parentOrgId'];
  125. $arr[$key]['orgId'] = $value['orgId'];
  126. $arr[$key]['name'] = str_replace('#', '号', $value['orgName']);
  127. $id = MineList::where('index_code', $value['orgId'])->value('id');
  128. $params = [
  129. 'id' => $id,
  130. 'parent_id' => $parent_id,
  131. 'title' => $value['orgName'],
  132. 'sort' => $arr_count - $key,
  133. 'index_code' => $value['orgId'],
  134. ];
  135. $result = $mineService->add($params);
  136. $mine_id = $result->id;
  137. Input::replace(
  138. [
  139. 'parentOrgIds' => $value['orgId'],
  140. 'url' => Input::get('url', ''),
  141. 'username' => Input::get('username', ''),
  142. 'password' => Input::get('password', ''),
  143. 'sysId' => 'PG',
  144. ]
  145. );
  146. $region_child = $this->getRegionByOrgId();
  147. $arr[$key]['children'] = self::saveTrees($region_child, $mine_id);
  148. if (count($arr[$key]['children']) == 0) {
  149. unset($arr[$key]['children']);
  150. }
  151. }
  152. return array_values($arr);
  153. }
  154. /**
  155. * 发送请求
  156. * @param string $url
  157. * @param string $postData
  158. * @return bool|string
  159. */
  160. public function curlPost($url = '', $postData = '')
  161. {
  162. if (is_array($postData)) {
  163. $postData = http_build_query($postData);
  164. }
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_URL, $url);
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  168. curl_setopt($ch, CURLOPT_POST, 1);
  169. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  170. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  171. curl_setopt_array($ch, array(
  172. CURLOPT_HTTPHEADER => array(
  173. "Accept:" . '*/*',
  174. "Content-Type:" . 'application/json',
  175. "token:" . $this->token,
  176. )
  177. ));
  178. //https请求 不验证证书和host
  179. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  180. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  181. $data = curl_exec($ch);
  182. curl_close($ch);
  183. return $data;
  184. }
  185. }