HaiKangController.php 12 KB

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