<?php
namespace AppBundle\Component\Track;
use AppBundle\Component\DeviceDetector\DeviceDetectorAdapter;
use Codeages\Biz\Framework\Context\Biz;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserOnlineTrack
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* @var \Codeages\Biz\Framework\Context\Biz
*/
private $biz;
/**
* @var \AppBundle\Component\DeviceDetector\DeviceDetectorAdapter
*/
private $deviceDetector;
public function __construct(ContainerInterface $container, Biz $biz)
{
$this->container = $container;
$this->biz = $biz;
$this->deviceDetector = new DeviceDetectorAdapter($this->getUserAgent());
}
/**
* @todo api 和 web 的sessionId 应该统一
*
* @param $sessionId
*/
public function track($sessionId)
{
// 过滤掉爬虫
if ($this->deviceDetector->isBot()) {
return;
}
$user = $this->biz['user'];
if (empty($user)) {
return;
}
if (!$user->isLogin()) {
return;
}
$online = [
'sess_id' => $sessionId,
'user_id' => $user['id'],
'ip' => $this->getClientIp(),
'user_agent' => $this->getUserAgent(),
'source' => $this->isFromApp() ? 'App' : 'Web',
];
$this->getUserActiveService()->saveOnline($online);
$this->logUserLogin();
}
protected function logUserLogin()
{
$user = $this->biz['user'];
if (empty($user)) {
return;
}
$conditions = [
'userId' => $user['id'],
'startDateTime_GE' => strtotime(date('Y-m-d')),
'startDateTime_LE' => strtotime(date('Y-m-d').' 23:59:59'),
'actions' => ['login_success', 'user_login'],
];
$hasLoginLog = $this->getLogService()->countUserLoginNum($conditions);
if (!$hasLoginLog) {
$this->getUserService()->markLoginInfo();
}
}
private function isFromApp()
{
$pathInfo = $this->getRequest()->getPathInfo();
return 0 === strpos($pathInfo, '/api');
}
private function getRequest()
{
return $this->container->get('request_stack')->getMainRequest();
}
private function getClientIp()
{
return $this->getRequest()->getClientIp();
}
private function getUserAgent()
{
return $this->getRequest()->headers->get('User-Agent');
}
/**
* @return \Biz\User\Service\UserActiveService
*/
private function getUserActiveService()
{
return $this->biz->service('User:UserActiveService');
}
private function getUserService()
{
return $this->biz->service('User:UserService');
}
private function getLogService()
{
return $this->biz->service('System:LogService');
}
}