HaiKangController.php 10 KB

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