TdwyController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 3/17/22
  6. * Time: 7:01 PM
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Api\BaseController;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Input;
  14. use Modules\Camera\Enum\CameraEnum;
  15. use Modules\Mine\Entities\MineList;
  16. use Modules\Mine\Enum\MineEnum;
  17. use Modules\Mine\Services\MineServices;
  18. class TdwyController extends BaseController
  19. {
  20. protected $pre_url; // 请求地址
  21. protected $username; // 请求username
  22. protected $password; // 请求password
  23. protected $sysId; // 请求sysId
  24. protected $token; // 请求sysId
  25. public function __construct()
  26. {
  27. $this->pre_url = Input::get('url');
  28. $this->username = Input::get('username');
  29. $this->password = Input::get('password');
  30. $this->sysId = Input::get('sysId');
  31. //先获取token
  32. $this->token = $this->loginUser();
  33. }
  34. //登录获取token
  35. protected function loginUser()
  36. {
  37. $url = '/pangu/sdkServer/user/loginUser';
  38. $params = [
  39. 'username' => $this->username,
  40. 'password' => $this->password,
  41. 'sysId' => $this->sysId,
  42. ];
  43. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  44. $result = json_decode($result, true);
  45. $token = '';
  46. if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) {
  47. $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE);
  48. return $token;
  49. }
  50. if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) {
  51. $token = $result['content']['token'];
  52. Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440);
  53. }
  54. return $token;
  55. }
  56. //查询设备列表
  57. public function queryDeviceList()
  58. {
  59. $url = '/pangu/sdkServer/device/queryDeviceList';
  60. $sOrgId = Input::get('sOrgId', '');
  61. $iDeviceTypes = Input::get('iDeviceTypes', [5]);
  62. $rootCodes = Input::get('rootCodes', ["BH-0001"]);
  63. $isOrgTree = Input::get('isOrgTree', true);
  64. $orgTypeIds = Input::get('orgTypeIds', []);
  65. $needPage = Input::get('needPage', true);
  66. $pageSize = Input::get('pageSize', 50);
  67. $currentPage = Input::get('currentPage', 1);
  68. $params = [
  69. 'sOrgId' => $sOrgId,
  70. 'iDeviceTypes' => $iDeviceTypes,
  71. 'rootCodes' => $rootCodes,
  72. 'isOrgTree' => $isOrgTree,
  73. 'orgTypeIds' => $orgTypeIds,
  74. 'needPage' => $needPage,
  75. 'pageSize' => $pageSize,
  76. 'currentPage' => $currentPage,
  77. ];
  78. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  79. $result = json_decode($result, true);
  80. return $result;
  81. }
  82. //查询设备rtsp流
  83. public function getRtspById()
  84. {
  85. $url = '/pangu/sdkServer/videoStreaming/getRtspById';
  86. $sId = Input::get('sId', []);
  87. $ip = Input::get('ip', '');
  88. $port = Input::get('port', '');
  89. $type = Input::get('type', 0);
  90. $params = [
  91. 'sId' => $sId,
  92. 'ip' => $ip,
  93. 'port' => $port,
  94. 'type' => $type
  95. ];
  96. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  97. $result = json_decode($result, true);
  98. return $result;
  99. }
  100. //查询区域列表
  101. public function getRegionByOrgId(){
  102. $url = '/pangu/sdkServer/OrgManager/queryOrgList';
  103. $parentOrgIds = Input::get('parentOrgIds', []);
  104. $params = [
  105. 'querySingleType' => true,
  106. 'needPage' => false,
  107. 'currentPage' => 1,
  108. 'pageSize' => 10,
  109. 'parentOrgIds' => $parentOrgIds,
  110. 'queryType' => 0,
  111. ];
  112. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  113. $result = json_decode($result, true);
  114. return $result;
  115. }
  116. //天地伟业获取区域列表
  117. public function saveTrees($regions, $parent_id)
  118. {
  119. $arr = [];
  120. if (empty($regions['content'])) {
  121. return [];
  122. }
  123. $mineService = new MineServices();
  124. $mineService->initMineList();
  125. $arr_count = $regions['count'];
  126. foreach ($regions['content'][0]['data'] as $key => $value) {
  127. $arr[$key]['parentOrgId'] = $value['parentOrgId'];
  128. $arr[$key]['orgId'] = $value['orgId'];
  129. $arr[$key]['name'] = str_replace('#', '号', $value['orgName']);
  130. $id = MineList::where('index_code', $value['orgId'])->value('id');
  131. $params = [
  132. 'id' => $id,
  133. 'parent_id' => $parent_id,
  134. 'title' => $value['orgName'],
  135. 'sort' => $arr_count - $key,
  136. 'index_code' => $value['orgId'],
  137. ];
  138. $result = $mineService->add($params);
  139. $mine_id = $result->id;
  140. Input::replace(
  141. [
  142. 'parentOrgIds' => $value['orgId'],
  143. 'url' => Input::get('url', ''),
  144. 'username' => Input::get('username', ''),
  145. 'password' => Input::get('password', ''),
  146. 'sysId' => 'PG',
  147. ]
  148. );
  149. $region_child = $this->getRegionByOrgId();
  150. $arr[$key]['children'] = self::saveTrees($region_child, $mine_id);
  151. if (count($arr[$key]['children']) == 0) {
  152. unset($arr[$key]['children']);
  153. }
  154. }
  155. return array_values($arr);
  156. }
  157. /**
  158. * 发送请求
  159. * @param string $url
  160. * @param string $postData
  161. * @return bool|string
  162. */
  163. public function curlPost($url = '', $postData = '')
  164. {
  165. if (is_array($postData)) {
  166. $postData = http_build_query($postData);
  167. }
  168. $ch = curl_init();
  169. curl_setopt($ch, CURLOPT_URL, $url);
  170. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  171. curl_setopt($ch, CURLOPT_POST, 1);
  172. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  173. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  174. curl_setopt_array($ch, array(
  175. CURLOPT_HTTPHEADER => array(
  176. "Accept:" . '*/*',
  177. "Content-Type:" . 'application/json',
  178. "token:" . $this->token,
  179. )
  180. ));
  181. //https请求 不验证证书和host
  182. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  183. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  184. $data = curl_exec($ch);
  185. curl_close($ch);
  186. return $data;
  187. }
  188. //同步大华视频服务器摄像头
  189. public function dahuaCamera(){
  190. //大华需要同步的区域
  191. $dahua_ext = DB::table('mine_list_ext')
  192. ->where('is_hak',MineEnum::IS_HAK_DH)
  193. ->where('deleted_at',null)->get();
  194. if(count($dahua_ext) > 0){
  195. for($i=0;$i<count($dahua_ext);$i++){
  196. $ip = $dahua_ext[$i]->ip.':'.$dahua_ext[$i]->port;
  197. //获取public_key
  198. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  199. $result = $this->httpRequest($ip.$url);
  200. if($result['data']['publicKey']){
  201. $public_key = $result['data']['publicKey'];
  202. }else{
  203. return;
  204. }
  205. //获取access_token
  206. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  207. $username_password = explode('|',$dahua_ext[$i]->sOrgId);
  208. if(count($username_password) != 2){
  209. return;
  210. }
  211. $password = $this->rsaEncode($username_password[1],$public_key);
  212. $params = [
  213. 'grant_type'=>'password',
  214. 'username'=>$username_password[0],
  215. 'password'=>$password,
  216. 'client_id'=>$dahua_ext[$i]->key,
  217. 'client_secret'=>$dahua_ext[$i]->secret,
  218. 'public_key'=>$public_key
  219. ];
  220. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  221. if($result2['data']['access_token']){
  222. $access_token = $result2['data']['access_token'];
  223. }else{
  224. return;
  225. }
  226. //获取摄像头列表
  227. $url3 = '/evo-apigw/evo-brm/1.2.0/device/channel/subsystem/page';
  228. $params2 = [
  229. 'pageNum'=>1,
  230. 'pageSize'=>1000,
  231. 'sortType'=>'ASC',
  232. 'sort'=>'channelSn',
  233. ];
  234. $result3 = $this->httpRequest($ip.$url3,'post',$params2,$access_token);
  235. DB::table('camera_list')->where('mine_id',$dahua_ext[$i]->mine_id)->delete();
  236. if($result3['data']['pageData']){
  237. $camera_list = $result3['data']['pageData'];
  238. if(count($camera_list) > 0){
  239. for($j=0;$j<count($camera_list);$j++){
  240. $param['mine_id'] = $dahua_ext[$i]->mine_id;
  241. $param['revert_id'] = 'NullId';
  242. $param['sort'] = 1;
  243. $param['camera_name'] = $camera_list[$j]['channelName'];
  244. $param['camera_source'] = 2;
  245. $param['index_code'] = $camera_list[$j]['channelCode'];
  246. $param['video_recorder'] = 2;
  247. $param['created_at'] = date('Y-m-d H:i:s');
  248. $param['updated_at'] = date('Y-m-d H:i:s');
  249. DB::table('camera_list')->insert($param);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. //大华rtsp
  257. public function dahuaRtsp($camera_id,$parent_id){
  258. $dahua_ext = DB::table('mine_list_ext')
  259. ->where('mine_id',$parent_id)
  260. ->where('deleted_at',null)->get();
  261. if(count($dahua_ext) > 0){
  262. $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port;
  263. //获取public_key
  264. $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key';
  265. $result1 = $this->httpRequest($ip.$url);
  266. if($result1['data']['publicKey']){
  267. $public_key = $result1['data']['publicKey'];
  268. }else{
  269. return;
  270. }
  271. //获取access_token
  272. $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token';
  273. $username_password = explode('|',$dahua_ext[0]->sOrgId);
  274. if(count($username_password) != 2){
  275. return;
  276. }
  277. $password = $this->rsaEncode($username_password[1],$public_key);
  278. $params = [
  279. 'grant_type'=>'password',
  280. 'username'=>$username_password[0],
  281. 'password'=>$password,
  282. 'client_id'=>$dahua_ext[0]->key,
  283. 'client_secret'=>$dahua_ext[0]->secret,
  284. 'public_key'=>$public_key
  285. ];
  286. $result2 = $this->httpRequest($ip.$url2,'post',$params);
  287. if($result2['data']['access_token']){
  288. $access_token = $result2['data']['access_token'];
  289. }else{
  290. return;
  291. }
  292. $camera = DB::table('camera_list')->where('id',$camera_id)->get();
  293. $url4 = '/evo-apigw/admin/API/MTS/Video/StartVideo';
  294. $params3['data'] = [
  295. 'channelId'=>$camera[0]->index_code,
  296. 'dataType'=>'1',
  297. 'streamType'=>'1'
  298. ];
  299. $result4 = $this->httpRequest($ip.$url4,'post',$params3,$access_token);
  300. if($result4['data']['url'] && $result4['data']['token']){
  301. $rtsp = explode('|',$result4['data']['url']);
  302. if($rtsp[1]){
  303. $result['data'] = [
  304. 'camera_id' => $camera_id,
  305. 'url' => $rtsp[1].'?token='.$result4['data']['token']
  306. ];
  307. return $result;
  308. }
  309. }
  310. }
  311. }
  312. //接口第三方调用
  313. public function httpRequest($url, $format = 'get', $data = null, $token = null){
  314. //设置头信息
  315. $headerArray =array("Content-type:application/json;","Accept:application/json");
  316. if ($token) {
  317. $headerArray[] = "Authorization:bearer " . $token;
  318. }
  319. $curl=curl_init();
  320. curl_setopt($curl, CURLOPT_URL, $url);
  321. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  322. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  323. if ($format == 'post') {
  324. //post传值设置post传参
  325. curl_setopt($curl, CURLOPT_POST, 1);
  326. if ($data) {
  327. $data = json_encode($data);
  328. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  329. }
  330. }
  331. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  332. curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
  333. $data=json_decode(curl_exec($curl), true);
  334. curl_close($curl);
  335. //返回接口返回数据
  336. return $data;
  337. }
  338. public function rsaEncode($password,$rsa_public_key)
  339. {
  340. // 要执行的代码
  341. $rsa_public = "-----BEGIN PUBLIC KEY-----\n";
  342. $rsa_public = $rsa_public.$rsa_public_key;
  343. $rsa_public = $rsa_public."\n-----END PUBLIC KEY-----";
  344. $key = openssl_pkey_get_public($rsa_public);
  345. if (!$key) {
  346. echo "公钥不可用\n";
  347. echo $rsa_public;
  348. }
  349. //openssl_public_encrypt 第一个参数只能是string
  350. //openssl_public_encrypt 第二个参数是处理后的数据
  351. //openssl_public_encrypt 第三个参数是openssl_pkey_get_public返回的资源类型
  352. $return_en = openssl_public_encrypt($password, $crypted, $key);
  353. if (!$return_en) {
  354. echo "加密失败,请检查RSA秘钥";
  355. }
  356. return base64_encode($crypted);
  357. }
  358. }