SearchLog.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $config = get_addon_config('shop');
  30. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  31. $field = empty($tag['field']) ? '*' : $tag['field'];
  32. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  33. $orderby = empty($tag['orderby']) ? 'nums' : $tag['orderby'];
  34. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  35. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  36. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  37. $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
  38. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('searchloglist', $tag);
  39. self::$tagCount++;
  40. $where = [];
  41. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  42. $tagModel = self::where($where)
  43. ->where('status', 'normal')
  44. ->where($condition)
  45. ->field($field)
  46. ->orderRaw($order);
  47. if ($paginate) {
  48. $paginateArr = explode(',', $paginate);
  49. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  50. $config = [];
  51. $config['var_page'] = $paginateArr[2] ?? 'tpage' . self::$tagCount;
  52. $config['path'] = $paginateArr[3] ?? '';
  53. $config['fragment'] = $paginateArr[4] ?? '';
  54. $config['query'] = request()->get();
  55. $list = $tagModel->paginate($listRows, ($paginateArr[1] ?? false), $config);
  56. } else {
  57. $list = $tagModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select();
  58. }
  59. return $list;
  60. }
  61. }