增加 文件上传支持
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());
|
||||
|
||||
$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']);
|
||||
|
@ -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();
|
||||
@ -131,14 +138,16 @@ private function getResponse(Response $response): array
|
||||
* @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, []);
|
||||
}
|
||||
@ -225,6 +235,7 @@ public function calculate(): array
|
||||
* @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) {
|
||||
@ -272,6 +283,7 @@ public function reduce(string|null $amount = '0', string|null $description = '
|
||||
* @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);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user