123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?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;
- }
- }
|