api_token, $this->url); $response = $http->post("hosts/{$host_id}/functions/" . $func, $requests); $json = $response->json(); $status = $response->status(); return [$json, $status]; } public function remote($func, $requests) { $http = Http::remote($this->api_token, $this->url); $response = $http->post('functions/' . $func, $requests); $json = $response->json(); $status = $response->status(); return [$json, $status]; } // post, get, patch, delete 等请求 public function remoteRequest($method, $path, $requests) { $http = Http::remote($this->api_token, $this->url) ->accept('application/json'); unset($requests['func']); $requests['user_id'] = auth('api')->id(); $user = auth('api')->user(); if ($method == 'post') { // add user to requests $requests['user'] = $user; } $requests['user_id'] = $user['id']; $response = $http->{$method}("functions/{$path}", $requests); $json = $response->json(); $status = $response->status(); return [ 'body' => $response->body(), 'json' => $json, 'status' => $status ]; } public function remotePost($path = '', $data = []) { $http = Http::remote($this->api_token, $this->url); $response = $http->post($path, $data); $json = $response->json(); $status = $response->status(); return [$json, $status]; } protected static function boot() { parent::boot(); static::creating(function ($model) { // if local if (!app()->environment('local')) { $model->api_token = Str::random(60); } }); } }