src/AppBundle/Component/Track/UserOnlineTrack.php line 100

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Component\Track;
  3. use AppBundle\Component\DeviceDetector\DeviceDetectorAdapter;
  4. use Codeages\Biz\Framework\Context\Biz;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. class UserOnlineTrack
  7. {
  8.     /**
  9.      * @var \Symfony\Component\DependencyInjection\ContainerInterface
  10.      */
  11.     private $container;
  12.     /**
  13.      * @var \Codeages\Biz\Framework\Context\Biz
  14.      */
  15.     private $biz;
  16.     /**
  17.      * @var \AppBundle\Component\DeviceDetector\DeviceDetectorAdapter
  18.      */
  19.     private $deviceDetector;
  20.     public function __construct(ContainerInterface $containerBiz $biz)
  21.     {
  22.         $this->container $container;
  23.         $this->biz $biz;
  24.         $this->deviceDetector = new DeviceDetectorAdapter($this->getUserAgent());
  25.     }
  26.     /**
  27.      * @todo api 和 web 的sessionId 应该统一
  28.      *
  29.      * @param $sessionId
  30.      */
  31.     public function track($sessionId)
  32.     {
  33.         // 过滤掉爬虫
  34.         if ($this->deviceDetector->isBot()) {
  35.             return;
  36.         }
  37.         $user $this->biz['user'];
  38.         if (empty($user)) {
  39.             return;
  40.         }
  41.         if (!$user->isLogin()) {
  42.             return;
  43.         }
  44.         $online = [
  45.             'sess_id' => $sessionId,
  46.             'user_id' => $user['id'],
  47.             'ip' => $this->getClientIp(),
  48.             'user_agent' => $this->getUserAgent(),
  49.             'source' => $this->isFromApp() ? 'App' 'Web',
  50.         ];
  51.         $this->getUserActiveService()->saveOnline($online);
  52.         $this->logUserLogin();
  53.     }
  54.     protected function logUserLogin()
  55.     {
  56.         $user $this->biz['user'];
  57.         if (empty($user)) {
  58.             return;
  59.         }
  60.         $conditions = [
  61.             'userId' => $user['id'],
  62.             'startDateTime_GE' => strtotime(date('Y-m-d')),
  63.             'startDateTime_LE' => strtotime(date('Y-m-d').' 23:59:59'),
  64.             'actions' => ['login_success''user_login'],
  65.         ];
  66.         $hasLoginLog $this->getLogService()->countUserLoginNum($conditions);
  67.         if (!$hasLoginLog) {
  68.             $this->getUserService()->markLoginInfo();
  69.         }
  70.     }
  71.     private function isFromApp()
  72.     {
  73.         $pathInfo $this->getRequest()->getPathInfo();
  74.         return === strpos($pathInfo'/api');
  75.     }
  76.     private function getRequest()
  77.     {
  78.         return $this->container->get('request_stack')->getMainRequest();
  79.     }
  80.     private function getClientIp()
  81.     {
  82.         return $this->getRequest()->getClientIp();
  83.     }
  84.     private function getUserAgent()
  85.     {
  86.         return $this->getRequest()->headers->get('User-Agent');
  87.     }
  88.     /**
  89.      * @return \Biz\User\Service\UserActiveService
  90.      */
  91.     private function getUserActiveService()
  92.     {
  93.         return $this->biz->service('User:UserActiveService');
  94.     }
  95.     private function getUserService()
  96.     {
  97.         return $this->biz->service('User:UserService');
  98.     }
  99.     private function getLogService()
  100.     {
  101.         return $this->biz->service('System:LogService');
  102.     }
  103. }