HaiKangController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 2021-04-28
  6. * Time: 20:59
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use App\Http\Controllers\Api\BaseController;
  10. use Illuminate\Http\Request;
  11. class HaiKangController extends BaseController
  12. {
  13. protected $pre_url; // 海康接口地址
  14. protected $app_key; // 海康app_key
  15. protected $app_secret; // 海康app_secret
  16. protected $sign;// 签名
  17. protected $time;// 时间戳
  18. protected $artemis;// OpenAPI接口的上下文
  19. public function __construct(Request $request)
  20. {
  21. // if ($request->has('mine')) {
  22. // $this->pre_url = config('haikang.' . $request->input('mine') . '.pre_url'); // https://120.253.79.51:4433
  23. // $this->app_key = config('haikang.' . $request->input('mine') . '.app_key'); // 25720460
  24. // $this->app_secret = config('haikang.' . $request->input('mine') . '.app_secret'); // qqP7NLcIDwO9MgtYmp8L
  25. // $this->artemis = config('haikang.' . $request->input('mine') . '.artemis'); // /artemis
  26. // } else {
  27. // $this->pre_url = env('HAI_KANG_URL'); // https://120.253.79.51:4433
  28. // $this->app_key = env('HAI_KANG_APP_KEY'); // 25720460
  29. // $this->app_secret = env('HAI_KANG_APP_SECRET'); // qqP7NLcIDwO9MgtYmp8L
  30. // $this->artemis = env('HAI_KANG_ARTEMIS_PATH'); // /artemis
  31. // }
  32. $this->pre_url = 'https://10.71.252.64:4433'; // https://120.253.79.51:4433
  33. $this->app_key = '25720460'; // 25720460
  34. $this->app_secret = 'qqP7NLcIDwO9MgtYmp8L'; // qqP7NLcIDwO9MgtYmp8L
  35. $this->artemis = '/artemis'; // /artemis
  36. // $this->pre_url = 'https://120.253.79.51:4433'; // https://120.253.79.51:4433
  37. // $this->app_key = '25720460'; // 25720460
  38. // $this->app_secret = 'qqP7NLcIDwO9MgtYmp8L'; // qqP7NLcIDwO9MgtYmp8L
  39. // $this->artemis = '/artemis'; // /artemis
  40. list($msec, $sec) = explode(' ', microtime());
  41. $this->time = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  42. }
  43. /**
  44. * 获取区域列表
  45. * @param Request $request
  46. * @return bool|mixed|string
  47. */
  48. public function getRegionsList(Request $request)
  49. {
  50. $url = $this->artemis . '/api/resource/v1/regions';
  51. //请求参数
  52. $params = [];
  53. $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  54. $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  55. $this->sign = $this->get_sign($url);
  56. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  57. $result = json_decode($result, true);
  58. if (isset($result['code']) && $result['code'] == 0) {
  59. $list = $result['data']['list'];
  60. foreach ($list as $key => $item) {
  61. if ($item['parentIndexCode'] == '-1') {
  62. unset($list[$key]);
  63. }
  64. }
  65. $result['data']['list'] = array_values($list);
  66. }
  67. if ($request->has('type') && $request->input('type') == 'has_sub') {
  68. $result['data']['list'] = $this->regionsTree($result['data']['list']);
  69. }
  70. return $result;
  71. }
  72. protected function regionsTree($regions, $pid = 'root000000')
  73. {
  74. $arr = [];
  75. if (empty($regions)) {
  76. return [];
  77. }
  78. foreach ($regions as $key => $value) {
  79. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  80. $arr[$key]['indexCode'] = $value['indexCode'];
  81. $arr[$key]['name'] = $value['name'];
  82. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  83. $arr[$key]['treeCode'] = $value['treeCode'];
  84. $arr[$key]['children'] = array_values(self::regionsTree($regions, $value['indexCode']));
  85. // if (count($arr[$key]['children']) == 0) {
  86. // unset($arr[$key]);
  87. // }
  88. }
  89. }
  90. return array_values($arr);
  91. }
  92. /**
  93. * 获取区域监控列表
  94. * @param Request $request
  95. * @return bool|mixed|string
  96. */
  97. public function getCamerasList(Request $request)
  98. {
  99. if (!$request->has('indexCode')) {
  100. return $this->error(1, '缺少必要参数');
  101. }
  102. $url = $this->artemis . '/api/resource/v1/regions/regionIndexCode/cameras';
  103. //请求参数
  104. $params = [];
  105. $params['regionIndexCode'] = $request->input('indexCode');
  106. $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  107. $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  108. $this->sign = $this->get_sign($url);
  109. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  110. $result = json_decode($result, true);
  111. return $result;
  112. }
  113. /**
  114. * 获取摄像头码流url
  115. * @param Request $request
  116. * @return array|mixed
  117. */
  118. public function getCamerasUrl(Request $request)
  119. {
  120. if (!$request->has('cameraIndexCode')) {
  121. return $this->error(1, '缺少必要参数');
  122. }
  123. $url = $this->artemis . '/api/video/v1/cameras/previewURLs';
  124. $cameras_info = json_decode($this->getCamerasInfo($request), true);
  125. //请求参数
  126. $params = [];
  127. // $params['regionIndexCode'] = $request->input('regionIndexCode');
  128. $params['cameraIndexCode'] = $request->input('cameraIndexCode');
  129. $params['streamType'] = 0;
  130. $params['protocol'] = 'rtsp';
  131. $params['transmode'] = 1;
  132. if ($cameras_info['data']['channelType'] == 'analog') {
  133. $params['expand'] = 'transcode=1&videotype=h264';
  134. }
  135. $this->sign = $this->get_sign($url);
  136. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  137. $result = json_decode($result, true);
  138. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  139. $video_url = $result['data']['url'];
  140. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  141. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  142. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  143. }
  144. return $result;
  145. }
  146. /**
  147. * 获取摄像头详情
  148. * @param Request $request
  149. * @return array|bool|string
  150. */
  151. public function getCamerasInfo(Request $request)
  152. {
  153. if (!$request->has('cameraIndexCode')) {
  154. return $this->error(1, '缺少必要参数');
  155. }
  156. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  157. //请求参数
  158. $params = [];
  159. $params['cameraIndexCode'] = $request->input('cameraIndexCode');
  160. $this->sign = $this->get_sign($url);
  161. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  162. return $result;
  163. }
  164. /**
  165. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  166. */
  167. protected function get_sign($url)
  168. {
  169. $sign_str = $this->get_sign_str($url); //签名字符串
  170. $priKey = $this->app_secret;
  171. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  172. $result = base64_encode($sign);
  173. return $result;
  174. }
  175. /**
  176. * 生成签名
  177. * @param $url
  178. * @return string
  179. */
  180. protected function get_sign_str($url)
  181. {
  182. // $next = "\n";
  183. $next = "\n";
  184. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  185. $str .= "x-ca-key:" . $this->app_key . $next;
  186. $str .= "x-ca-timestamp:" . $this->time . $next;
  187. $str .= $url;
  188. return $str;
  189. }
  190. /**
  191. * 发送请求
  192. * @param string $url
  193. * @param string $postData
  194. * @param array $options
  195. * @return bool|string
  196. */
  197. public function curlPost($url = '', $postData = '', $options = array())
  198. {
  199. if (is_array($postData)) {
  200. $postData = http_build_query($postData);
  201. }
  202. $ch = curl_init();
  203. curl_setopt($ch, CURLOPT_URL, $url);
  204. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  205. curl_setopt($ch, CURLOPT_POST, 1);
  206. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  207. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  208. curl_setopt_array($ch, array(
  209. CURLOPT_HTTPHEADER => array(
  210. "Accept:" . '*/*',
  211. "Content-Type:" . 'application/json',
  212. "x-Ca-Key:" . $this->app_key,
  213. "X-Ca-Signature:" . $this->sign,
  214. "X-Ca-Timestamp:" . $this->time,
  215. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  216. )
  217. ));
  218. //https请求 不验证证书和host
  219. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  220. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  221. $data = curl_exec($ch);
  222. curl_close($ch);
  223. return $data;
  224. }
  225. }