environment('local')) { $model->api_token = Str::random(60); } }); } // public function moduleHostFunctions($host_id, $func, $requests): array // { // $http = Http::module($this->api_token, $this->url); // $response = $http->post("hosts/{$host_id}/functions/" . $func, $requests); // // return $this->getResponse($response); // } // post, get, patch, delete 等请求 public function remote($func, $requests): array { $http = Http::module($this->api_token, $this->url); $response = $http->post('functions/' . $func, $requests); return $this->getResponse($response); } public function request($method, $path, $requests): array { $user = auth()->user(); $http = Http::module($this->api_token, $this->url); $requests['user_id'] = $user->id; if ($method == 'post') { // add user to requests $requests['user'] = $user; } $response = $http->{$method}("functions/{$path}", $requests); return $this->getResponse($response); } public function moduleRequest($method, $path, $requests): array { $module_id = auth('module')->id(); $http = Http::module($this->api_token, $this->url) ->accept('application/json'); $http->withHeaders([ 'X-Module' => $module_id ]); $requests['module_id'] = $module_id; $response = $http->{$method}("exports/{$path}", $requests); return $this->getResponse($response); } private function getResponse(Response $response): array { $json = $response->json(); $status = $response->status(); return [ 'body' => $response->body(), 'json' => $json, 'status' => $status ]; } public function check($module_id = null): bool { if ($module_id) { $module = Module::find($module_id); } else { $module = $this; } $http = Http::module($module->api_token, $module->url); $success = 0; try { $response = $http->get('remote'); if ($response->status() == 200) { $success = 1; } } catch (ConnectException $e) { Log::error($e->getMessage()); } return $success; } #[ArrayShape(['transactions' => "array"])] public function calculate(): array { $cache_key = 'module_earning_' . $this->id; return Cache::get($cache_key, []); } }