增加 文件上传支持

This commit is contained in:
iVampireSP.com 2023-02-07 17:01:43 +08:00
parent fb56b30d16
commit 5aa804ccd0
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 53 additions and 30 deletions

View File

@ -24,7 +24,21 @@ public function call(Request $request, Module $module): Response|JsonResponse
$method = Str::lower($request->method()); $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) { if ($response['json'] === null && $response['body'] !== null) {
return response($response['body'], $response['status']); return response($response['body'], $response['status']);

View File

@ -50,7 +50,7 @@ protected static function boot()
{ {
parent::boot(); parent::boot();
static::creating(function (self $model) { static::creating(function (self $model) {
if (! app()->environment('local')) { if (!app()->environment('local')) {
$model->api_token = Str::random(60); $model->api_token = Str::random(60);
} }
@ -76,14 +76,23 @@ protected static function boot()
// post, get, patch, delete 等请求 // post, get, patch, delete 等请求
public function remote($func, $requests): array public function remote($func, $requests): array
{ {
$response = $this->http()->post('functions/'.$func, $requests); $response = $this->http()->post('functions/' . $func, $requests);
return $this->getResponse($response); 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) { if ($this->ip_port) {
// 如果设置了 ip_port 则使用 ip_port // 如果设置了 ip_port 则使用 ip_port
@ -100,8 +109,6 @@ public function http(): PendingRequest
private function getResponse(Response $response): array private function getResponse(Response $response): array
{ {
// $module_token = $response->header('x-module-api-token');
$success = true; $success = true;
$json = $response->json(); $json = $response->json();
$status = $response->status(); $status = $response->status();
@ -128,17 +135,19 @@ private function getResponse(Response $response): array
} }
/** /**
* @param $method * @param $method
* @param $path * @param $path
* @param $requests * @param $requests
* @param array $files
*
* @return array * @return array
*/ */
public function request($method, $path, $requests): array public function request($method, $path, $requests, array $files = []): array
{ {
try { try {
return $this->baseRequest($method, "functions/$path", $requests); return $this->baseRequest($method, "functions/$path", $requests, $files);
} catch (ConnectException|ConnectionException $e) { } /** @noinspection PhpRedundantCatchClauseInspection */ catch (ConnectException|ConnectionException $e) {
Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage()); Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage());
return [ return [
'body' => null, '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(); $user = auth('sanctum')->user();
$http = $this->http(); $http = $this->http($files);
if ($user) { if ($user) {
$http = $http->withHeaders([ $http = $http->withHeaders([
'X-User-Id' => $user->id, 'X-User-Id' => $user->id,
@ -213,7 +223,7 @@ public function check(): bool
#[ArrayShape(['transactions' => 'array'])] #[ArrayShape(['transactions' => 'array'])]
public function calculate(): array public function calculate(): array
{ {
$cache_key = 'module_earning_'.$this->id; $cache_key = 'module_earning_' . $this->id;
return Cache::get($cache_key, []); return Cache::get($cache_key, []);
} }
@ -221,10 +231,11 @@ public function calculate(): array
/** /**
* 扣除费用 * 扣除费用
* *
* @param string|null $amount * @param string|null $amount
* @param string|null $description * @param string|null $description
* @param bool $fail * @param bool $fail
* @param array $options * @param array $options
*
* @return string * @return string
*/ */
public function reduce(string|null $amount = '0', string|null $description = '消费', bool $fail = false, array $options = []): 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; 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(); $this->refresh();
if ($this->balance < $amount) { if ($this->balance < $amount) {
@ -268,10 +279,11 @@ public function reduce(string|null $amount = '0', string|null $description = '
/** /**
* 增加余额 * 增加余额
* *
* @param string|null $amount * @param string|null $amount
* @param string $payment * @param string $payment
* @param string|null $description * @param string|null $description
* @param array $options * @param array $options
*
* @return string * @return string
*/ */
public function charge(string|null $amount = '0', string $payment = 'console', string|null $description = '充值', array $options = []): 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; 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->refresh();
$this->balance = bcadd($this->balance, $amount, 4); $this->balance = bcadd($this->balance, $amount, 4);

View File

@ -37,14 +37,11 @@ public function boot(): void
->withUserAgent('LAECloud-Client') ->withUserAgent('LAECloud-Client')
->withHeaders([ ->withHeaders([
'X-Module-Api-Token' => $api_token, 'X-Module-Api-Token' => $api_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
])->withOptions([ ])->withOptions([
'version' => 2, 'version' => 2,
]); ]);
}); });
// Carbon setTestNow
// Carbon::setTestNow(now()->addDays(1)); // Carbon::setTestNow(now()->addDays(1));
} }
} }