增加 文件上传支持
This commit is contained in:
parent
fb56b30d16
commit
5aa804ccd0
@ -24,7 +24,21 @@ public function call(Request $request, Module $module): Response|JsonResponse
|
|||||||
|
|
||||||
$method = Str::lower($request->method());
|
$method = Str::lower($request->method());
|
||||||
|
|
||||||
|
$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());
|
$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']);
|
||||||
|
@ -81,9 +81,18 @@ public function remote($func, $requests): array
|
|||||||
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();
|
||||||
@ -131,13 +138,15 @@ 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 [
|
||||||
@ -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,
|
||||||
@ -225,6 +235,7 @@ public function calculate(): array
|
|||||||
* @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
|
||||||
@ -272,6 +283,7 @@ public function reduce(string|null $amount = '0', string|null $description = '
|
|||||||
* @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
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user