StatVisitClass.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace bizHd\stat\classes;
  3. use Yii;
  4. use bizHd\base\classes\BaseClass;
  5. /**
  6. * 零售端 -- 浏览记录与浏览统计
  7. * Class StatVisitClass
  8. * @package bizHd\stat\classes
  9. */
  10. class StatVisitClass extends BaseClass
  11. {
  12. //public static $baseFile = '\bizHd\stat\models\StatVisit';
  13. //零售记录访客
  14. public static function addHdCustomVisit($shopId, $userId, $date = null)
  15. {
  16. $date = isset($date) == false ? date("Ymd") : $date;
  17. $cacheKey = 'hd_visit:' . $shopId . '_' . $date;
  18. $exists = Yii::$app->redis->executeCommand('SISMEMBER', [$cacheKey, $userId]);
  19. if (empty($exists)) {
  20. Yii::$app->redis->executeCommand('SADD', [$cacheKey, $userId]);
  21. }
  22. // 只缓存1天:若未设置过期时间则设置为 86400 秒
  23. $ttl = (int)Yii::$app->redis->executeCommand('TTL', [$cacheKey]);
  24. if ($ttl === -1) {
  25. Yii::$app->redis->executeCommand('EXPIRE', [$cacheKey, 86400]);
  26. }
  27. }
  28. //保存访客数
  29. public static function saveVisit($shop, $visitNum, $date = null)
  30. {
  31. }
  32. //获取访客数
  33. public static function getVisitNum($shopId, $date = null)
  34. {
  35. $date = isset($date) == false ? date("Ymd") : $date;
  36. $cacheKey = 'hd_visit:' . $shopId . '_' . $date;
  37. $num = Yii::$app->redis->executeCommand('SCARD', [$cacheKey]);
  38. return intval($num);
  39. }
  40. //获取访客ID
  41. public static function getVisitCustomIds($shopId, $date = null)
  42. {
  43. $date = isset($date) ? $date : date("Ymd");
  44. $cacheKey = 'hd_visit:' . $shopId . '_' . $date;
  45. $userIds = Yii::$app->redis->executeCommand('SMEMBERS', [$cacheKey]);
  46. return $userIds;
  47. }
  48. }