pre_url = Input::get('url'); $this->username = Input::get('username'); $this->password = Input::get('password'); $this->sysId = Input::get('sysId'); //先获取token $this->token = $this->loginUser(); } //登录获取token protected function loginUser() { $url = '/pangu/sdkServer/user/loginUser'; $params = [ 'username' => $this->username, 'password' => $this->password, 'sysId' => $this->sysId, ]; $result = $this->curlPost($this->pre_url . $url, json_encode($params)); $result = json_decode($result, true); $token = ''; if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) { $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE); return $token; } if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) { $token = $result['content']['token']; Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440); } return $token; } //查询设备列表 public function queryDeviceList() { $url = '/pangu/sdkServer/device/queryDeviceList'; $sOrgId = Input::get('sOrgId', ''); $iDeviceTypes = Input::get('iDeviceTypes', [5]); $rootCodes = Input::get('rootCodes', ["BH-0001"]); $isOrgTree = Input::get('isOrgTree', true); $orgTypeIds = Input::get('orgTypeIds', []); $needPage = Input::get('needPage', true); $pageSize = Input::get('pageSize', 50); $currentPage = Input::get('currentPage', 1); $params = [ 'sOrgId' => $sOrgId, 'iDeviceTypes' => $iDeviceTypes, 'rootCodes' => $rootCodes, 'isOrgTree' => $isOrgTree, 'orgTypeIds' => $orgTypeIds, 'needPage' => $needPage, 'pageSize' => $pageSize, 'currentPage' => $currentPage, ]; $result = $this->curlPost($this->pre_url . $url, json_encode($params)); $result = json_decode($result, true); return $result; } //查询设备rtsp流 public function getRtspById() { $url = '/pangu/sdkServer/videoStreaming/getRtspById'; $sId = Input::get('sId', []); $ip = Input::get('ip', ''); $port = Input::get('port', ''); $type = Input::get('type', 0); $params = [ 'sId' => $sId, 'ip' => $ip, 'port' => $port, 'type' => $type ]; $result = $this->curlPost($this->pre_url . $url, json_encode($params)); $result = json_decode($result, true); return $result; } //查询区域列表 public function getRegionByOrgId(){ $url = '/pangu/sdkServer/OrgManager/queryOrgList'; $parentOrgIds = Input::get('parentOrgIds', []); $params = [ 'querySingleType' => true, 'needPage' => false, 'currentPage' => 1, 'pageSize' => 10, 'parentOrgIds' => $parentOrgIds, 'queryType' => 0, ]; $result = $this->curlPost($this->pre_url . $url, json_encode($params)); $result = json_decode($result, true); return $result; } //天地伟业获取区域列表 public function saveTrees($regions, $parent_id) { $arr = []; if (empty($regions['content'])) { return []; } $mineService = new MineServices(); $mineService->initMineList(); $arr_count = $regions['count']; foreach ($regions['content'][0]['data'] as $key => $value) { $arr[$key]['parentOrgId'] = $value['parentOrgId']; $arr[$key]['orgId'] = $value['orgId']; $arr[$key]['name'] = str_replace('#', '号', $value['orgName']); $id = MineList::where('index_code', $value['orgId'])->value('id'); $params = [ 'id' => $id, 'parent_id' => $parent_id, 'title' => $value['orgName'], 'sort' => $arr_count - $key, 'index_code' => $value['orgId'], ]; $result = $mineService->add($params); $mine_id = $result->id; Input::replace( [ 'parentOrgIds' => $value['orgId'], 'url' => Input::get('url', ''), 'username' => Input::get('username', ''), 'password' => Input::get('password', ''), 'sysId' => 'PG', ] ); $region_child = $this->getRegionByOrgId(); $arr[$key]['children'] = self::saveTrees($region_child, $mine_id); if (count($arr[$key]['children']) == 0) { unset($arr[$key]['children']); } } return array_values($arr); } /** * 发送请求 * @param string $url * @param string $postData * @return bool|string */ public function curlPost($url = '', $postData = '') { if (is_array($postData)) { $postData = http_build_query($postData); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数 curl_setopt_array($ch, array( CURLOPT_HTTPHEADER => array( "Accept:" . '*/*', "Content-Type:" . 'application/json', "token:" . $this->token, ) )); //https请求 不验证证书和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); curl_close($ch); return $data; } //同步大华视频服务器摄像头 public function dahuaCamera(){ //大华需要同步的区域 $dahua_ext = DB::table('mine_list_ext') ->where('is_hak',MineEnum::IS_HAK_DH) ->where('deleted_at',null)->get(); if(count($dahua_ext) > 0){ for($i=0;$iip.':'.$dahua_ext[$i]->port; //获取public_key $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key'; $result = $this->httpRequest($ip.$url); if($result['data']['publicKey']){ $public_key = $result['data']['publicKey']; }else{ return; } //获取access_token $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token'; $username_password = explode('|',$dahua_ext[$i]->sOrgId); if(count($username_password) != 2){ return; } $password = $this->rsaEncode($username_password[1],$public_key); $params = [ 'grant_type'=>'password', 'username'=>$username_password[0], 'password'=>$password, 'client_id'=>$dahua_ext[$i]->key, 'client_secret'=>$dahua_ext[$i]->secret, 'public_key'=>$public_key ]; $result2 = $this->httpRequest($ip.$url2,'post',$params); if(isset($result2['data']['access_token'])){ $access_token = $result2['data']['access_token']; }else{ return; } //获取摄像头列表 $url3 = '/evo-apigw/evo-brm/1.2.0/device/channel/subsystem/page'; $params2 = [ 'pageNum'=>1, 'pageSize'=>1000, 'sortType'=>'ASC', 'sort'=>'channelSn', ]; $result3 = $this->httpRequest($ip.$url3,'post',$params2,$access_token); // DB::table('camera_list')->where('mine_id',$dahua_ext[$i]->mine_id)->delete(); if($result3['data']['pageData']){ $camera_list = $result3['data']['pageData']; if(count($camera_list) > 0){ for($j=0;$jmine_id; $param['revert_id'] = 'NullId'; $param['sort'] = 1; $param['camera_name'] = $camera_list[$j]['channelName']; $param['camera_source'] = 2; $param['index_code'] = $camera_list[$j]['channelCode']; $param['video_recorder'] = 2; // $param['created_at'] = date('Y-m-d H:i:s'); // $param['updated_at'] = date('Y-m-d H:i:s'); // DB::table('camera_list')->insert($param); CameraList::updateOrCreate(['index_code' => $camera_list[$j]['channelCode']], $param); } } } } } } //大华rtsp public function dahuaRtsp($camera_id,$parent_id){ $dahua_ext = DB::table('mine_list_ext') ->where('mine_id',$parent_id) ->where('deleted_at',null)->get(); if(count($dahua_ext) > 0){ $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port; //获取public_key $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key'; $result1 = $this->httpRequest($ip.$url); if($result1['data']['publicKey']){ $public_key = $result1['data']['publicKey']; }else{ return; } //获取access_token $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token'; $username_password = explode('|',$dahua_ext[0]->sOrgId); if(count($username_password) != 2){ return; } $password = $this->rsaEncode($username_password[1],$public_key); $params = [ 'grant_type'=>'password', 'username'=>$username_password[0], 'password'=>$password, 'client_id'=>$dahua_ext[0]->key, 'client_secret'=>$dahua_ext[0]->secret, 'public_key'=>$public_key ]; $result2 = $this->httpRequest($ip.$url2,'post',$params); if($result2['data']['access_token']){ $access_token = $result2['data']['access_token']; }else{ return; } $camera = DB::table('camera_list')->where('id',$camera_id)->get(); $url4 = '/evo-apigw/admin/API/MTS/Video/StartVideo'; $params3['data'] = [ 'channelId'=>$camera[0]->index_code, 'dataType'=>'1', 'streamType'=>'1' ]; $result4 = $this->httpRequest($ip.$url4,'post',$params3,$access_token); if($result4['data']['url'] && $result4['data']['token']){ $rtsp = explode('|',$result4['data']['url']); if($rtsp[1]){ $result['data'] = [ 'camera_id' => $camera_id, 'url' => $rtsp[1].'?token='.$result4['data']['token'] ]; return $result; } } } } //大华停止语音对讲 public function talkStop($camera_id,$parent_id,$device_code,$session){ $dahua_ext = DB::table('mine_list_ext') ->where('mine_id',$parent_id) ->where('deleted_at',null)->get(); if(count($dahua_ext) > 0){ $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port; //获取public_key $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key'; $result1 = $this->httpRequest($ip.$url); if($result1['data']['publicKey']){ $public_key = $result1['data']['publicKey']; }else{ return; } //获取access_token $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token'; $username_password = explode('|',$dahua_ext[0]->sOrgId); if(count($username_password) != 2){ return; } $password = $this->rsaEncode($username_password[1],$public_key); $params = [ 'grant_type'=>'password', 'username'=>$username_password[0], 'password'=>$password, 'client_id'=>$dahua_ext[0]->key, 'client_secret'=>$dahua_ext[0]->secret, 'public_key'=>$public_key ]; $result2 = $this->httpRequest($ip.$url2,'post',$params); if($result2['data']['access_token']){ $access_token = $result2['data']['access_token']; }else{ return; } //语音对讲 $url5 = '/evo-apigw/admin/API/MTS/Audio/StopTalk'; $params4['data'] = [ "deviceCode"=>$device_code, "talkType"=>"1", "session"=>$session, "channelSeq"=>"" ]; $result = $this->httpRequest($ip.$url5,'post',$params4,$access_token); return $result; } } //大华语音对讲 public function talkUrl($camera_id,$parent_id){ $dahua_ext = DB::table('mine_list_ext') ->where('mine_id',$parent_id) ->where('deleted_at',null)->get(); if(count($dahua_ext) > 0){ $ip = $dahua_ext[0]->ip.':'.$dahua_ext[0]->port; //获取public_key $url = '/evo-apigw/evo-oauth/1.0.0/oauth/public-key'; $result1 = $this->httpRequest($ip.$url); if($result1['data']['publicKey']){ $public_key = $result1['data']['publicKey']; }else{ return; } //获取access_token $url2 = '/evo-apigw/evo-oauth/1.0.0/oauth/extend/token'; $username_password = explode('|',$dahua_ext[0]->sOrgId); if(count($username_password) != 2){ return; } $password = $this->rsaEncode($username_password[1],$public_key); $params = [ 'grant_type'=>'password', 'username'=>$username_password[0], 'password'=>$password, 'client_id'=>$dahua_ext[0]->key, 'client_secret'=>$dahua_ext[0]->secret, 'public_key'=>$public_key ]; $result2 = $this->httpRequest($ip.$url2,'post',$params); if($result2['data']['access_token']){ $access_token = $result2['data']['access_token']; }else{ return; } $camera = DB::table('camera_list')->where('id',$camera_id)->get(); $result['data']['camera_id'] = $camera_id; $result['data']['deviceCode'] = explode('$',$camera[0]->index_code)[0]; //语音对讲 $url5 = '/evo-apigw/admin/API/MTS/Audio/StartTalk'; $params4['data'] = [ "deviceCode"=>explode('$',$camera[0]->index_code)[0], "talkType"=>"1", "audioBit"=>"16", "audioType"=>"2", "broadcastChannels"=>"", "sampleRate"=>"8000", "talkmode"=>"", "channelSeq"=>"0" ]; $result5 = $this->httpRequest($ip.$url5,'post',$params4,$access_token); if(isset($result5['data']['url'])){ $result['data']['url'] = explode('|',$result5['data']['url'])[1].'?token='.$result5['data']['token']; $result['data']['session'] = $result5['data']['session']; $result['data']['id'] = $camera[0]->index_code; }else{ $result['msg'] = $result5['desc']; } return $result; } } //接口第三方调用 public function httpRequest($url, $format = 'get', $data = null, $token = null){ //设置头信息 $headerArray =array("Content-type:application/json;","Accept:application/json"); if ($token) { $headerArray[] = "Authorization:bearer " . $token; } $curl=curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if ($format == 'post') { //post传值设置post传参 curl_setopt($curl, CURLOPT_POST, 1); if ($data) { $data = json_encode($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); $data=json_decode(curl_exec($curl), true); curl_close($curl); //返回接口返回数据 return $data; } public function rsaEncode($password,$rsa_public_key) { // 要执行的代码 $rsa_public = "-----BEGIN PUBLIC KEY-----\n"; $rsa_public = $rsa_public.$rsa_public_key; $rsa_public = $rsa_public."\n-----END PUBLIC KEY-----"; $key = openssl_pkey_get_public($rsa_public); if (!$key) { echo "公钥不可用\n"; echo $rsa_public; } //openssl_public_encrypt 第一个参数只能是string //openssl_public_encrypt 第二个参数是处理后的数据 //openssl_public_encrypt 第三个参数是openssl_pkey_get_public返回的资源类型 $return_en = openssl_public_encrypt($password, $crypted, $key); if (!$return_en) { echo "加密失败,请检查RSA秘钥"; } return base64_encode($crypted); } }