post('/api/deviceFlow/v1/list', [ 'device_id' => $deviceId, 'start_time' => $start->unix() * 1000, 'end_time' => $end->unix() * 1000, 'pageable' => [ 'page' => $page - 1, 'size' => $perPage, ], ]); return $result['data']; } /** * @param string $url * @param array $data * @return array */ public function post(string $url, array $data = []): array { return $this->request('POST', $url, [ 'json' => $data, ]); } /** * @param string $method * @param string $url * @param array $options * @return array * * @throws \Illuminate\Http\Client\RequestException * @throws \RuntimeException */ public function request(string $method, string $url, array $options = []): array { $nonce = $this->nonce(); $timestamp = now()->getTimestampMs(); /** @var \Illuminate\Http\Client\Response */ $response = Http::withHeaders([ 'Content-Type' => 'application/json', 'api-key' => $this->apiKey, 'Nonce' => $nonce, 'Timestamp' => $timestamp, 'Signature' => $this->sign(compact('nonce', 'timestamp')), ])->baseUrl(self::ENDPOINT_URL)->send($method, $url, $options); $result = $response->throw()->json(); if (data_get($result, 'success') !== true) { throw new RuntimeException(data_get($result, 'msg', '出错啦!')); } return $result; } /** * @param array $data * @return string */ protected function sign(array $data): string { return sha1( sprintf( '%s%s%s', $data['nonce'] ?? '', $data['timestamp'] ?? '', $this->apiSecret ) ); } protected function nonce(): string { $nonce = ''; for ($i = 0; $i < 8; $i++) { $nonce .= mt_rand(0, 9); } return $nonce; } }