You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
6.7 KiB
184 lines
6.7 KiB
<?php
|
|
|
|
namespace app\admin\controller\shopro;
|
|
|
|
use app\common\controller\Backend;
|
|
|
|
/**
|
|
* shopro 基础控制器
|
|
*
|
|
* @icon fa fa-circle-o
|
|
*/
|
|
class Base extends Backend
|
|
{
|
|
|
|
protected $noNeedRight = [];
|
|
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
}
|
|
|
|
|
|
/**
|
|
* 可自定义组合的条件 生成查询所需要的条件,排序方式
|
|
* @param mixed $searchfields 快速查询的字段
|
|
* @param mixed $nobuildfields 不参与buildParams 的字段
|
|
* @param boolean $relationSearch 是否关联查询
|
|
* @return array
|
|
*/
|
|
protected function custombuildparams($searchfields = null, $nobuildfields = [], $relationSearch = null)
|
|
{
|
|
$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
|
|
$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
|
|
$search = $this->request->get("search", '');
|
|
$filter = $this->request->get("filter", '');
|
|
$op = $this->request->get("op", '', 'trim');
|
|
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
|
|
$order = $this->request->get("order", "DESC");
|
|
$offset = $this->request->get("offset", 0);
|
|
$limit = $this->request->get("limit", 0);
|
|
$filter = (array)json_decode($filter, true);
|
|
$op = (array)json_decode($op, true);
|
|
$filter = $filter ? $this->filterParams($filter, $nobuildfields) : []; // 过滤掉不参与 buildParams 的参数
|
|
$where = [];
|
|
$tableName = '';
|
|
if ($relationSearch) {
|
|
if (!empty($this->model)) {
|
|
$name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
|
|
$name = $this->model->getTable();
|
|
$tableName = $name . '.';
|
|
}
|
|
$sortArr = explode(',', $sort);
|
|
foreach ($sortArr as $index => &$item) {
|
|
$item = stripos($item, ".") === false ? $tableName . trim($item) : $item;
|
|
}
|
|
unset($item);
|
|
$sort = implode(',', $sortArr);
|
|
}
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
if (is_array($adminIds)) {
|
|
$where[] = [$tableName . $this->dataLimitField, 'in', $adminIds];
|
|
}
|
|
if ($search) {
|
|
$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
|
|
foreach ($searcharr as $k => &$v) {
|
|
$v = stripos($v, ".") === false ? $tableName . $v : $v;
|
|
}
|
|
unset($v);
|
|
$where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
|
|
}
|
|
foreach ($filter as $k => $v) {
|
|
$sym = isset($op[$k]) ? $op[$k] : '=';
|
|
if (stripos($k, ".") === false) {
|
|
$k = $tableName . $k;
|
|
}
|
|
$v = !is_array($v) ? trim($v) : $v;
|
|
$sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
|
|
switch ($sym) {
|
|
case '=':
|
|
case '<>':
|
|
$where[] = [$k, $sym, (string)$v];
|
|
break;
|
|
case 'LIKE':
|
|
case 'NOT LIKE':
|
|
case 'LIKE %...%':
|
|
case 'NOT LIKE %...%':
|
|
$where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
|
|
break;
|
|
case '>':
|
|
case '>=':
|
|
case '<':
|
|
case '<=':
|
|
$where[] = [$k, $sym, intval($v)];
|
|
break;
|
|
case 'FINDIN':
|
|
case 'FINDINSET':
|
|
case 'FIND_IN_SET':
|
|
$where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")";
|
|
break;
|
|
case 'IN':
|
|
case 'IN(...)':
|
|
case 'NOT IN':
|
|
case 'NOT IN(...)':
|
|
$where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
|
|
break;
|
|
case 'BETWEEN':
|
|
case 'NOT BETWEEN':
|
|
$arr = array_slice(explode(',', $v), 0, 2);
|
|
if (stripos($v, ',') === false || !array_filter($arr)) {
|
|
continue 2;
|
|
}
|
|
//当出现一边为空时改变操作符
|
|
if ($arr[0] === '') {
|
|
$sym = $sym == 'BETWEEN' ? '<=' : '>';
|
|
$arr = $arr[1];
|
|
} elseif ($arr[1] === '') {
|
|
$sym = $sym == 'BETWEEN' ? '>=' : '<';
|
|
$arr = $arr[0];
|
|
}
|
|
$where[] = [$k, $sym, $arr];
|
|
break;
|
|
case 'RANGE':
|
|
case 'NOT RANGE':
|
|
$v = str_replace(' - ', ',', $v);
|
|
$arr = array_slice(explode(',', $v), 0, 2);
|
|
if (stripos($v, ',') === false || !array_filter($arr)) {
|
|
continue 2;
|
|
}
|
|
//当出现一边为空时改变操作符
|
|
if ($arr[0] === '') {
|
|
$sym = $sym == 'RANGE' ? '<=' : '>';
|
|
$arr = $arr[1];
|
|
} elseif ($arr[1] === '') {
|
|
$sym = $sym == 'RANGE' ? '>=' : '<';
|
|
$arr = $arr[0];
|
|
}
|
|
$where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
|
|
break;
|
|
case 'LIKE':
|
|
case 'LIKE %...%':
|
|
$where[] = [$k, 'LIKE', "%{$v}%"];
|
|
break;
|
|
case 'NULL':
|
|
case 'IS NULL':
|
|
case 'NOT NULL':
|
|
case 'IS NOT NULL':
|
|
$where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
$where = function ($query) use ($where) {
|
|
foreach ($where as $k => $v) {
|
|
if (is_array($v)) {
|
|
call_user_func_array([$query, 'where'], $v);
|
|
} else {
|
|
$query->where($v);
|
|
}
|
|
}
|
|
};
|
|
return [$where, $sort, $order, $offset, $limit];
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 过滤原始的不能用buildParams 的条件
|
|
*/
|
|
public function filterParams($filter, $nobuildfields = []) {
|
|
if ($nobuildfields) {
|
|
foreach ($filter as $k => $f) {
|
|
if (in_array($k, $nobuildfields)) {
|
|
unset($filter[$k]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $filter;
|
|
}
|
|
|
|
}
|
|
|