Service.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\extra\basic;
  3. use app\model\system\SystemConfig;
  4. class Service
  5. {
  6. protected $mode = null;
  7. /**
  8. * 白名单
  9. * @var array|string[]
  10. */
  11. protected array $mobileWhite = [];
  12. /**
  13. * 默认排序筛选
  14. * @param array $param
  15. * @param string $prefix
  16. * @param array $filter
  17. * @return string[]
  18. */
  19. public function defaultSort(array $param = [],string $prefix = "",array $filter = []): array
  20. {
  21. if (!empty($prefix)) $prefix = $prefix.".";
  22. if (isset($param['order']) && $param['order'] == 'descending'){
  23. $orderBy = ["{$prefix}{$param['field']}" => "desc"];
  24. } else if (isset($param['order']) && $param['order'] == 'ascending'){
  25. $orderBy = ["{$prefix}{$param['field']}" => "asc"];
  26. } else {
  27. $orderBy = ["{$prefix}create_at" => "desc"];
  28. }
  29. return $orderBy;
  30. }
  31. /**
  32. * @param array $param 参数
  33. * @param array $filter 过滤器
  34. * @param string $prefix 链表前缀
  35. * @param string $join 链表表名
  36. * @param string $field 字段
  37. * @param string $forKey 链表首字母
  38. * @param string $localKey 被链表首字母
  39. */
  40. protected function searchVal(array $param = [],array $filter = [],string $prefix = "",string $join = "",string $field = "",string $forKey = "",string $localKey = "")
  41. {
  42. $orderBy = $this->defaultSort($param,$prefix);
  43. $commonFilter = [];
  44. // 起止时间
  45. if (!empty($param['create'])) {
  46. $times = between_time($param['create']);
  47. $start = date('Y-m-d',$times['start_time']);
  48. $end = date('Y-m-d',($times['end_time'] + 86400));
  49. $commonFilter[] = ['create_at', '>=', $start ];
  50. $commonFilter[] = ['create_at', '<', $end ];
  51. }
  52. $filter = array_merge($filter,$commonFilter);
  53. if (!empty($join)) {
  54. $this->mode = $this->mode->alias('a')->join("{$join} {$prefix}","a.{$forKey} = {$prefix}.{$localKey}")->field($field);
  55. }
  56. return $this->mode->order($orderBy)->where($filter);
  57. }
  58. /**
  59. *
  60. */
  61. public function getList(array $param = [],$with = null,bool $page = true,array $append = [],$withAttr = null)
  62. {
  63. $model = $this->searchVal($param,$this->searchFilter($param));
  64. // 支持多种类型的 $with 参数
  65. if ($with !== null) {
  66. if (is_callable($with)) {
  67. $with($model);
  68. } elseif (is_string($with) || is_array($with)) {
  69. $model->with($with);
  70. }
  71. }
  72. // 3. 动态设置修改器(withAttr)
  73. if ($withAttr !== null) {
  74. $model->withAttr($withAttr);
  75. }
  76. if (!empty($append)) {
  77. $model->append($append);
  78. }
  79. if ($page) {
  80. $resp = $model->paginate([
  81. "list_rows" => $param['pageSize'],
  82. "page" => $param['page']
  83. ]);
  84. } else {
  85. $resp = $model->select()->toArray();
  86. }
  87. return $resp;
  88. }
  89. }