HaiKangController.php 12 KB

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