HaiKangController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. $area_list = [];
  67. if (isset($result['code']) && $result['code'] == 0) {
  68. $list = $result['data']['list'];
  69. foreach ($list as $key => $item) {
  70. $area_list[] = $item['indexCode'];
  71. if ($item['parentIndexCode'] == '-1') {
  72. unset($list[$key]);
  73. }
  74. }
  75. $result['data']['list'] = array_values($list);
  76. }
  77. if ($type == CameraEnum::REQUEST_TYPE_API) {
  78. $result['data']['list'] = $this->regionsTree($result['data']['list']);
  79. } else {
  80. //先删除不存在的区域
  81. if ($area_list) {
  82. MineList::where('degree', 'like', $mine_id . '|%')->whereNotIn('index_code', $area_list)->delete();
  83. }
  84. //更新区域
  85. $result['data']['list'] = $this->saveTree($result['data']['list'], $mine_id);
  86. }
  87. return $result;
  88. }
  89. protected function regionsTree($regions, $pid = 'root000000')
  90. {
  91. $arr = [];
  92. if (empty($regions)) {
  93. return [];
  94. }
  95. foreach ($regions as $key => $value) {
  96. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  97. $arr[$key]['indexCode'] = $value['indexCode'];
  98. $arr[$key]['mine_id'] = $value['indexCode'];
  99. $arr[$key]['name'] = $value['name'];
  100. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  101. $arr[$key]['treeCode'] = $value['treeCode'];
  102. $arr[$key]['children'] = array_values(self::regionsTree($regions, $value['indexCode']));
  103. unset($arr[$key]['indexCode']);
  104. unset($arr[$key]['parentIndexCode']);
  105. unset($arr[$key]['treeCode']);
  106. if (count($arr[$key]['children']) == 0) {
  107. unset($arr[$key]['children']);
  108. }
  109. }
  110. }
  111. return array_values($arr);
  112. }
  113. protected function saveTree($regions, $parent_id, $pid = 'root000000')
  114. {
  115. $arr = [];
  116. if (empty($regions)) {
  117. return [];
  118. }
  119. $mineService = new MineServices();
  120. $mineService->initMineList();
  121. $arr_count = count($regions);
  122. foreach ($regions as $key => $value) {
  123. if (isset($value['parentIndexCode']) && $value['parentIndexCode'] == $pid) {
  124. $arr[$key]['indexCode'] = $value['indexCode'];
  125. $arr[$key]['mine_id'] = $value['indexCode'];
  126. $arr[$key]['name'] = str_replace('#', '号', $value['name']);
  127. $arr[$key]['parentIndexCode'] = $value['parentIndexCode'];
  128. $arr[$key]['treeCode'] = $value['treeCode'];
  129. $id = MineList::where('index_code', $value['indexCode'])->value('id');
  130. $params = [
  131. 'id' => $id,
  132. 'parent_id' => $parent_id,
  133. 'title' => $value['name'],
  134. 'sort' => $arr_count - $key,
  135. 'index_code' => $value['indexCode'],
  136. ];
  137. $result = $mineService->add($params);
  138. $mine_id = $result->id;
  139. $arr[$key]['children'] = array_values(self::saveTree($regions, $mine_id, $value['indexCode']));
  140. if (count($arr[$key]['children']) == 0) {
  141. unset($arr[$key]['children']);
  142. }
  143. }
  144. }
  145. return array_values($arr);
  146. }
  147. /**
  148. * 获取区域监控列表
  149. * @return bool|mixed|string
  150. */
  151. public function getCamerasList()
  152. {
  153. if (!Input::has('indexCode')) {
  154. return $this->error(1, '缺少必要参数');
  155. }
  156. $url = $this->artemis . '/api/resource/v1/regions/regionIndexCode/cameras';
  157. //请求参数
  158. $params = [];
  159. $params['regionIndexCode'] = Input::get('indexCode');
  160. // $params['pageNo'] = $request->has('pageNo') ? intval($request->input('pageNo')) : 1;
  161. // $params['pageSize'] = $request->has('pageSize') ? intval($request->input('pageSize')) : 1000;
  162. $params['pageNo'] = 1;
  163. $params['pageSize'] = 1000;
  164. $this->sign = $this->get_sign($url);
  165. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  166. $result = json_decode($result, true);
  167. return $result;
  168. }
  169. /**
  170. * 获取摄像头码流url
  171. * @return array|mixed
  172. */
  173. public function getCamerasUrl()
  174. {
  175. if (!Input::has('cameraIndexCode')) {
  176. return $this->error(1, '缺少必要参数');
  177. }
  178. $protocol = Input::get('protocol', 'hls');
  179. $streamType = Input::get('streamType', 1);
  180. $url = $this->artemis . '/api/video/v1/cameras/previewURLs';
  181. // $cameras_info = json_decode($this->getCamerasInfo(Input::all()), true);
  182. //请求参数
  183. $params = [];
  184. // $params['regionIndexCode'] = $request->input('regionIndexCode');
  185. $params['cameraIndexCode'] = Input::get('cameraIndexCode');
  186. $params['streamType'] = $streamType; //0主码流 1子码流 2第三码流
  187. $params['protocol'] = $protocol;
  188. $params['transmode'] = 1; //0:UDP 1:TCP
  189. $params['expand'] = 'transcode=1&systemformat=ps&videotype=h264';
  190. $this->sign = $this->get_sign($url);
  191. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  192. $result = json_decode($result, true);
  193. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  194. $video_url = $result['data']['url'];
  195. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  196. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  197. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  198. }
  199. return $result;
  200. }
  201. /**
  202. * 获取摄像头详情
  203. * @param array $data
  204. * @return array|bool|string
  205. */
  206. public function getCamerasInfo($data)
  207. {
  208. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  209. //请求参数
  210. $params = [];
  211. $params['cameraIndexCode'] = $data['cameraIndexCode'];
  212. $this->sign = $this->get_sign($url);
  213. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  214. return $result;
  215. }
  216. //获取编码设备信息
  217. public function getTranscodeInfo()
  218. {
  219. $url = $this->artemis . '/api/resource/v1/encodeDevice/get';
  220. //请求参数
  221. $params = [];
  222. $params['pageNo'] = 1;
  223. $params['pageSize'] = 1;
  224. $this->sign = $this->get_sign($url);
  225. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  226. return $result;
  227. }
  228. //根据区域获取下级编码设备列表
  229. public function getTest()
  230. {
  231. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  232. //请求参数
  233. $params = [];
  234. $params['pageNo'] = 1;
  235. $params['pageSize'] = 1000;
  236. $params['regionIndexCode'] = Input::get('indexCode');
  237. $this->sign = $this->get_sign($url);
  238. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  239. $result = json_decode($result, true);
  240. return $result;
  241. }
  242. //获取编码设备列表
  243. public function getTranscodeList()
  244. {
  245. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  246. //请求参数
  247. $params['regionIndexCode'] = 'root000000';
  248. $params['pageNo'] = 1;
  249. $params['pageSize'] = 100;
  250. $this->sign = $this->get_sign($url);
  251. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  252. $result = json_decode($result, true);
  253. return $result;
  254. }
  255. public function getRegionsInfo()
  256. {
  257. $url = $this->artemis . '/api/resource/v1/region/regionCatalog/regionInfo';
  258. //请求参数
  259. $params = [];
  260. $indexCodes = Input::get('indexCodes');
  261. $params['indexCodes'] = $indexCodes;
  262. $this->sign = $this->get_sign($url);
  263. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  264. $result = json_decode($result, true);
  265. // if (isset($result['code']) && $result['code'] == 0) {
  266. // }
  267. return $result;
  268. }
  269. /**
  270. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  271. */
  272. protected function get_sign($url)
  273. {
  274. $sign_str = $this->get_sign_str($url); //签名字符串
  275. $priKey = $this->app_secret;
  276. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  277. $result = base64_encode($sign);
  278. return $result;
  279. }
  280. /**
  281. * 生成签名
  282. * @param $url
  283. * @return string
  284. */
  285. protected function get_sign_str($url)
  286. {
  287. // $next = "\n";
  288. $next = "\n";
  289. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  290. $str .= "x-ca-key:" . $this->app_key . $next;
  291. $str .= "x-ca-timestamp:" . $this->time . $next;
  292. $str .= $url;
  293. return $str;
  294. }
  295. /**
  296. * 发送请求
  297. * @param string $url
  298. * @param string $postData
  299. * @param array $options
  300. * @return bool|string
  301. */
  302. public function curlPost($url = '', $postData = '', $options = array())
  303. {
  304. if (is_array($postData)) {
  305. $postData = http_build_query($postData);
  306. }
  307. $ch = curl_init();
  308. curl_setopt($ch, CURLOPT_URL, $url);
  309. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  310. curl_setopt($ch, CURLOPT_POST, 1);
  311. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  312. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  313. curl_setopt_array($ch, array(
  314. CURLOPT_HTTPHEADER => array(
  315. "Accept:" . '*/*',
  316. "Content-Type:" . 'application/json',
  317. "x-Ca-Key:" . $this->app_key,
  318. "X-Ca-Signature:" . $this->sign,
  319. "X-Ca-Timestamp:" . $this->time,
  320. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  321. )
  322. ));
  323. //https请求 不验证证书和host
  324. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  325. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  326. $data = curl_exec($ch);
  327. curl_close($ch);
  328. return $data;
  329. }
  330. }