优化 代码
This commit is contained in:
parent
49c46eaaa1
commit
4d2c52284d
@ -4,19 +4,18 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Module;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ModuleController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$module = auth('module')->user();
|
||||
$module = auth('module')->user()->calculate();
|
||||
|
||||
$calc = $module->calculate();
|
||||
|
||||
|
||||
return $this->success($calc);
|
||||
return $this->success($module);
|
||||
}
|
||||
|
||||
public function call(Request $request, Module $module)
|
||||
@ -27,7 +26,6 @@ public function call(Request $request, Module $module)
|
||||
|
||||
$path = preg_replace('/[^a-zA-Z0-9\/]/', '', $path);
|
||||
|
||||
|
||||
$method = Str::lower($request->method());
|
||||
|
||||
// 如果 method 为 post, 检查用户余额
|
||||
@ -39,17 +37,16 @@ public function call(Request $request, Module $module)
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
$response = $module->remoteRequest($method, $path, $request->all());
|
||||
$response = $module->request($method, $path, $request->all());
|
||||
|
||||
if ($response['json'] === null && $response['body'] !== null) {
|
||||
return response($response['body'], $response['status']);
|
||||
}
|
||||
|
||||
return $this->remoteResponse($response['json'], $response['status']);
|
||||
return $this->moduleResponse($response['json'], $response['status']);
|
||||
}
|
||||
|
||||
public function exportCall(Request $request, Module $module): \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory
|
||||
public function exportCall(Request $request, Module $module): Response|JsonResponse
|
||||
{
|
||||
$path = request()->path();
|
||||
|
||||
@ -64,6 +61,6 @@ public function exportCall(Request $request, Module $module): \Illuminate\Http\R
|
||||
return response($response['body'], $response['status']);
|
||||
}
|
||||
|
||||
return $this->remoteResponse($response['json'], $response['status']);
|
||||
return $this->moduleResponse($response['json'], $response['status']);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
@ -81,45 +82,32 @@ protected static function boot()
|
||||
});
|
||||
}
|
||||
|
||||
public function remoteHost($host_id, $func, $requests)
|
||||
{
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
$response = $http->post("hosts/{$host_id}/functions/" . $func, $requests);
|
||||
|
||||
$json = $response->json();
|
||||
$status = $response->status();
|
||||
|
||||
return [$json, $status];
|
||||
}
|
||||
// public function moduleHostFunctions($host_id, $func, $requests): array
|
||||
// {
|
||||
// $http = Http::module($this->api_token, $this->url);
|
||||
// $response = $http->post("hosts/{$host_id}/functions/" . $func, $requests);
|
||||
//
|
||||
// return $this->getResponse($response);
|
||||
// }
|
||||
|
||||
|
||||
// post, get, patch, delete 等请求
|
||||
|
||||
public function remote($func, $requests)
|
||||
public function remote($func, $requests): array
|
||||
{
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
$response = $http->post('functions/' . $func, $requests);
|
||||
|
||||
$json = $response->json();
|
||||
$status = $response->status();
|
||||
|
||||
return [$json, $status];
|
||||
return $this->getResponse($response);
|
||||
}
|
||||
|
||||
public function remoteRequest($method, $path, $requests)
|
||||
public function request($method, $path, $requests): array
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
|
||||
// add Headers
|
||||
$http->withHeaders([
|
||||
'X-User' => $user->id
|
||||
]);
|
||||
|
||||
$requests['user_id'] = $user->id;
|
||||
|
||||
|
||||
if ($method == 'post') {
|
||||
// add user to requests
|
||||
$requests['user'] = $user;
|
||||
@ -127,18 +115,10 @@ public function remoteRequest($method, $path, $requests)
|
||||
|
||||
$response = $http->{$method}("functions/{$path}", $requests);
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
$status = $response->status();
|
||||
|
||||
return [
|
||||
'body' => $response->body(),
|
||||
'json' => $json,
|
||||
'status' => $status
|
||||
];
|
||||
return $this->getResponse($response);
|
||||
}
|
||||
|
||||
public function moduleRequest($method, $path, $requests)
|
||||
public function moduleRequest($method, $path, $requests): array
|
||||
{
|
||||
$module_id = auth('module')->id();
|
||||
|
||||
@ -153,9 +133,14 @@ public function moduleRequest($method, $path, $requests)
|
||||
|
||||
$response = $http->{$method}("exports/{$path}", $requests);
|
||||
|
||||
$json = $response->json();
|
||||
return $this->getResponse($response);
|
||||
}
|
||||
|
||||
private function getResponse(Response $response): array
|
||||
{
|
||||
$json = $response->json();
|
||||
$status = $response->status();
|
||||
|
||||
return [
|
||||
'body' => $response->body(),
|
||||
'json' => $json,
|
||||
@ -163,27 +148,7 @@ public function moduleRequest($method, $path, $requests)
|
||||
];
|
||||
}
|
||||
|
||||
public function remotePost($path = '', $data = [])
|
||||
{
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
$response = $http->post($path, $data);
|
||||
|
||||
$json = $response->json();
|
||||
$status = $response->status();
|
||||
|
||||
return [$json, $status];
|
||||
}
|
||||
|
||||
|
||||
// // get cached modules
|
||||
// public static function cached_modules()
|
||||
// {
|
||||
// return Cache::remember('modules', 600, function () {
|
||||
// return Module::all();
|
||||
// });
|
||||
// }
|
||||
|
||||
public function check($module_id = null)
|
||||
public function check($module_id = null): bool
|
||||
{
|
||||
if ($module_id) {
|
||||
$module = Module::find($module_id);
|
||||
@ -191,26 +156,27 @@ public function check($module_id = null)
|
||||
$module = $this;
|
||||
}
|
||||
|
||||
try {
|
||||
$http = Http::module($module->api_token, $module->url);
|
||||
// dd($module->url);
|
||||
|
||||
$success = 0;
|
||||
|
||||
try {
|
||||
$response = $http->get('remote');
|
||||
|
||||
if ($response->status() == 200) {
|
||||
$success = 1;
|
||||
}
|
||||
} catch (ConnectException $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
if ($response->status() == 200) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
#[ArrayShape(['transactions' => "array"])]
|
||||
public function calculate(): array
|
||||
{
|
||||
$cache_key = 'module_earning_' . $this->id;
|
||||
|
||||
return Cache::get($cache_key, []);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user