Переглянути джерело

报名活动增加活动时间状态,能搜索状态

lizhen 21 годин тому
батько
коміт
cfa015d04f

+ 27 - 13
application/admin/controller/unishop/Product.php

@@ -110,25 +110,39 @@ class Product extends Backend
             if ($this->request->request('keyField')) {
                 return $this->selectpage();
             }
-            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-            $total = $this->model
-                ->where($where)
-                ->count();
+            list($where, $sort, $order, $offset, $limit, $page, $alias, $bind, $out_where) = $this->buildparams_product();
+
+            $where_activestatus = [];
+            if(!empty($out_where)){
+                if($out_where[2] == 'passed'){
+                    $where_activestatus = ['activetime' => ['<',time()]];
+                }else{
+                    $where_activestatus = ['activetime' => ['>',time()]];
+                }
+            }
+//            dump($out_where);
+//            dump($where_activestatus);
 
             $list = $this->model
-                ->with([
-                    'category' => function($query) {
-                        $query->with('parent');
-                    },
-                    'delivery'
-                ])
+
                 ->where($where)
+                ->where($where_activestatus)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
-                ->select();
+//                ->select(false);echo $list;exit;
+                ->paginate();
+
+            $rows = $list->items();
+            $list2 = collection($rows)->toArray();
+
+            foreach($list2 as $key => &$value){
+                $value['activestatus'] = '正常';
+                if($value['activetime'] < time()){
+                    $value['activestatus'] = '已过期';
+                }
+            }
 
-            $list = collection($list)->toArray();
-            $result = array("total" => $total, "rows" => $list);
+            $result = array("total" => $list->total(), "rows" => $list2);
             return json($result);
         }
         return $this->view->fetch();

+ 10 - 1
application/admin/view/unishop/product/index.html

@@ -1,5 +1,14 @@
 <div class="panel panel-default panel-intro">
-    {:build_heading()}
+    <div class="panel-heading">
+        {:build_heading(null,FALSE)}
+        <ul class="nav nav-tabs" data-field="activestatus">
+            <li class="{:$Think.get.status === null ? 'active' : ''}"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
+
+            <li class="{:$Think.get.status === (string)0 ? 'active' : ''}"><a href="#t-0" data-value="normal" data-toggle="tab">正常</a></li>
+            <li class="{:$Think.get.status === (string)1 ? 'active' : ''}"><a href="#t-1" data-value="passed" data-toggle="tab">已过期</a></li>
+
+        </ul>
+    </div>
 
     <div class="panel-body">
         <div id="myTabContent" class="tab-content">

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

@@ -436,6 +436,196 @@ class Backend extends Controller
         };
         return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
     }
+    protected function buildparams_product($searchfields = null, $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/d", 0);
+        $limit = $this->request->get("limit/d", 0);
+        $limit = $limit ?: 999999;
+        //新增自动计算页码
+        $page = $limit ? intval($offset / $limit) + 1 : 1;
+        if ($this->request->has("page")) {
+            $page = $this->request->get("page/d", 1);
+        }
+        $this->request->get([config('paginate.var_page') => $page]);
+        $filter = (array)json_decode($filter, true);
+        $op = (array)json_decode($op, true);
+        $filter = $filter ? $filter : [];
+        $where = [];
+        $alias = [];
+        $bind = [];
+        $name = '';
+        $aliasName = '';
+        if (!empty($this->model) && $relationSearch) {
+            $name = $this->model->getTable();
+            $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
+            $aliasName = $alias[$name] . '.';
+        }
+        $sortArr = explode(',', $sort);
+        foreach ($sortArr as $index => & $item) {
+            $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
+        }
+        unset($item);
+        $sort = implode(',', $sortArr);
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
+        }
+        if ($search) {
+            $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
+            foreach ($searcharr as $k => &$v) {
+                $v = stripos($v, ".") === false ? $aliasName . $v : $v;
+            }
+            unset($v);
+            $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
+        }
+        $index = 0;
+        foreach ($filter as $k => $v) {
+            if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
+                continue;
+            }
+            $sym = $op[$k] ?? '=';
+            if (stripos($k, ".") === false) {
+                $k = $aliasName . $k;
+            }
+            $v = !is_array($v) ? trim($v) : $v;
+            $sym = strtoupper($op[$k] ?? $sym);
+            //null和空字符串特殊处理
+            if (!is_array($v)) {
+                if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
+                    $sym = strtoupper($v);
+                }
+                if (in_array($v, ['""', "''"])) {
+                    $v = '';
+                    $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':
+                    $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
+                    $findArr = array_values($v);
+                    foreach ($findArr as $idx => $item) {
+                        $bindName = "item_" . $index . "_" . $idx;
+                        $bind[$bindName] = $item;
+                        $where[] = "FIND_IN_SET(:{$bindName}, `" . 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, function ($v) {
+                        return $v != '' && $v !== false && $v !== null;
+                    })) {
+                        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];
+                    }
+                    $tableArr = explode('.', $k);
+                    if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias)
+                        && !empty($this->model) && $this->relationSearch) {
+                        //修复关联模型下时间无法搜索的BUG
+                        $relation = Loader::parseName($tableArr[0], 1, false);
+                        $alias[$this->model->$relation()->getTable()] = $tableArr[0];
+                    }
+                    $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
+                    break;
+                case 'NULL':
+                case 'IS NULL':
+                case 'NOT NULL':
+                case 'IS NOT NULL':
+                    $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
+                    break;
+                default:
+                    break;
+            }
+            $index++;
+        }
+        if (!empty($this->model)) {
+            $this->model->alias($alias);
+        }
+        $model = $this->model;
+
+//        dump($where);
+        $out_where = [];
+        foreach($where as $key => $val) {
+            if($val[0] == 'activestatus'){
+                $out_where = $val;
+                unset($where[$key]);
+            }
+        }
+//        dump($out_where);
+//        dump($where);
+//        exit;
+
+        $where = function ($query) use ($where, $alias, $bind, &$model) {
+            if (!empty($model)) {
+                $model->alias($alias);
+                $model->bind($bind);
+            }
+            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, $page, $alias, $bind, $out_where];
+    }
 
     /**
      * 获取数据限制的管理员ID

+ 8 - 6
public/assets/js/backend/unishop/product.js

@@ -620,22 +620,24 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload', 'vue'], fun
                         {
                             field: 'image',
                             title: __('Image'),
+                            operate: false,
                             events: Table.api.events.image,
                             formatter: Table.api.formatter.image
                         },
                         {field: 'activetime', title: __('Activetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
-                        {
+                        {field: 'activestatus', title: '活动状态',searchList: {"normal":'正常',"passed":'已过期'}, formatter: Table.api.formatter.status},
+                        /*{
                             field: 'images',
                             title: __('Images'),
                             events: Table.api.events.image,
                             formatter: Table.api.formatter.images,
                             visible: false
-                        },
-                        {field: 'sales_price', title: __('Lower price')},
-                        {field: 'stock', title: __('Stock')},
+                        },*/
+                        {field: 'sales_price',operate: false, title: __('Lower price')},
+                        {field: 'stock', title: __('Stock'),operate: false},
 //                        {field: 'look', title: __('Look')},
 //                        {field: 'sales', title: __('Sales')},
-                        {field: 'real_sales', title: __('Real sales')},
+                        {field: 'real_sales', title: __('Real sales'),operate: false},
 //                        {field: 'no_buy_yet', title: __('No buy yet')},
 //                        {field: 'real_look', title: __('Real look')},
 //                        {field: 'delivery_id', title: __('Delivery_id'), visible: false},
@@ -656,7 +658,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload', 'vue'], fun
                             formatter: Table.api.formatter.datetime
                         },
                         {field: 'weigh', title: __('Weigh')},
-                        {field: 'switch', title: __('Switch'), formatter: Table.api.formatter.toggle},
+                        {field: 'switch', title: __('Switch'),operate: false, formatter: Table.api.formatter.toggle},
                         {
                             field: 'operate',
                             title: __('Operate'),