IndexLogic.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\logic;
  20. use app\common\enum\FootprintEnum;
  21. use app\common\enum\UserTerminalEnum;
  22. use app\common\logic\BaseLogic;
  23. use app\common\model\IndexVisit;
  24. use app\common\model\Visitor;
  25. class IndexLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 首页访客记录
  29. * @return bool
  30. * @author Tab
  31. * @date 2021/9/11 9:29
  32. */
  33. public static function visit($userId)
  34. {
  35. try {
  36. if ($userId) {
  37. // 汽泡足迹
  38. event('Footprint', ['type' => FootprintEnum::VISIT_MALL, 'user_id' => $userId]);
  39. }
  40. $params = request()->post();
  41. if (!isset($params['terminal']) || !in_array($params['terminal'], UserTerminalEnum::ALL_TERMINAL)) {
  42. throw new \Exception('终端参数缺失或有误');
  43. }
  44. $ip = request()->ip();
  45. // 一个ip一个终端一天只生成一条记录
  46. $record = IndexVisit::where([
  47. 'ip' => $ip,
  48. 'terminal' => $params['terminal']
  49. ])->whereDay('create_time')->findOrEmpty();
  50. if (!$record->isEmpty()) {
  51. // 增加访客在终端的浏览量
  52. $record->visit += 1;
  53. $record->save();
  54. return true;
  55. }
  56. // 生成访客记录
  57. IndexVisit::create([
  58. 'ip' => $ip,
  59. 'terminal' => $params['terminal'],
  60. 'visit' => 1
  61. ]);
  62. return true;
  63. } catch (\Exception $e) {
  64. self::setError($e->getMessage());
  65. return false;
  66. }
  67. }
  68. }