StoreVisit.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 商品浏览分析
  7. * Class StoreVisit
  8. * @package app\admin\model\store
  9. */
  10. class StoreVisit extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'store_visit';
  22. use ModelTrait;
  23. /**
  24. * 设置浏览信息
  25. * @param $uid
  26. * @param int $product_id
  27. * @param int $product_type
  28. * @param int $cate
  29. * @param string $type
  30. * @param string $content
  31. * @param int $min
  32. */
  33. public static function setView($uid, $product_id = 0, $product_type = 'product', $cate = 0, $type = '', $content = '', $min = 20)
  34. {
  35. $model = new self();
  36. $view = $model->where('uid', $uid)->where('product_id', $product_id)->where('product_type', $product_type)->field('count,add_time,id')->find();
  37. if ($view && $type != 'search') {
  38. $time = time();
  39. if (($view['add_time'] + $min) < $time) {
  40. $model->where(['id' => $view['id']])->update(['count' => $view['count'] + 1, 'add_time' => time()]);
  41. }
  42. } else {
  43. $cate = explode(',', $cate)[0];
  44. $model->insert([
  45. 'add_time' => time(),
  46. 'count' => 1,
  47. 'product_id' => $product_id,
  48. 'product_type' => $product_type,
  49. 'cate_id' => $cate,
  50. 'type' => $type,
  51. 'uid' => $uid,
  52. 'content' => $content
  53. ]);
  54. }
  55. }
  56. }