HaiKangController.php 11 KB

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