handler(); $tokenInfo = $tokenRedis->hget('third_token_info', 'swager'); } catch (\Exception $e) { // $this->errMsg = $e->getMessage(); // echo 'Message: ' .$e->getMessage(); $tokenInfo = Cache::get('third_token_info', ''); } $tokenInfo = json_decode( $tokenInfo, true ); if($tokenInfo && ( $tokenInfo['expiredAt'] / 1000 - time() ) > 30 ){ //存在并且过期时间距现在大于30秒 return $tokenInfo; } // 没有或过期了,调用接口获取 $url = self::API_URL_PREFIX . self::TOKEN_URL; $headers[] = "Content-Type: application/json"; $postData = [ 'username' => $this->username, 'password' => $this->password, ]; $output = $this->http_post($url, json_encode($postData), $headers); if($output === false){ // $this->errMsg = '获取Token失败'; return false; } $tokenRes = json_decode($output, true); $iserr = $tokenRes['error'] ?? false; $token = $tokenRes['token'] ?? ''; if($iserr || !$token){ // $this->errMsg = 'token 不存在'; self::pushCommonLog('thirdapi_gettoken_response_error', $tokenRes); return false; } if ($tokenRedis) { $tokenRedis->hset('third_token_info', 'swager', $output); } else { Cache::set('third_token_info', $output); } return $tokenRes; } /** * 注册设备到第三方 * @param $data ['deviceImei'=>'12345678','phone'=>''] */ public static function registerDevice($data){ $tokenInfo = (new static)->getTokenInfo(); if(!$tokenInfo){ return false; } $imei = self::imeiToSn($data['deviceImei']); if (!isset($imei)) { // static::$errMsg = '设备IMEI不能为空'; return false; } $postData = [ "deviceImei" => $imei, "phone" => $data['phone'] ?? '', "userId" => $tokenInfo['holder'] ]; $url = self::API_URL_PREFIX. "/device"; $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: ". $tokenInfo['token']; $output = (new static)->http_post($url, json_encode($postData), $headers); if($output === false){ // static::$errMsg = '注册设备失败'; return false; } $deviceRes = json_decode($output, true) ; if(array_key_exists("error",$deviceRes) || array_key_exists("code",$deviceRes)){ $response['request_imei'] = $imei; // static::$errMsg = '出错了'; self::pushCommonLog('thirdapi_register_device_response_error',$deviceRes); return false; } self::pushCommonLog('thirdapi_register_device_response_success',$deviceRes); return true; } /** * 获取设备列表 */ public static function getDeviceList(){ $tokenInfo = (new static)->getTokenInfo(); if(!$tokenInfo){ return false; } $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: ". $tokenInfo['token']; $url = self::API_URL_PREFIX. '/device?userId='.$tokenInfo['holder']; $response = (new static)->http_get($url, $headers); if (!$response) { self::pushCommonLog('thirdapi_devicelist_curl_error', $response); return false; } return json_decode($response, true); } /** * 获取第三方设备历史轨迹 */ public static function getHistoryLocations($imei, $option = null){ /* { "page": 0, "size": 6000, "total": 8, "content": [{ "collectDt": "2021-07-07 10:33:39", "receiveAt": "2021-07-07 10:33:39", "lat": "40.02577751048506", "lon": "116.35762812669113", "battery": 91, "positionType": 2, "imei": "71041607133", "isOnline": null }] } */ $imei = self::imeiToSn($imei); if (!$imei) { self::pushCommonLog('thirdapi_devicelocation_curl_error', 'imei 为空'); return false; } $tokenInfo = (new static)->getTokenInfo(); if(!$tokenInfo){ return false; } $pageNo = $option['pageNo'] ?? 0; $pageSize = $option['pageSize'] ?? 6000; $startTime = isset($option['startTime']) ? $option['startTime'] * 1000 : ''; $endTime = isset($option['endTime']) ? $option['endTime'] * 1000 : ''; $positionTypes = $option['positionTypes'] ?? ''; $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: ". $tokenInfo['token']; $url = self::API_URL_PREFIX. '/devicedata/location/filter?imei='.$imei.'&pageNo='.$pageNo.'&pageSize='.$pageSize.'&startTime='.$startTime.'&endTime='.$endTime.'&positionTypes='.$positionTypes; // var_dump($url); $response = (new static)->http_get($url, $headers); if (!$response) { self::pushCommonLog('thirdapi_devicelocation_curl_error', $response); return false; } return json_decode($response, true); } /** * 获取单个设备轨迹 */ public static function getDeviceLocation($imei){ /* { "page": 0, "size": 6000, "total": 8, "content": [{ "collectDt": "2021-07-07 10:33:39", "receiveAt": "2021-07-07 10:33:39", "lat": "40.02577751048506", "lon": "116.35762812669113", "battery": 91, "positionType": 2, "imei": "71041607133", "isOnline": null }] } */ if (!$imei) { self::pushCommonLog('thirdapi_devicelocation_curl_error', 'imei 为空'); return false; } $tokenInfo = (new static)->getTokenInfo(); if(!$tokenInfo){ return false; } $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: ". $tokenInfo['token']; $url = self::API_URL_PREFIX. '/devicedata/location/'.$imei; // var_dump($url); $response = (new static)->http_get($url, $headers); if (!$response) { self::pushCommonLog('getDeviceLocation', $response); return false; } return json_decode($response, true); } /** * 删除第三方设备 */ public static function deleteDevice($imei){ $tokenInfo = (new static)->getTokenInfo(); if(!$tokenInfo){ return false; } $headers[] = "Content-Type: application/json"; $headers[] = "Authorization: ". $tokenInfo['token']; $imei = self::imeiToSn($imei); $url = self::API_URL_PREFIX.'/device/'.$imei; $output = (new static)->http_delete($url, $headers); if(!$output){ return false; } $response = json_decode($output, true); if(array_key_exists("error",$response) || array_key_exists("code",$response)){ $response['request_imei'] = $imei; self::pushCommonLog('thirdapi_delete_device_response_error',$response); return false; } return $response; } /** * delete 请求 * @param string $url */ private function http_delete($url, $headers = []){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers ); curl_setopt($oCurl, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); if(intval($aStatus["http_code"])==200){ curl_close($oCurl); return $sContent; }else{ $this->pushCommonLog('thirdapi_token_curl_error',curl_error($oCurl)); curl_close($oCurl); return false; } } /** * GET 请求 * @param string $url */ public function http_get($url, $headers = []){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers ); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); if(intval($aStatus["http_code"])==200){ curl_close($oCurl); return $sContent; }else{ $this->pushCommonLog('thirdapi_token_curl_error',curl_error($oCurl)); curl_close($oCurl); return false; } } /** * POST 请求 * @param string $url * @param array $param * @return string content */ public function http_post($url, $param, $headers = []){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); } if (is_string($param)) { $strPOST = $param; } else { $aPOST = array(); foreach($param as $key=>$val){ $aPOST[] = $key."=".urlencode($val); } $strPOST = join("&", $aPOST); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_POST,true); curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers ); curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); if(intval($aStatus["http_code"])==200){ curl_close($oCurl); return $sContent; }else{ $this->pushCommonLog('thirdapi_post_error', curl_error($oCurl)); curl_close($oCurl); if($sContent){ $sContent .= '【RESPONSE_MSG】: '. $sContent .' || 【REQUEST_PARMS】:'.$strPOST; // static::$errMsg = '出错了'; self::pushCommonLog('thirdapi_register_device_response_error',$sContent); } return false; } } /** * 写入日志 */ public static function pushCommonLog( $filename = 'default', $msg) { $file = runtime_path() . '/log/' . date("Ymd", time()) . "/".$filename."log"; $folder = dirname($file); if (!is_dir($folder)) { mkdir($folder, 0777, true); } file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $msg . PHP_EOL, FILE_APPEND); } /** * IMEI 转 SN * 862571041604239 -> 71041604239 */ public static function imeiToSn($imei) { $imei = trim($imei); if (strlen($imei) <= 11) { return $imei; } return substr($imei, -11); } }