App_Controller_CoreController.proxy.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * 核心控制器.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2020-01-22 20:35:31
  7. */
  8. declare (strict_types=1);
  9. namespace App\Controller;
  10. use App\Constants\CommonConstant;
  11. use Hyperf\HttpServer\Contract\RequestInterface;
  12. use Hyperf\HttpServer\Contract\ResponseInterface;
  13. use Hyperf\HttpServer\Router\Router;
  14. use Psr\Container\ContainerInterface;
  15. use Hyperf\Di\Annotation\Inject;
  16. class CoreController
  17. {
  18. use \Hyperf\Di\Aop\ProxyTrait;
  19. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  20. function __construct()
  21. {
  22. $this->__handlePropertyHandler(__CLASS__);
  23. }
  24. /**
  25. * 容器接口.
  26. *
  27. * @Inject
  28. * @var ContainerInterface
  29. */
  30. protected $container;
  31. /**
  32. * 请求接口.
  33. *
  34. * @Inject
  35. * @var RequestInterface
  36. */
  37. protected $request;
  38. /**
  39. * 相应接口.
  40. *
  41. * @Inject
  42. * @var ResponseInterface
  43. */
  44. protected $response;
  45. /**
  46. * 获取
  47. * @return array|mixed
  48. */
  49. protected function getJsonData()
  50. {
  51. return getDecodeData();
  52. }
  53. /**
  54. * 获取日志需要的当前模块名和动作名.
  55. * @return array
  56. */
  57. protected function getModuleAndAction()
  58. {
  59. $routeAction = $this->getCurrentRouteHandler();
  60. $controller_suffix = 'Controller';
  61. preg_match_all('/([a-z0-9A-Z]+\\\\)?(\\w+)@(\\w+)/', $routeAction, $matchs);
  62. $controller_full = !empty($matchs[2][0]) ? $matchs[2][0] : '';
  63. //类名.
  64. $controller = str_replace($controller_suffix, '', $controller_full);
  65. // 控制器名.
  66. $action = !empty($matchs[3][0]) ? $matchs[3][0] : '';
  67. //方法名.
  68. return [$controller, $action];
  69. }
  70. /**
  71. * 获取当前路由话柄.
  72. * @return string
  73. */
  74. protected function getCurrentRouteHandler()
  75. {
  76. $currentUri = $this->request->getRequestUri();
  77. $currentMethod = $this->request->getMethod();
  78. $routeData = Router::getData();
  79. if (isset($routeData[0][$currentMethod][$currentUri])) {
  80. return $routeData[0][$currentMethod][$currentUri]->callback;
  81. } else {
  82. return '';
  83. }
  84. }
  85. }