SearchHistory.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace addons\shopro\service;
  3. use app\admin\model\shopro\SearchHistory as SearchHistoryModel;
  4. use app\admin\model\shopro\user\User;
  5. class SearchHistory
  6. {
  7. protected $user = null;
  8. /**
  9. * @var array
  10. */
  11. public $config = [];
  12. public function __construct($user = null)
  13. {
  14. $this->user = is_numeric($user) ? User::get($user) : $user;
  15. $this->user = $this->user ?: auth_user();
  16. }
  17. public function save($params)
  18. {
  19. $keyword = $params['keyword'];
  20. // SearchHistoryModel::where('user_id', $this->user->id)->where('keyword', $keyword)->delete();
  21. $searchHistory = new SearchHistoryModel();
  22. $searchHistory->user_id = $this->user ? $this->user->id : 0;
  23. $searchHistory->keyword = $keyword;
  24. $searchHistory->save();
  25. }
  26. public function hotSearch()
  27. {
  28. $now = time();
  29. $start = time() - (86400 * 30); // 30 天前
  30. $hotSearchs = SearchHistoryModel::field('keyword,count(*) as num')
  31. ->whereTime('createtime', 'between', [$start, $now])
  32. ->group('keyword')->order('num', 'desc')->limit(5)->select();
  33. return $hotSearchs;
  34. }
  35. }