HaiKangController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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(Request $request)
  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. * @param Request $request
  48. * @return bool|mixed|string
  49. */
  50. public function getRegionsList(Request $request)
  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. $this->sign = $this->get_sign($url);
  58. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  59. $result = json_decode($result, true);
  60. if (isset($result['code']) && $result['code'] == 0) {
  61. $list = $result['data']['list'];
  62. foreach ($list as $key => $item) {
  63. if ($item['parentIndexCode'] == '-1') {
  64. unset($list[$key]);
  65. }
  66. }
  67. $result['data']['list'] = array_values($list);
  68. }
  69. // if ($request->has('type') && $request->input('type') == 'has_sub') {
  70. $result['data']['list'] = $this->regionsTree($result['data']['list']);
  71. // }
  72. return $result;
  73. }
  74. protected function regionsTree($regions, $pid = 'root000000')
  75. {
  76. $arr = [];
  77. if (empty($regions)) {
  78. return [];
  79. }
  80. foreach ($regions as $key => $value) {
  81. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  82. $arr[$key]['indexCode'] = $value['indexCode'];
  83. $arr[$key]['mine_id'] = $value['indexCode'];
  84. $arr[$key]['name'] = $value['name'];
  85. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  86. $arr[$key]['treeCode'] = $value['treeCode'];
  87. $arr[$key]['children'] = array_values(self::regionsTree($regions, $value['indexCode']));
  88. unset($arr[$key]['indexCode']);
  89. unset($arr[$key]['parentIndexCode']);
  90. unset($arr[$key]['treeCode']);
  91. if (count($arr[$key]['children']) == 0) {
  92. unset($arr[$key]['children']);
  93. }
  94. }
  95. }
  96. return array_values($arr);
  97. }
  98. /**
  99. * 获取区域监控列表
  100. * @param Request $request
  101. * @return bool|mixed|string
  102. */
  103. public function getCamerasList(Request $request)
  104. {
  105. if (!$request->has('indexCode')) {
  106. return $this->error(1, '缺少必要参数');
  107. }
  108. $url = $this->artemis . '/api/resource/v1/regions/regionIndexCode/cameras';
  109. //请求参数
  110. $params = [];
  111. $params['regionIndexCode'] = $request->input('indexCode');
  112. $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  113. $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  114. $this->sign = $this->get_sign($url);
  115. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  116. $result = json_decode($result, true);
  117. return $result;
  118. }
  119. /**
  120. * 获取摄像头码流url
  121. * @param Request $request
  122. * @return array|mixed
  123. */
  124. public function getCamerasUrl(Request $request)
  125. {
  126. if (!$request->has('cameraIndexCode')) {
  127. return $this->error(1, '缺少必要参数');
  128. }
  129. $url = $this->artemis . '/api/video/v1/cameras/previewURLs';
  130. $cameras_info = json_decode($this->getCamerasInfo($request), true);
  131. //请求参数
  132. $params = [];
  133. // $params['regionIndexCode'] = $request->input('regionIndexCode');
  134. $params['cameraIndexCode'] = $request->input('cameraIndexCode');
  135. $params['streamType'] = 1;
  136. $params['protocol'] = 'hls';
  137. $params['transmode'] = 1;
  138. $params['streamform'] = 'ps';
  139. if ($cameras_info['data']['channelType'] == 'analog') {
  140. $params['expand'] = 'transcode=1&videotype=h264';
  141. }
  142. $this->sign = $this->get_sign($url);
  143. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  144. $result = json_decode($result, true);
  145. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  146. $video_url = $result['data']['url'];
  147. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  148. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  149. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  150. }
  151. return $result;
  152. }
  153. /**
  154. * 获取摄像头详情
  155. * @param Request $request
  156. * @return array|bool|string
  157. */
  158. public function getCamerasInfo(Request $request)
  159. {
  160. if (!$request->has('cameraIndexCode')) {
  161. return $this->error(1, '缺少必要参数');
  162. }
  163. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  164. //请求参数
  165. $params = [];
  166. $params['cameraIndexCode'] = $request->input('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. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  173. */
  174. protected function get_sign($url)
  175. {
  176. $sign_str = $this->get_sign_str($url); //签名字符串
  177. $priKey = $this->app_secret;
  178. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  179. $result = base64_encode($sign);
  180. return $result;
  181. }
  182. /**
  183. * 生成签名
  184. * @param $url
  185. * @return string
  186. */
  187. protected function get_sign_str($url)
  188. {
  189. // $next = "\n";
  190. $next = "\n";
  191. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  192. $str .= "x-ca-key:" . $this->app_key . $next;
  193. $str .= "x-ca-timestamp:" . $this->time . $next;
  194. $str .= $url;
  195. return $str;
  196. }
  197. /**
  198. * 发送请求
  199. * @param string $url
  200. * @param string $postData
  201. * @param array $options
  202. * @return bool|string
  203. */
  204. public function curlPost($url = '', $postData = '', $options = array())
  205. {
  206. if (is_array($postData)) {
  207. $postData = http_build_query($postData);
  208. }
  209. $ch = curl_init();
  210. curl_setopt($ch, CURLOPT_URL, $url);
  211. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  212. curl_setopt($ch, CURLOPT_POST, 1);
  213. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  214. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  215. curl_setopt_array($ch, array(
  216. CURLOPT_HTTPHEADER => array(
  217. "Accept:" . '*/*',
  218. "Content-Type:" . 'application/json',
  219. "x-Ca-Key:" . $this->app_key,
  220. "X-Ca-Signature:" . $this->sign,
  221. "X-Ca-Timestamp:" . $this->time,
  222. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  223. )
  224. ));
  225. //https请求 不验证证书和host
  226. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  227. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  228. $data = curl_exec($ch);
  229. curl_close($ch);
  230. return $data;
  231. }
  232. }