Modules_Api_Controllers_BaseController.proxy.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare (strict_types=1);
  3. namespace Modules\Api\Controllers;
  4. use App\Constants\CommonConstant;
  5. use App\Controller\CoreController;
  6. use App\Repositories\UserLogRepository;
  7. use App\Repositories\Utils\JwtRepository;
  8. use Hyperf\Di\Annotation\Inject;
  9. use Hyperf\HttpServer\Contract\RequestInterface;
  10. use Hyperf\HttpServer\Contract\ResponseInterface;
  11. use Modules\Api\Services\AuthService;
  12. use Psr\Container\ContainerInterface;
  13. class BaseController extends CoreController
  14. {
  15. use \Hyperf\Di\Aop\ProxyTrait;
  16. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  17. function __construct()
  18. {
  19. if (method_exists(parent::class, '__construct')) {
  20. parent::__construct(...func_get_args());
  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. * 获取用户Id.
  47. * @return string
  48. */
  49. public function getUserId()
  50. {
  51. return (new AuthService())->getUserId();
  52. }
  53. /**
  54. * 记录操作日志.
  55. * @param string $subject 主题.
  56. * @param string $svalue 关联数据.
  57. * @return bool
  58. */
  59. protected function log(string $subject, $svalue = '')
  60. {
  61. list($module, $action) = $this->getModuleAndAction();
  62. $inputs = ['user_id' => $this->getUserId(), 'subject' => mb_strcut((string) $subject, 0, 700), 'module' => $module, 'action' => $action, 'svalue' => !empty($svalue) ? mb_strcut((string) $svalue, 0, 700) : ''];
  63. make(UserLogRepository::class)->createOne($inputs);
  64. return true;
  65. }
  66. }