EavTrait.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Support\Facades\Request;
  4. use App\Services\Interface\InterfaceService;
  5. use Illuminate\Http\JsonResponse;
  6. trait EavTrait
  7. {
  8. /**
  9. * @var InterfaceService
  10. */
  11. protected InterfaceService $interfaceService;
  12. /**
  13. * @var string 接口别名
  14. */
  15. protected string $interfaceAlias = '';
  16. /**
  17. * @param $interfaceAlias
  18. * @return JsonResponse|null
  19. * @throws \Exception
  20. */
  21. public function index($interfaceAlias): JsonResponse|null
  22. {
  23. return $this->manageRequest($interfaceAlias);
  24. }
  25. /**
  26. * @param $interfaceAlias
  27. * @return JsonResponse|null
  28. * @throws \Exception
  29. */
  30. private function manageRequest($interfaceAlias): JsonResponse|null
  31. {
  32. $this->interfaceAlias = $interfaceAlias;
  33. // 获取接口信息
  34. $this->interfaceService = new InterfaceService($interfaceAlias, $this->siteInfo);
  35. $errorMsg = $this->interfaceService->validator();
  36. if ($errorMsg) {
  37. return responseMessage(3001, $errorMsg);
  38. }
  39. $interfaceInfo = $this->interfaceService->getInterfaceInfo();
  40. if(Request::method() == strtoupper($interfaceInfo['request_method'])){
  41. return $this->interfaceService->exec();
  42. }else{
  43. return responseMessage(3001, "请求方式【".Request::method()."】,不被允许!");
  44. }
  45. }
  46. }