BaseController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /**
  16. * 容器接口.
  17. *
  18. * @Inject
  19. * @var ContainerInterface
  20. */
  21. protected $container;
  22. /**
  23. * 请求接口.
  24. *
  25. * @Inject
  26. * @var RequestInterface
  27. */
  28. protected $request;
  29. /**
  30. * 相应接口.
  31. *
  32. * @Inject
  33. * @var ResponseInterface
  34. */
  35. protected $response;
  36. /**
  37. * 获取用户Id.
  38. * @return string
  39. */
  40. public function getUserId()
  41. {
  42. return (new AuthService())->getUserId();
  43. }
  44. /**
  45. * 记录操作日志.
  46. * @param string $subject 主题.
  47. * @param string $svalue 关联数据.
  48. * @return bool
  49. */
  50. protected function log(string $subject, $svalue = '')
  51. {
  52. list($module, $action) = $this->getModuleAndAction();
  53. $inputs = [
  54. 'user_id' => $this->getUserId(),
  55. 'subject' => mb_strcut((string)$subject, 0, 700),
  56. 'module' => $module,
  57. 'action' => $action,
  58. 'svalue' => !empty($svalue) ? mb_strcut((string)$svalue, 0, 700) : '',
  59. ];
  60. make(UserLogRepository::class)->createOne($inputs);
  61. return true;
  62. }
  63. }