SearchLog.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Service;
  4. use think\Db;
  5. use think\Model;
  6. /**
  7. * 搜索记录
  8. */
  9. class SearchLog extends Model
  10. {
  11. // 表名
  12. protected $name = 'shop_search_log';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = '';
  18. // 追加属性
  19. protected $append = [
  20. ];
  21. protected static $tagCount = 0;
  22. /**
  23. * 获取标签列表
  24. * @param $tag
  25. * @return false|\PDOStatement|string|\think\Collection
  26. */
  27. public static function getSearchlogList($tag)
  28. {
  29. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  30. $field = empty($tag['field']) ? '*' : $tag['field'];
  31. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  32. $orderby = empty($tag['orderby']) ? 'nums' : $tag['orderby'];
  33. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  34. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  35. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  36. $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
  37. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('searchloglist', $tag);
  38. self::$tagCount++;
  39. $where = [];
  40. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  41. $tagModel = self::where($where)
  42. ->where('status', 'normal')
  43. ->where($condition)
  44. ->field($field)
  45. ->orderRaw($order);
  46. if ($paginate) {
  47. $paginateArr = explode(',', $paginate);
  48. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  49. $config = [];
  50. $config['var_page'] = $paginateArr[2] ?? 'tpage' . self::$tagCount;
  51. $config['path'] = $paginateArr[3] ?? '';
  52. $config['fragment'] = $paginateArr[4] ?? '';
  53. $config['query'] = request()->get();
  54. $list = $tagModel->paginate($listRows, ($paginateArr[1] ?? false), $config);
  55. } else {
  56. $list = $tagModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select();
  57. }
  58. return $list;
  59. }
  60. }