فهرست منبع

wesmiler ECY 画画交易平台

APPLE 3 سال پیش
والد
کامیت
60f45487ec
2فایلهای تغییر یافته به همراه193 افزوده شده و 58 حذف شده
  1. 53 58
      application/cmgadm/controller/shop/Goods.php
  2. 140 0
      application/common/controller/Backend.php

+ 53 - 58
application/cmgadm/controller/shop/Goods.php

@@ -15,7 +15,7 @@ use think\Session;
  */
 class Goods extends Backend
 {
-    
+
     /**
      * Goods模型对象
      * @var \app\cmgadm\model\Goods
@@ -26,18 +26,18 @@ class Goods extends Backend
     {
         parent::_initialize();
         $this->model = new \app\cmgadm\model\Goods;
-        $this->categoryList=Goodscats::getTreeList();
-        array_unshift($this->categoryList, ['id' => 0, 'name' => '无','type'=>0]);
+        $this->categoryList = Goodscats::getTreeList();
+        array_unshift($this->categoryList, ['id' => 0, 'name' => '无', 'type' => 0]);
         $this->assign('categoryList', $this->categoryList);
-        $this->assign('shoptype',config('shopType'));
+        $this->assign('shoptype', config('shopType'));
     }
-    
+
     /**
      * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
-    
+
 
     /**
      * 查看
@@ -48,41 +48,35 @@ class Goods extends Backend
         $this->relationSearch = false;
         //设置过滤方法
         $this->request->filter(['strip_tags']);
-        if ($this->request->isAjax())
-        {
+        if ($this->request->isAjax()) {
             //如果发送的来源是Selectpage,则转发到Selectpage
-            if ($this->request->request('keyField'))
-            {
+            if ($this->request->request('keyField')) {
                 return $this->selectpage();
             }
-            $filter =  $this->request->get("filter", '');
-            $filter = $filter? json_decode($filter, true) : [];
+            $filter = $this->request->get("filter", '');
+            $filter = $filter ? json_decode($filter, true) : [];
+            if (isset($filter['stuid']) && $filter['stuid']) {
+                $filter['stuid'] = Studio::where(['title' => $filter['stuid']])->value('id');
+            }
 
-                if(isset($filter['stuid']) && $filter['stuid']){
-                    global $_GET;
-                    $filter['stuid'] = Studio::where(['title'=>$filter['stuid']])->value('id');
-                    $_GET['filter'] = urlencode(json_encode($filter, 256));
-                }
-                
-                var_dump($_GET);
-                var_dump($this->request->get('filter'));
-            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-
-            $total = $this->model                    
-                    ->where($where)
-                    ->order($sort, $order)
-                    ->count();
-
-            $list = $this->model                   
-                    ->where($where)
-                    ->order($sort, $order)
-                    ->limit($offset, $limit)
-                    ->select();
+            list($where, $sort, $order, $offset, $limit) = $this->buildparamsFilter('', '', $filter);
+
+            $total = $this->model
+                ->where($where)
+                ->order($sort, $order)
+                ->count();
+
+            var_dump($this->model->getLastSql());
+            $list = $this->model
+                ->where($where)
+                ->order($sort, $order)
+                ->limit($offset, $limit)
+                ->select();
 
             foreach ($list as &$row) {
-                $row['catid']=get_table_column('goods_cats',$row['catid'],'name');
-                $row['stuid']=get_table_column('studio',$row['stuid'],'title');
-                $row['userid']=get_user_info($row['userid'],'username','mobile');
+                $row['catid'] = get_table_column('goods_cats', $row['catid'], 'name');
+                $row['stuid'] = get_table_column('studio', $row['stuid'], 'title');
+                $row['userid'] = get_user_info($row['userid'], 'username', 'mobile');
             }
             $result = array("total" => $total, "rows" => $list);
 
@@ -90,6 +84,7 @@ class Goods extends Backend
         }
         return $this->view->fetch();
     }
+
     function add()
     {
         if ($this->request->isPost()) {
@@ -99,7 +94,7 @@ class Goods extends Backend
                 if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                     $params[$this->dataLimitField] = $this->auth->id;
                 }
-                $params['price1']=$params['price'];
+                $params['price1'] = $params['price'];
 
                 $result = false;
                 Db::startTrans();
@@ -112,7 +107,7 @@ class Goods extends Backend
                     }
                     $result = $this->model->allowField(true)->insertGetId($params);
                     Db::commit();
-                    $this->model->where(['id'=> $result])->update(['description'=> 'G'.$result]);
+                    $this->model->where(['id' => $result])->update(['description' => 'G' . $result]);
                 } catch (ValidateException $e) {
                     Db::rollback();
                     $this->error($e->getMessage());
@@ -133,17 +128,17 @@ class Goods extends Backend
         }
         return parent::add();
     }
+
     public function edit($ids = NULL)
     {
         $row = $this->model->get($ids);
-        if (!$row)
-        {
+        if (!$row) {
             $this->error(__('No Results were found'));
         }
         if ($this->request->isPost()) {
             $params = $this->request->post("row/a");
             if ($params) {
-                $params['price1']=$params['price'];
+                $params['price1'] = $params['price'];
                 $params = $this->preExcludeFields($params);
                 $result = false;
                 Db::startTrans();
@@ -176,25 +171,25 @@ class Goods extends Backend
         }
         return parent::edit($ids);
     }
-	function multi($ids = '')
-	{
-		 $ids = $ids ? $ids : $this->request->param("ids");
-         if ($ids) {
-           $params=input('params');
-           $arrs=explode('=', $params);
-           $data[$arrs[0]]=$arrs[1];
-           $res=$this->model->where(['id'=>$ids])->update ($data);
-           if($res)
-           {
-           		$this->success('操作成功');
-           }else{
-           		$this->error('操作失败');
-           }
-        }else{
-        	$this->error('请选择要操作的数据');
+
+    function multi($ids = '')
+    {
+        $ids = $ids ? $ids : $this->request->param("ids");
+        if ($ids) {
+            $params = input('params');
+            $arrs = explode('=', $params);
+            $data[$arrs[0]] = $arrs[1];
+            $res = $this->model->where(['id' => $ids])->update($data);
+            if ($res) {
+                $this->success('操作成功');
+            } else {
+                $this->error('操作失败');
+            }
+        } else {
+            $this->error('请选择要操作的数据');
         }
-		return parent::multi($ids);
-	}
+        return parent::multi($ids);
+    }
 
 
 }

+ 140 - 0
application/common/controller/Backend.php

@@ -383,6 +383,146 @@ class Backend extends Controller
     }
 
     /**
+     * 生成查询所需要的条件,排序方式
+     * @param mixed   $searchfields   快速查询的字段
+     * @param boolean $relationSearch 是否关联查询
+     * @return array
+     */
+    protected function buildparamsFilter($searchfields = null, $relationSearch = null, $filters=[])
+    {
+        $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 ? array_merge($filter, $filters) : [];
+        $where = [];
+        $tableName = '';
+        if ($relationSearch) {
+            if (!empty($this->model)) {
+                $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
+                $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];
+    }
+
+    /**
      * 获取数据限制的管理员ID
      * 禁用数据限制时返回的是null
      * @return mixed