GoodsBrowse.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\goods;
  11. use app\model\member\Member;
  12. use app\model\BaseModel;
  13. use app\model\system\Stat;
  14. /**
  15. * 商品浏览历史
  16. */
  17. class GoodsBrowse extends BaseModel
  18. {
  19. /**
  20. * 添加商品浏览记录
  21. * @param array $data
  22. */
  23. public function addBrowse($data)
  24. {
  25. $res = model('goods_browse')->getInfo([ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'site_id,id');
  26. $data[ 'browse_time' ] = time();
  27. $stat = new Stat();
  28. $stat->switchStat([ 'type' => 'goods_visit', 'data' => [
  29. 'site_id' => $data[ 'site_id' ],
  30. 'goods_id' => $data[ 'goods_id' ],
  31. 'member_id' => $data[ 'member_id' ]
  32. ] ]);
  33. if (!empty($res)) {
  34. $collect_id = model('goods_browse')->update($data, [ [ 'id', '=', $res[ 'id' ] ] ]);
  35. } else {
  36. $collect_id = model('goods_browse')->add($data);
  37. }
  38. Member::modifyLastVisitTime($data[ 'member_id' ]);
  39. //添加浏览统计
  40. $stat->switchStat([ 'type' => 'visit', 'data' => [
  41. 'visit_count' => 1,
  42. 'site_id' => $data[ 'site_id' ],
  43. 'app_module' => $data[ 'app_module' ] ?? '',
  44. 'goods_id' => $data[ 'goods_id' ],
  45. ] ]);
  46. return $this->success($collect_id);
  47. }
  48. /**
  49. * 删除浏览记录
  50. * @param int $id
  51. * @param int $member_id
  52. */
  53. public function deleteBrowse($id, $member_id)
  54. {
  55. $res = model('goods_browse')->delete([ [ 'member_id', '=', $member_id ], [ 'id', 'in', $id ] ]);
  56. return $this->success($res);
  57. }
  58. /**
  59. * 获取浏览记录分页列表
  60. * @param array $condition
  61. * @param number $page
  62. * @param string $page_size
  63. * @param string $order
  64. * @param string $field
  65. */
  66. public function getBrowsePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'browse_time desc', $field = 'id,member_id,browse_time,goods_id,sku_id', $alias = 'a', $join = [])
  67. {
  68. $list = model('goods_browse')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  69. return $this->success($list);
  70. }
  71. /**
  72. * 获取浏览商品列表
  73. * @param array $condition
  74. * @param string $field
  75. * @param string $order
  76. * @param null $limit
  77. * @return array
  78. */
  79. public function getBrowseList($condition = [], $field = '*', $order = 'id asc', $limit = null)
  80. {
  81. $list = model('goods_browse')->getList($condition, $field, $order, '', '', '', $limit);
  82. return $this->success($list);
  83. }
  84. }