BaseHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Module\OpenAPI\Handlers;
  3. use App\Module\OpenAPI\Contracts\HandlerInterface;
  4. use App\Module\OpenAPI\Services\ScopeService;
  5. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  6. use Illuminate\Http\JsonResponse;
  7. /**
  8. * OpenAPI Handler基类
  9. *
  10. * 提供Handler的基础功能和通用方法
  11. */
  12. abstract class BaseHandler implements HandlerInterface
  13. {
  14. protected ScopeService $scopeService;
  15. public function __construct(ScopeService $scopeService)
  16. {
  17. $this->scopeService = $scopeService;
  18. }
  19. /**
  20. * 处理请求(模板方法)
  21. *
  22. * @param array $data 请求数据
  23. * @param array $context 上下文信息
  24. * @return JsonResponse
  25. */
  26. public function handle(array $data, array $context = []): JsonResponse
  27. {
  28. try {
  29. // 验证权限
  30. $app = $this->getApp($context);
  31. if (!$app) {
  32. return $this->errorResponse('应用信息不存在', null, 404);
  33. }
  34. if (!$this->validatePermissions($app->scopes ?? [], $context)) {
  35. return $this->errorResponse('权限不足', null, 403);
  36. }
  37. // 调用具体的业务处理方法
  38. return $this->process($data, $context);
  39. } catch (\Exception $e) {
  40. return $this->errorResponse('请求处理失败', ['error' => $e->getMessage()], 500);
  41. }
  42. }
  43. /**
  44. * 具体的业务处理方法(由子类实现)
  45. *
  46. * @param array $data 请求数据
  47. * @param array $context 上下文信息
  48. * @return JsonResponse
  49. */
  50. abstract protected function process(array $data, array $context = []): JsonResponse;
  51. /**
  52. * 验证权限
  53. *
  54. * @param array $scopes 应用权限范围
  55. * @param array $context 上下文信息
  56. * @return bool
  57. */
  58. public function validatePermissions(array $scopes, array $context = []): bool
  59. {
  60. $requiredScopes = $this->getRequiredScopes();
  61. foreach ($requiredScopes as $requiredScope) {
  62. if (!in_array($requiredScope->value, $scopes)) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. /**
  69. * 成功响应
  70. *
  71. * @param string $message
  72. * @param mixed $data
  73. * @param int $code
  74. * @return JsonResponse
  75. */
  76. protected function successResponse(string $message, $data = null, int $code = 200): JsonResponse
  77. {
  78. $response = [
  79. 'success' => true,
  80. 'message' => $message,
  81. ];
  82. if ($data !== null) {
  83. $response['data'] = $data;
  84. }
  85. return response()->json($response, $code);
  86. }
  87. /**
  88. * 错误响应
  89. *
  90. * @param string $message
  91. * @param mixed $errors
  92. * @param int $code
  93. * @return JsonResponse
  94. */
  95. protected function errorResponse(string $message, $errors = null, int $code = 400): JsonResponse
  96. {
  97. $response = [
  98. 'success' => false,
  99. 'message' => $message,
  100. ];
  101. if ($errors !== null) {
  102. $response['errors'] = $errors;
  103. }
  104. return response()->json($response, $code);
  105. }
  106. /**
  107. * 验证请求数据
  108. *
  109. * @param array $data
  110. * @param array $rules
  111. * @return array|null 返回错误信息,null表示验证通过
  112. */
  113. protected function validateData(array $data, array $rules): ?array
  114. {
  115. $validator = validator($data, $rules);
  116. if ($validator->fails()) {
  117. return $validator->errors()->toArray();
  118. }
  119. return null;
  120. }
  121. /**
  122. * 获取用户ID
  123. *
  124. * @param array $context
  125. * @return int
  126. */
  127. protected function getUserId(array $context): int
  128. {
  129. return $context['user_id'] ?? 0;
  130. }
  131. /**
  132. * 获取应用信息
  133. *
  134. * @param array $context
  135. * @return \App\Module\OpenAPI\Models\OpenApiApp|null
  136. */
  137. protected function getApp(array $context): ?\App\Module\OpenAPI\Models\OpenApiApp
  138. {
  139. return $context['app'] ?? null;
  140. }
  141. /**
  142. * 记录操作日志
  143. *
  144. * @param string $action
  145. * @param array $data
  146. * @param array $context
  147. * @return void
  148. */
  149. protected function logAction(string $action, array $data, array $context): void
  150. {
  151. // 这里可以实现日志记录逻辑
  152. // 例如记录到数据库或日志文件
  153. }
  154. }