HaiKangController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $url = $this->artemis . '/api/video/v1/cameras/previewURLs';
  131. $cameras_info = json_decode($this->getCamerasInfo(Input::all()), true);
  132. //请求参数
  133. $params = [];
  134. // $params['regionIndexCode'] = $request->input('regionIndexCode');
  135. $params['cameraIndexCode'] = Input::get('cameraIndexCode');
  136. $params['streamType'] = 1; //0主码流 1子码流 2第三码流
  137. $params['protocol'] = 'hls';
  138. $params['transmode'] = 1; //0:UDP 1:TCP
  139. $params['streamform'] = 'ps';
  140. // $parmas['expand'] = 'transcode=1&videotype=h264';
  141. if ($cameras_info['data']['channelType'] == 'analog') {
  142. $params['expand'] = 'transcode=1&videotype=h264';
  143. }
  144. $this->sign = $this->get_sign($url);
  145. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  146. $result = json_decode($result, true);
  147. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  148. $video_url = $result['data']['url'];
  149. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  150. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  151. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  152. }
  153. return $result;
  154. }
  155. /**
  156. * 获取摄像头详情
  157. * @param array $data
  158. * @return array|bool|string
  159. */
  160. public function getCamerasInfo($data)
  161. {
  162. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  163. //请求参数
  164. $params = [];
  165. $params['cameraIndexCode'] = $data['cameraIndexCode'];
  166. $this->sign = $this->get_sign($url);
  167. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  168. return $result;
  169. }
  170. //获取编码设备信息
  171. public function getTranscodeInfo()
  172. {
  173. $url = $this->artemis . '/api/resource/v1/encodeDevice/get';
  174. //请求参数
  175. $params = [];
  176. $params['pageNo'] = 1;
  177. $params['pageSize'] = 1;
  178. $this->sign = $this->get_sign($url);
  179. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  180. return $result;
  181. }
  182. /**
  183. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  184. */
  185. protected function get_sign($url)
  186. {
  187. $sign_str = $this->get_sign_str($url); //签名字符串
  188. $priKey = $this->app_secret;
  189. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  190. $result = base64_encode($sign);
  191. return $result;
  192. }
  193. /**
  194. * 生成签名
  195. * @param $url
  196. * @return string
  197. */
  198. protected function get_sign_str($url)
  199. {
  200. // $next = "\n";
  201. $next = "\n";
  202. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  203. $str .= "x-ca-key:" . $this->app_key . $next;
  204. $str .= "x-ca-timestamp:" . $this->time . $next;
  205. $str .= $url;
  206. return $str;
  207. }
  208. /**
  209. * 发送请求
  210. * @param string $url
  211. * @param string $postData
  212. * @param array $options
  213. * @return bool|string
  214. */
  215. public function curlPost($url = '', $postData = '', $options = array())
  216. {
  217. if (is_array($postData)) {
  218. $postData = http_build_query($postData);
  219. }
  220. $ch = curl_init();
  221. curl_setopt($ch, CURLOPT_URL, $url);
  222. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  223. curl_setopt($ch, CURLOPT_POST, 1);
  224. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  225. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  226. curl_setopt_array($ch, array(
  227. CURLOPT_HTTPHEADER => array(
  228. "Accept:" . '*/*',
  229. "Content-Type:" . 'application/json',
  230. "x-Ca-Key:" . $this->app_key,
  231. "X-Ca-Signature:" . $this->sign,
  232. "X-Ca-Timestamp:" . $this->time,
  233. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  234. )
  235. ));
  236. //https请求 不验证证书和host
  237. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  238. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  239. $data = curl_exec($ch);
  240. curl_close($ch);
  241. return $data;
  242. }
  243. }