StoreVisit.php 1.6 KB

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