| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Module\AppGame\Handler;
- use App\Module\User\Services\UserActivityService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Response;
- /**
- * Handler 基类
- */
- abstract class BaseHandler
- {
- /**
- * @var bool 是否需要登录
- */
- protected bool $need_login = false;
- public $user_id = 0;
- /**
- * @var Response
- */
- protected Response $response;
- /**
- * BaseHandler constructor.
- * @param Response $response
- */
- public function __construct(Response $response)
- {
- $this->response = $response;
- }
- /**
- * 是否需要登录
- * @return bool
- */
- public function needLogin(): bool
- {
- return $this->need_login;
- }
- /**
- * 处理请求
- * @param Message $data
- * @return Message
- */
- abstract public function handle(Message $data): Message;
- /**
- * 更新用户活动时间
- * 在需要登录的Handler中调用此方法来更新用户活动时间
- *
- * @return void
- */
- protected function updateUserActivityTime(): void
- {
- try {
- if ($this->needLogin() && $this->user_id > 0) {
- UserActivityService::updateActivityTime($this->user_id);
- Log::debug('用户活动时间已更新', [
- 'user_id' => $this->user_id,
- 'handler' => static::class
- ]);
- }
- } catch (\Exception $e) {
- Log::error('更新用户活动时间失败', [
- 'user_id' => $this->user_id,
- 'handler' => static::class,
- 'error' => $e->getMessage()
- ]);
- }
- }
- }
|