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

后台商品管理增加记忆功能,否则每次页面跳转回来就从头开始刷新,
1, 记忆页面标签
2, 记忆每个标签下产品列表的排序字段

TODO: 增加记忆分页页面,目前这个B js框架不支持分页事件回调

joe 4 роки тому
батько
коміт
1fe3c78bc4

+ 2 - 1
app/admin/controller/store/StoreProduct.php

@@ -139,9 +139,10 @@ class StoreProduct extends AuthController
      *
      * @return \think\Response
      */
-    public function create($id = 0)
+    public function create($id = 0, $type=1)
     {
         $this->assign('id', (int)$id);
+        $this->assign('type', intval($type));
         $luckies = get_luckies();
         $this->assign('lucky_id', json_encode($luckies));
         $this->assign('lucky_spec_name', Config::get('activity.lucky_spec_name') );

+ 3 - 2
app/admin/model/store/StoreCategory.php

@@ -88,8 +88,9 @@ class StoreCategory extends BaseModel
 
     /**
      * 分级排序列表
-     * @param null $model
-     * @param int $type
+     * 
+     * @param null $model 为 null 时构造本对象,不为 null 时用于提前构造过滤条件
+     * @param int $type 为 0 时只查找根分类
      * @return array
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\ModelNotFoundException

+ 3 - 2
app/admin/view/store/store_product/create.php

@@ -892,6 +892,7 @@
 </div>
 <script>
     var id = {$id};
+    var type = {$type};
     var lucky_id = {$lucky_id};
     var lucky_spec_name = '{$lucky_spec_name}';
 
@@ -1034,7 +1035,7 @@
                 });
             },
             goBack:function(){
-                location.href = this.U({c:'store.StoreProduct',a:'index'});
+                location.href = this.U({c:'store.StoreProduct',a:'index', q: {type: type}});
             },
             U: function (opt) {
                 var m = opt.m || 'admin', c = opt.c || window.controlle || '', a = opt.a || 'index', q = opt.q || '',
@@ -1584,7 +1585,7 @@
                     layer.confirm(that.id ? '修改成功是否返回产品列表' : '添加成功是否返回产品列表', {
                         btn: ['返回列表',that.id ? '继续修改' : '继续添加'] //按钮
                     }, function(){
-                        location.href = that.U({c:'store.StoreProduct',a:'index'});
+                        location.href = that.U({c:'store.StoreProduct',a:'index', q: {type: type}});
                     }, function(){
                         location.reload();
                     });

+ 31 - 15
app/admin/view/store/store_product/index.php

@@ -2,6 +2,7 @@
 {block name="content"}
 <div class="layui-fluid" style="background: #fff;margin-top: -10px;">
     <div class="layui-tab layui-tab-brief" lay-filter="tab">
+        <!-- Tab 遗漏 3 : 待上架产品 -->
         <ul class="layui-tab-title">
             <li lay-id="list" {eq name='type' value='1'}class="layui-this" {/eq} >
                 <a href="{eq name='type' value='1'}javascript:;{else}{:Url('index',['type'=>1])}{/eq}">出售中商品({$onsale})</a>
@@ -20,6 +21,7 @@
             </li>
         </ul>
     </div>
+    <!-- 过滤操作面板,几个 Tab 完全一致 -->
     <div class="layui-row layui-col-space15"  id="app">
         <div class="layui-col-md12">
             <div class="layui-card">
@@ -61,10 +63,12 @@
         <div class="layui-col-md12">
             <div class="layui-card">
                 <div class="layui-card-body">
+                    <!-- 信息提示栏,可点击 x 关闭 -->
                     <div class="alert alert-info" role="alert">
                         列表[虚拟销量],[库存],[排序]可进行快速修改,双击或者单击进入编辑模式,失去焦点可进行自动保存
                         <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                     </div>
+                    <!-- 操作按钮 -->
                     <div class="layui-btn-container">
                         {switch name='type'}
                             {case value="1"}
@@ -136,11 +140,24 @@
 </div>
 <script src="{__ADMIN_PATH}js/layuiList.js"></script>
 <script>
-    var type=<?=$type?>;
+    // 記憶排序
+    var sortField = {
+        remember: (field) => {
+            localStorage.setItem('product_sort_by' + type, field)
+        },
+        load: () => {
+            return localStorage.getItem('product_sort_by' + type)
+        }
+    }
+
+    var type = <?= $type ?>;
+    var order = sortField.load() || 'id-desc';
+
     //实例化form
     layList.form.render();
     //加载列表
-    layList.tableList('List',"{:Url('product_ist',['type'=>$type])}",function (){
+    var url = layList.Url({c:'store.store_product',a:'product_ist', q:{type: type, order: order}})
+    layList.tableList('List', url, function (){
         var join=new Array();
         switch (parseInt(type)){
             case 1:case 3:case 4:case 5:
@@ -156,7 +173,7 @@
                     {field: 'right', title: '操作',align:'center',toolbar:'#act',width:'14%'},
                 ];
                 break;
-            case 2:
+            case 2: // 仓库中商品 is_show == 0
                 join=[
                     {type:'checkbox'},
                     {field: 'id', title: 'ID', sort: true,event:'id',width:'6%'},
@@ -171,7 +188,7 @@
                     {field: 'right', title: '操作',align:'center',toolbar:'#act',width:'14%'},
                 ];
                 break;
-            case 6:
+            case 6: // 回收站 is_del == 1
                 join=[
                     {field: 'id', title: '商品ID', sort: true,event:'id'},
                     {field: 'image', title: '商品图片',templet:'#image'},
@@ -181,7 +198,7 @@
                     {field: 'stock', title: '库存'},
                     {field: 'sort', title: '排序',edit:'sort'},
                     {field: 'sales', title: '销量',sort: true,event:'sales'},
-//                    {field: 'status', title: '状态',templet:"#checkboxstatus"},
+                    // {field: 'status', title: '状态',templet:"#checkboxstatus"},
                     {field: 'right', title: '操作',align:'center',toolbar:'#act',width:'14%'},
                 ];
                 break;
@@ -242,7 +259,7 @@
                 break;
         }
     });
-    //上下商品
+    //上下商品
     layList.switch('is_show',function (odj,value) {
         if(odj.elem.checked==true){
             layList.baseGet(layList.Url({c:'store.store_product',a:'set_show',p:{is_show:1,id:value}}),function (res) {
@@ -282,7 +299,7 @@
                 $eb.openImage(data.image);
                 break;
             case 'edit':
-                location.href = layList.U({a:'create',q:{id:data.id}});
+                location.href = layList.U({a:'create',q:{id:data.id, type:type}});
                 break;
             case 'attr':
                 $eb.createModalFrame(data.store_name+'-属性',layList.U({a:'attr',q:{id:data.id}}),{h:600,w:800})
@@ -292,13 +309,12 @@
     //排序
     layList.sort(function (obj) {
         var type = obj.type;
-        switch (obj.field){
-            case 'id':
-                layList.reload({order: layList.order(type,'id')},true,null,obj);
-                break;
-            case 'sales':
-                layList.reload({order: layList.order(type,'sales')},true,null,obj);
-                break;
+        
+        if (obj.field === 'id' || obj.field === 'sales') {
+            var order = layList.order(type, obj.field)
+            sortField.remember(order)
+
+            layList.reload({order: order}, true, null, obj)
         }
     });
     //查询
@@ -324,7 +340,7 @@
             }
         }
     };
-    //多选事件绑定
+    //多选事件绑定(也就一個批量上架)
     $('.layui-btn-container').find('button').each(function () {
         var type=$(this).data('type');
         $(this).on('click',function(){

+ 25 - 10
crmeb/traits/CurdControllerTrait.php

@@ -1,9 +1,11 @@
 <?php
+
 /**
  *
  * @author: xaboy<365615158@qq.com>
  * @day: 2017/11/15
  */
+
 namespace crmeb\traits;
 
 use crmeb\services\JsonService;
@@ -11,23 +13,36 @@ use think\Request;
 
 trait CurdControllerTrait
 {
-    /**保存数据库
+    /**
+     * 保存数据库
+     * 
+     * @deprecated: 取消使用/要优化这个更新操作,一次更新操作里执行了 2 次 sql.
+     * 
      * @param $id
      * @param $field
      */
-    public function change_field($id,$field)
+    public function change_field($id, $field)
     {
-        if(!isset($this->bindModel)) return exception('方法不存在!');
-        if(!class_exists($this->bindModel)) return JsonService::fail('操作Model不存在!');
+        if (!isset($this->bindModel)) {
+            return exception('方法不存在!');
+        }
+        if (!class_exists($this->bindModel)) {
+            return JsonService::fail('操作Model不存在!');
+        }
         $model = new $this->bindModel;
         $pk = $model->getPk();
-        if(strtolower($pk) == strtolower($field)) return JsonService::fail('主键不允许修改!');
-        $data = $model->where($pk,$id)->find();
-        if(!$data) JsonService::fail('记录不存在!');
+        if (strtolower($pk) == strtolower($field)) {
+            return JsonService::fail('主键不允许修改!');
+        }
+        $data = $model->where($pk, $id)->find();
+        if (!$data) {
+            JsonService::fail('记录不存在!');
+        }
         $value = app('request')->post($field);
-        if($value === null) return JsonService::fail('请提交需要编辑的数据!');
+        if ($value === null) {
+            return JsonService::fail('请提交需要编辑的数据!');
+        }
         $data->$field = $value;
         return false !== $data->save() ? JsonService::successful('编辑成功!') : JsonService::fail('编辑失败!');
-
     }
-}
+}