Footprint.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\event;
  20. use app\common\enum\FootprintEnum;
  21. use app\common\model\User;
  22. use app\common\service\ConfigService;
  23. use app\common\model\Footprint as FootprintModel;
  24. use app\common\model\FootprintRecord;
  25. use app\common\model\Goods;
  26. use app\common\model\Coupon;
  27. /**
  28. * 气泡足迹事件
  29. * Class Footprint
  30. * @package app\shopapi\event
  31. * 不同场景触发示例
  32. * event('Footprint', ['type' => 1, 'user_id' => 1]); // 访问商城
  33. * event('Footprint', ['type' => 2, 'user_id' => 1, 'foreign_id'=>1]); // 浏览商品, foreign_id为商品id
  34. * event('Footprint', ['type' => 3, 'user_id' => 1, 'foreign_id'=>1]); // 加入购物车, foreign_id为商品id
  35. * event('Footprint', ['type' => 4, 'user_id' => 1]); // 领取优惠券
  36. * event('Footprint', ['type' => 5, 'user_id' => 1]); // 下单结算
  37. */
  38. class Footprint
  39. {
  40. public function __construct($params)
  41. {
  42. // 全局气泡开关
  43. $footprint_status = ConfigService::get('footprint', 'status', 1);
  44. if (!$footprint_status) {
  45. return false;
  46. }
  47. // 判断模板允许使用
  48. if ($this->isAllowUse($params['type'])) {
  49. // 记录足迹
  50. $this->record($params);
  51. }
  52. }
  53. /**
  54. * @notes 判断足迹气泡模板是否允许使用
  55. */
  56. private function isAllowUse($type)
  57. {
  58. $model = FootprintModel::where(['type'=>(int)$type])->findOrEmpty();
  59. if (!$model->isEmpty() && $model['status']) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * @notes 记录足迹
  66. */
  67. private function record($params)
  68. {
  69. // 获取参数(主要参数: type, user_id, goods_id[可能不存在])
  70. if (empty($params['type']) || !$params['type']) {
  71. return false;
  72. }
  73. if (empty($params['user_id']) || !$params['user_id']) {
  74. return false;
  75. }
  76. $template = FootprintModel::where(['type'=>(int)$params['type']])->value('template');
  77. $username = User::where('id', $params['user_id'])->value('nickname');
  78. $username = mb_strlen($username) > 8 ? mb_substr($username, 0, 8) . '*' : $username;
  79. $template = str_replace('${user_name}', $username, $template);
  80. switch ($params['type']) {
  81. case FootprintEnum::VISIT_MALL: //进入商城(30分钟内没记录才记录)
  82. if(!$this->getPeriodTimeRecord($params)) {
  83. $this->add($params, $template);
  84. }
  85. break;
  86. case FootprintEnum::BROWSE_PRODUCT: //浏览商品(30分钟内没记录才记录)
  87. if(!$this->getPeriodTimeRecord($params)) {
  88. $goodsName = Goods::where('id', $params['foreign_id'])->value('name');
  89. $goodsName = mb_strlen($goodsName) > 16 ? mb_substr($goodsName, 0, 16) . '*' : $goodsName;
  90. $template = str_replace('${goods_name}', $goodsName, $template);
  91. $this->add($params, $template);
  92. }
  93. break;
  94. case FootprintEnum::ADD_CART: //加入购物车
  95. $goodsName = Goods::where('id', $params['foreign_id'])->value('name');
  96. $goodsName = mb_strlen($goodsName) > 16 ? mb_substr($goodsName, 0, 16) . '*' : $goodsName;
  97. $template = str_replace('${goods_name}', $goodsName, $template);
  98. $this->add($params, $template);
  99. break;
  100. case FootprintEnum::GET_COUPONS: //领取优惠券
  101. $this->add($params, $template);
  102. break;
  103. case FootprintEnum::ORDER_SETTLEMENT: //下单结算
  104. $this->add($params, $template);
  105. break;
  106. }
  107. }
  108. /**
  109. * @notes 获取30分钟内足迹记录
  110. */
  111. private function getPeriodTimeRecord($params)
  112. {
  113. // 一小时前时间戳
  114. $an_hour_ago = strtotime("-1 hour");
  115. // 30分钟前时间戳
  116. $half_an_hour_ago = $an_hour_ago + 1800;
  117. // 当前时间戳
  118. $current_time = time();
  119. $where = [
  120. ['create_time', '>', $half_an_hour_ago],
  121. ['create_time', '<', $current_time]
  122. ];
  123. if ($params['type']) {
  124. $where[] = ['user_id', '=', (int)$params['user_id']];
  125. $where[] = ['type', '=', (int)$params['type']];
  126. }
  127. // 访问商城
  128. if ($params['type'] === FootprintEnum::VISIT_MALL) {
  129. $where[] = ['foreign_id', '=', 0];
  130. }
  131. // 浏览器商品
  132. if ($params['type'] === FootprintEnum::BROWSE_PRODUCT) {
  133. $where[] = ['foreign_id', '=', (int)$params['foreign_id']];
  134. }
  135. return FootprintRecord::where($where)->select()->toArray();
  136. }
  137. /**
  138. * @notes 添加足迹记录
  139. */
  140. private function add($params, $tpl)
  141. {
  142. return FootprintRecord::create([
  143. 'type' => $params['type'],
  144. 'user_id' => $params['user_id'],
  145. 'foreign_id' => empty($params['foreign_id']) ? 0 : $params['foreign_id'],
  146. 'template' => $tpl
  147. ]);
  148. }
  149. }