This commit is contained in:
JaguarJack
2021-01-17 11:22:13 +08:00
5 changed files with 136 additions and 6 deletions

View File

@@ -138,6 +138,57 @@ class CatchQuery extends Query
return $this;
}
/**
* 快速搜索
*
* @param array $params
* @return Query
*/
public function quickSearch($params = []): Query
{
$requestParams = \request()->param();
if (empty($params) && empty($requestParams)) {
return $this;
}
foreach ($requestParams as $field => $value) {
if (isset($params[$field])) {
// ['>', value] || value
if (is_array($params[$field])) {
$this->where($field, $params[$field][0], $params[$field][1]);
} else {
$this->where($field, $value);
}
} else {
// 区间范围 start_数据库字段 & end_数据库字段
$startPos = strpos($field, 'start_');
if ($startPos === 0) {
$this->where(str_replace('start_','', $field), '>=', strtotime($value));
}
$endPos = strpos($field, 'end_');
if ($endPos === 0) {
$this->where(str_replace('end_', '', $field), '>=', strtotime($value));
}
// 模糊搜索
if (Str::contains($field, 'like')) {
[$operate, $field] = explode('_', $field);
if ($operate === 'like') {
$this->whereLike($field, $value);
} else if ($operate === '%like') {
$this->whereLeftLike($field, $value);
} else {
$this->whereRightLike($field, $value);
}
}
// = 值搜索
$this->where($field, $value);
}
}
return $this;
}
/**
*
* @time 2020年01月13日
@@ -178,6 +229,28 @@ class CatchQuery extends Query
return parent::whereLike($field, $condition, $logic);
}
/**
* @param string $field
* @param $condition
* @param string $logic
* @return Query
*/
public function whereLeftLike(string $field, $condition, string $logic = 'AND'): Query
{
return $this->where($field, $condition, $logic, 'left');
}
/**
* @param string $field
* @param $condition
* @param string $logic
* @return Query
*/
public function whereRightLike(string $field, $condition, string $logic = 'AND'): Query
{
return $this->where($field, $condition, $logic, 'right');
}
/**
* 额外的字段
*

View File

@@ -5,6 +5,7 @@ namespace catcher\base;
use catcher\CatchQuery;
use catcher\traits\db\BaseOptionsTrait;
use catcher\traits\db\RewriteTrait;
use catcher\traits\db\TransTrait;
use think\model\concern\SoftDelete;
use catcher\traits\db\ScopeTrait;
@@ -17,7 +18,7 @@ use catcher\traits\db\ScopeTrait;
*/
abstract class CatchModel extends \think\Model
{
use SoftDelete, TransTrait, BaseOptionsTrait, ScopeTrait;
use SoftDelete, TransTrait, BaseOptionsTrait, ScopeTrait, RewriteTrait;
protected $createTime = 'created_at';
@@ -29,8 +30,8 @@ abstract class CatchModel extends \think\Model
protected $autoWriteTimestamp = true;
// 分页 Limit
public const LIMIT = 10;
// 开启
public const ENABLE = 1;
// 禁用

View File

@@ -0,0 +1,56 @@
<?php
namespace catcher\traits\db;
/**
* 重写 think\Model 的方法
*
* Trait RewriteTrait
* @package catcher\traits\db
*/
trait RewriteTrait
{
/**
* 初始化
*
* CatchModel constructor.
* @param array $data
*/
public function __construct(array $data = [])
{
parent::__construct($data);
$this->hidden = array_merge($this->hidden, $this->defaultHiddenFields());
}
/**
* hidden model fields
*
* @return array
*/
protected function defaultHiddenFields(): array
{
return [$this->deleteTime];
}
/**
* 重写 hidden 方法,支持合并 hidden 属性
*
* @param array $hidden
* @return $this
*/
public function hidden(array $hidden = [])
{
/**
* 合并属性
*/
if (!count($this->hidden)) {
$this->hidden = array_merge($this->hidden, $hidden);
return $this;
}
$this->hidden = $hidden;
return $this;
}
}