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; } /** * 发送请求 * @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; } }