BaseHandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Module\AppGame\Handler;
  3. use App\Module\User\Services\UserActivityService;
  4. use Google\Protobuf\Internal\Message;
  5. use Illuminate\Support\Facades\Log;
  6. use Uraus\Kku\Response;
  7. /**
  8. * Handler 基类
  9. */
  10. abstract class BaseHandler
  11. {
  12. /**
  13. * @var bool 是否需要登录
  14. */
  15. protected bool $need_login = false;
  16. public $user_id = 0;
  17. /**
  18. * @var Response
  19. */
  20. protected Response $response;
  21. /**
  22. * BaseHandler constructor.
  23. * @param Response $response
  24. */
  25. public function __construct(Response $response)
  26. {
  27. $this->response = $response;
  28. }
  29. /**
  30. * 是否需要登录
  31. * @return bool
  32. */
  33. public function needLogin(): bool
  34. {
  35. return $this->need_login;
  36. }
  37. /**
  38. * 处理请求
  39. * @param Message $data
  40. * @return Message
  41. */
  42. abstract public function handle(Message $data): Message;
  43. /**
  44. * 更新用户活动时间
  45. * 在需要登录的Handler中调用此方法来更新用户活动时间
  46. *
  47. * @return void
  48. */
  49. protected function updateUserActivityTime(): void
  50. {
  51. try {
  52. if ($this->needLogin() && $this->user_id > 0) {
  53. UserActivityService::updateActivityTime($this->user_id);
  54. Log::debug('用户活动时间已更新', [
  55. 'user_id' => $this->user_id,
  56. 'handler' => static::class
  57. ]);
  58. }
  59. } catch (\Exception $e) {
  60. Log::error('更新用户活动时间失败', [
  61. 'user_id' => $this->user_id,
  62. 'handler' => static::class,
  63. 'error' => $e->getMessage()
  64. ]);
  65. }
  66. }
  67. }