HaiKangController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. //先删除不存在的区域
  79. $area_list = [];
  80. foreach ($result['data']['list'] as $k => $v) {
  81. $area_list[] = $v['indexCode'];
  82. }
  83. MineList::where('degree', 'like', $mine_id . '|%')->whereNotIn('index_code', $area_list)->delete();
  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['streamform'] = 'ps';
  190. // $parmas['expand'] = 'transcode=1&videotype=h264';
  191. // if ($cameras_info['data']['channelType'] == 'analog') {
  192. // $params['expand'] = 'transcode=1&videotype=h264';
  193. // }
  194. $this->sign = $this->get_sign($url);
  195. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  196. $result = json_decode($result, true);
  197. if (isset($result['code']) && $result['code'] == 0) { // 将内网地址替换为外网地址
  198. $video_url = $result['data']['url'];
  199. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $video_url, $res); // 提取内网ip
  200. preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $this->pre_url, $res_n); // 提取外网ip
  201. $result['data']['url'] = str_replace($res, $res_n, $video_url);
  202. }
  203. return $result;
  204. }
  205. /**
  206. * 获取摄像头详情
  207. * @param array $data
  208. * @return array|bool|string
  209. */
  210. public function getCamerasInfo($data)
  211. {
  212. $url = $this->artemis . '/api/resource/v1/cameras/indexCode';
  213. //请求参数
  214. $params = [];
  215. $params['cameraIndexCode'] = $data['cameraIndexCode'];
  216. $this->sign = $this->get_sign($url);
  217. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  218. return $result;
  219. }
  220. //获取编码设备信息
  221. public function getTranscodeInfo()
  222. {
  223. $url = $this->artemis . '/api/resource/v1/encodeDevice/get';
  224. //请求参数
  225. $params = [];
  226. $params['pageNo'] = 1;
  227. $params['pageSize'] = 1;
  228. $this->sign = $this->get_sign($url);
  229. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  230. return $result;
  231. }
  232. //根据区域获取下级编码设备列表
  233. public function getTest()
  234. {
  235. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  236. //请求参数
  237. $params = [];
  238. $params['pageNo'] = 1;
  239. $params['pageSize'] = 1000;
  240. $params['regionIndexCode'] = Input::get('indexCode');
  241. $this->sign = $this->get_sign($url);
  242. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  243. $result = json_decode($result, true);
  244. return $result;
  245. }
  246. //获取编码设备列表
  247. public function getTranscodeList()
  248. {
  249. $url = $this->artemis . '/api/resource/v1/encodeDevice/subResources';
  250. //请求参数
  251. $params['regionIndexCode'] = 'root000000';
  252. $params['pageNo'] = 1;
  253. $params['pageSize'] = 100;
  254. $this->sign = $this->get_sign($url);
  255. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  256. $result = json_decode($result, true);
  257. return $result;
  258. }
  259. public function getRegionsInfo()
  260. {
  261. $url = $this->artemis . '/api/resource/v1/region/regionCatalog/regionInfo';
  262. //请求参数
  263. $params = [];
  264. $indexCodes = Input::get('indexCodes');
  265. $params['indexCodes'] = $indexCodes;
  266. $this->sign = $this->get_sign($url);
  267. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  268. $result = json_decode($result, true);
  269. // if (isset($result['code']) && $result['code'] == 0) {
  270. // }
  271. return $result;
  272. }
  273. /**
  274. * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
  275. */
  276. protected function get_sign($url)
  277. {
  278. $sign_str = $this->get_sign_str($url); //签名字符串
  279. $priKey = $this->app_secret;
  280. $sign = hash_hmac('sha256', $sign_str, $priKey, true); //生成消息摘要
  281. $result = base64_encode($sign);
  282. return $result;
  283. }
  284. /**
  285. * 生成签名
  286. * @param $url
  287. * @return string
  288. */
  289. protected function get_sign_str($url)
  290. {
  291. // $next = "\n";
  292. $next = "\n";
  293. $str = "POST" . $next . '*/*' . $next . 'application/json' . $next;
  294. $str .= "x-ca-key:" . $this->app_key . $next;
  295. $str .= "x-ca-timestamp:" . $this->time . $next;
  296. $str .= $url;
  297. return $str;
  298. }
  299. /**
  300. * 发送请求
  301. * @param string $url
  302. * @param string $postData
  303. * @param array $options
  304. * @return bool|string
  305. */
  306. public function curlPost($url = '', $postData = '', $options = array())
  307. {
  308. if (is_array($postData)) {
  309. $postData = http_build_query($postData);
  310. }
  311. $ch = curl_init();
  312. curl_setopt($ch, CURLOPT_URL, $url);
  313. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  314. curl_setopt($ch, CURLOPT_POST, 1);
  315. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  316. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  317. curl_setopt_array($ch, array(
  318. CURLOPT_HTTPHEADER => array(
  319. "Accept:" . '*/*',
  320. "Content-Type:" . 'application/json',
  321. "x-Ca-Key:" . $this->app_key,
  322. "X-Ca-Signature:" . $this->sign,
  323. "X-Ca-Timestamp:" . $this->time,
  324. "X-Ca-Signature-Headers:" . "x-ca-key,x-ca-timestamp",
  325. )
  326. ));
  327. //https请求 不验证证书和host
  328. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  329. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  330. $data = curl_exec($ch);
  331. curl_close($ch);
  332. return $data;
  333. }
  334. }