diff --git a/app/Http/Controllers/Module/ModuleController.php b/app/Http/Controllers/Module/ModuleController.php index b6f8336..3ad47a6 100644 --- a/app/Http/Controllers/Module/ModuleController.php +++ b/app/Http/Controllers/Module/ModuleController.php @@ -24,7 +24,21 @@ public function call(Request $request, Module $module): Response|JsonResponse $method = Str::lower($request->method()); - $response = $module->request($method, $path, $request->all()); + $all_files = $request->allFiles(); + + if ($all_files) { + $files = []; + foreach ($all_files as $key => $file) { + $files[$key] = [ + 'name' => $file->getClientOriginalName(), + 'content' => fopen($file->getRealPath(), 'r') + ]; + } + + $response = $module->request($method, $path, $request->all(), $files); + } else { + $response = $module->request($method, $path, $request->all()); + } if ($response['json'] === null && $response['body'] !== null) { return response($response['body'], $response['status']); diff --git a/app/Models/Module.php b/app/Models/Module.php index c9efd6f..6a26405 100644 --- a/app/Models/Module.php +++ b/app/Models/Module.php @@ -50,7 +50,7 @@ protected static function boot() { parent::boot(); static::creating(function (self $model) { - if (! app()->environment('local')) { + if (!app()->environment('local')) { $model->api_token = Str::random(60); } @@ -76,14 +76,23 @@ protected static function boot() // post, get, patch, delete 等请求 public function remote($func, $requests): array { - $response = $this->http()->post('functions/'.$func, $requests); + $response = $this->http()->post('functions/' . $func, $requests); return $this->getResponse($response); } - public function http(): PendingRequest + public function http($files = []): PendingRequest { - $http = Http::module($this->api_token, $this->url.'/remote')->acceptJson()->timeout(5); + $http = Http::module($this->api_token, $this->url . '/remote'); + + if ($files) { + $http->asMultipart(); + foreach ($files as $name => $file) { + $http->attach($name, $file['content'], $file['name']); + } + } + + $http->acceptJson()->timeout(5); if ($this->ip_port) { // 如果设置了 ip_port 则使用 ip_port @@ -100,8 +109,6 @@ public function http(): PendingRequest private function getResponse(Response $response): array { - // $module_token = $response->header('x-module-api-token'); - $success = true; $json = $response->json(); $status = $response->status(); @@ -128,17 +135,19 @@ private function getResponse(Response $response): array } /** - * @param $method - * @param $path - * @param $requests + * @param $method + * @param $path + * @param $requests + * @param array $files + * * @return array */ - public function request($method, $path, $requests): array + public function request($method, $path, $requests, array $files = []): array { try { - return $this->baseRequest($method, "functions/$path", $requests); - } catch (ConnectException|ConnectionException $e) { - Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage()); + return $this->baseRequest($method, "functions/$path", $requests, $files); + } /** @noinspection PhpRedundantCatchClauseInspection */ catch (ConnectException|ConnectionException $e) { + Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage()); return [ 'body' => null, @@ -149,11 +158,12 @@ public function request($method, $path, $requests): array } } - public function baseRequest($method, $path, $requests = []): array + public function baseRequest($method, $path, $requests = [], $files = []): array { $user = auth('sanctum')->user(); - $http = $this->http(); + $http = $this->http($files); + if ($user) { $http = $http->withHeaders([ 'X-User-Id' => $user->id, @@ -213,7 +223,7 @@ public function check(): bool #[ArrayShape(['transactions' => 'array'])] public function calculate(): array { - $cache_key = 'module_earning_'.$this->id; + $cache_key = 'module_earning_' . $this->id; return Cache::get($cache_key, []); } @@ -221,10 +231,11 @@ public function calculate(): array /** * 扣除费用 * - * @param string|null $amount - * @param string|null $description - * @param bool $fail - * @param array $options + * @param string|null $amount + * @param string|null $description + * @param bool $fail + * @param array $options + * * @return string */ public function reduce(string|null $amount = '0', string|null $description = '消费', bool $fail = false, array $options = []): string @@ -233,7 +244,7 @@ public function reduce(string|null $amount = '0', string|null $description = ' return $this->balance; } - Cache::lock('module_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { + Cache::lock('module_balance_' . $this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { $this->refresh(); if ($this->balance < $amount) { @@ -268,10 +279,11 @@ public function reduce(string|null $amount = '0', string|null $description = ' /** * 增加余额 * - * @param string|null $amount - * @param string $payment - * @param string|null $description - * @param array $options + * @param string|null $amount + * @param string $payment + * @param string|null $description + * @param array $options + * * @return string */ public function charge(string|null $amount = '0', string $payment = 'console', string|null $description = '充值', array $options = []): string @@ -280,7 +292,7 @@ public function charge(string|null $amount = '0', string $payment = 'console', s return $this->balance; } - Cache::lock('module_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { + Cache::lock('module_balance_' . $this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { $this->refresh(); $this->balance = bcadd($this->balance, $amount, 4); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 693c1dc..7521977 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -37,14 +37,11 @@ public function boot(): void ->withUserAgent('LAECloud-Client') ->withHeaders([ 'X-Module-Api-Token' => $api_token, - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', ])->withOptions([ 'version' => 2, ]); }); - // Carbon setTestNow // Carbon::setTestNow(now()->addDays(1)); } }