| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Module\ThirdParty\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Facades\Log;
- use App\Module\ThirdParty\Services\WebhookDispatchService;
- /**
- * Webhook分发控制器
- *
- * 实现路由规则:/thirdParty/webhook/{包名}/{Handler路由}
- * 负责将Webhook请求分发到对应的包处理器
- */
- class WebhookDispatchController extends Controller
- {
- /**
- * Webhook分发服务
- */
- protected WebhookDispatchService $dispatchService;
-
- /**
- * 构造函数
- */
- public function __construct(WebhookDispatchService $dispatchService)
- {
- $this->dispatchService = $dispatchService;
- }
-
- /**
- * 处理Webhook请求
- *
- * @param Request $request 请求对象
- * @param string $packageName 包名
- * @param string $handlerRoute Handler路由
- * @return JsonResponse
- */
- public function dispatch(Request $request, string $packageName, string $handlerRoute): JsonResponse
- {
- $requestId = uniqid('webhook_dispatch_', true);
-
- try {
- Log::info("Webhook分发开始", [
- 'request_id' => $requestId,
- 'package_name' => $packageName,
- 'handler_route' => $handlerRoute,
- 'method' => $request->method(),
- 'url' => $request->fullUrl(),
- 'ip' => $request->ip(),
- ]);
-
- // 验证包名格式
- $this->validatePackageName($packageName);
-
- // 验证Handler路由格式
- $this->validateHandlerRoute($handlerRoute);
-
- // 分发到具体的处理器
- $result = $this->dispatchService->dispatch($packageName, $handlerRoute, $request);
-
- Log::info("Webhook分发成功", [
- 'request_id' => $requestId,
- 'package_name' => $packageName,
- 'handler_route' => $handlerRoute,
- 'result' => $result,
- ]);
-
- return response()->json([
- 'success' => true,
- 'data' => $result,
- 'request_id' => $requestId,
- ]);
-
- } catch (\Exception $e) {
- Log::error("Webhook分发失败", [
- 'request_id' => $requestId,
- 'package_name' => $packageName,
- 'handler_route' => $handlerRoute,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
-
- return response()->json([
- 'success' => false,
- 'error' => $e->getMessage(),
- 'request_id' => $requestId,
- ], 400);
- }
- }
-
- /**
- * 验证包名格式
- *
- * @param string $packageName 包名
- * @throws \Exception
- */
- protected function validatePackageName(string $packageName): void
- {
- // 包名只能包含字母、数字、下划线
- if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $packageName)) {
- throw new \Exception("无效的包名格式: {$packageName}");
- }
-
- // 包名长度限制
- if (strlen($packageName) > 50) {
- throw new \Exception("包名长度不能超过50个字符: {$packageName}");
- }
- }
-
- /**
- * 验证Handler路由格式
- *
- * @param string $handlerRoute Handler路由
- * @throws \Exception
- */
- protected function validateHandlerRoute(string $handlerRoute): void
- {
- // Handler路由只能包含字母、数字、下划线、斜杠
- if (!preg_match('/^[a-zA-Z0-9_\/]+$/', $handlerRoute)) {
- throw new \Exception("无效的Handler路由格式: {$handlerRoute}");
- }
-
- // Handler路由长度限制
- if (strlen($handlerRoute) > 100) {
- throw new \Exception("Handler路由长度不能超过100个字符: {$handlerRoute}");
- }
-
- // 不能以斜杠开头或结尾
- if (str_starts_with($handlerRoute, '/') || str_ends_with($handlerRoute, '/')) {
- throw new \Exception("Handler路由不能以斜杠开头或结尾: {$handlerRoute}");
- }
- }
-
- /**
- * 健康检查接口
- *
- * @return JsonResponse
- */
- public function health(): JsonResponse
- {
- return response()->json([
- 'success' => true,
- 'message' => 'ThirdParty Webhook分发服务运行正常',
- 'timestamp' => now()->toISOString(),
- ]);
- }
-
- /**
- * 获取已注册的包列表
- *
- * @return JsonResponse
- */
- public function packages(): JsonResponse
- {
- try {
- $packages = $this->dispatchService->getRegisteredPackages();
-
- return response()->json([
- 'success' => true,
- 'data' => $packages,
- ]);
-
- } catch (\Exception $e) {
- return response()->json([
- 'success' => false,
- 'error' => $e->getMessage(),
- ], 500);
- }
- }
- }
|