StatVisitClass.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * 获取访客数
  34. * @param $shopId
  35. * @param null $date
  36. * @return int
  37. */
  38. public static function getVisitNum($shopId, $date = null)
  39. {
  40. $date = isset($date) == false ? date("Ymd") : $date;
  41. $cacheKey = 'hd_visit:' . $shopId . '_' . $date;
  42. $num = Yii::$app->redis->executeCommand('SCARD', [$cacheKey]);
  43. return intval($num);
  44. }
  45. //获取访客ID
  46. public static function getVisitCustomIds($shopId, $date = null)
  47. {
  48. $date = isset($date) ? $date : date("Ymd");
  49. $cacheKey = 'hd_visit:' . $shopId . '_' . $date;
  50. $userIds = Yii::$app->redis->executeCommand('SMEMBERS', [$cacheKey]);
  51. return $userIds;
  52. }
  53. }