| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- /**
- * Created by PhpStorm.
- * User: qiuzijian
- * Date: 3/17/22
- * Time: 7:01 PM
- */
- namespace Modules\Camera\Http\Controllers\Api;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Api\BaseController;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Input;
- use Modules\Camera\Enum\CameraEnum;
- use Modules\Mine\Entities\MineList;
- use Modules\Mine\Enum\MineEnum;
- use Modules\Mine\Services\MineServices;
- class TdwyController extends BaseController
- {
- protected $pre_url; // 请求地址
- protected $username; // 请求username
- protected $password; // 请求password
- protected $sysId; // 请求sysId
- protected $token; // 请求sysId
- public function __construct()
- {
- $this->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;$i<count($dahua_ext);$i++){
- $ip = $dahua_ext[$i]->ip.':'.$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($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;$j<count($camera_list);$j++){
- $param['mine_id'] = $dahua_ext[$i]->mine_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);
- }
- }
- }
- }
- }
- }
- //大华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]){
- return $rtsp[1].'?token='.$result4['data']['token'];
- }
- }
- }
- }
- //接口第三方调用
- 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);
- }
- }
|