2022-08-14 09:40:25 +00:00
|
|
|
<?php
|
|
|
|
|
2023-01-19 16:11:50 +00:00
|
|
|
namespace App\Http\Controllers\Module;
|
2022-08-14 09:40:25 +00:00
|
|
|
|
2022-08-28 17:04:44 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Models\Module;
|
2023-02-05 12:36:41 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2022-11-20 14:16:04 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-11-20 14:16:04 +00:00
|
|
|
use Illuminate\Http\Response;
|
2023-02-05 12:36:41 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-08-14 09:40:25 +00:00
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
class ModuleController extends Controller
|
2022-08-14 09:40:25 +00:00
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
public function index(): JsonResponse
|
2022-08-14 09:40:25 +00:00
|
|
|
{
|
2022-11-20 14:16:04 +00:00
|
|
|
$module = auth('module')->user()->calculate();
|
2023-01-01 10:45:58 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->success($module);
|
2022-08-14 09:40:25 +00:00
|
|
|
}
|
2022-08-28 17:04:44 +00:00
|
|
|
|
2023-01-10 13:42:27 +00:00
|
|
|
public function call(Request $request, Module $module): Response|JsonResponse
|
2022-08-28 17:04:44 +00:00
|
|
|
{
|
2022-11-24 11:59:32 +00:00
|
|
|
$path = $this->fixPath($request, $module, 'api');
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
$method = Str::lower($request->method());
|
|
|
|
|
2023-02-05 12:44:42 +00:00
|
|
|
$response = $module->request($method, $path, $request->all());
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
if ($response['json'] === null && $response['body'] !== null) {
|
|
|
|
return response($response['body'], $response['status']);
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->moduleResponse($response['json'], $response['status']);
|
2022-08-28 17:04:44 +00:00
|
|
|
}
|
2022-09-16 11:56:58 +00:00
|
|
|
|
2023-01-01 13:01:13 +00:00
|
|
|
private function fixPath(Request $request, Module $module, $prefix): string
|
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
$path = substr($request->path(), strlen("/$prefix/modules/$module->id"));
|
2023-01-01 13:01:13 +00:00
|
|
|
|
2023-01-21 10:29:40 +00:00
|
|
|
// 只允许最基本的字符,以及 _,-
|
|
|
|
return preg_replace('/[^a-zA-Z0-9_\-\/]/', '', $path);
|
2023-01-01 13:01:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
public function exportCall(Request $request, Module $module): Response|JsonResponse
|
2022-09-19 13:24:14 +00:00
|
|
|
{
|
2022-11-24 11:59:32 +00:00
|
|
|
$path = $this->fixPath($request, $module, 'modules');
|
2022-09-19 13:24:14 +00:00
|
|
|
$method = Str::lower($request->method());
|
|
|
|
|
|
|
|
$response = $module->moduleRequest($method, $path, $request->all());
|
|
|
|
|
|
|
|
if ($response['json'] === null && $response['body'] !== null) {
|
|
|
|
return response($response['body'], $response['status']);
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->moduleResponse($response['json'], $response['status']);
|
2022-09-19 13:24:14 +00:00
|
|
|
}
|
2022-08-14 09:40:25 +00:00
|
|
|
}
|