Browse Source

8-9分类列表排序功能开发与优化

Home 4 years ago
parent
commit
d8d436f94e

+ 27 - 3
app/admin/controller/Category.php

@@ -20,7 +20,7 @@ class Category extends AdminBase
             "pid" => $pid
         ];
         try {
-            $categorys = (new CategoryBusiness())->getLists($data, 1);
+            $categorys = (new CategoryBusiness())->getLists($data, 5);
         } catch (\Exception $e) {
             $categorys = [];
         }
@@ -67,10 +67,34 @@ class Category extends AdminBase
         }
 
         try {
-            $result = (new CategoryBusiness)->add($data);
+            $id = (new CategoryBusiness)->add($data);
         } catch (\Exception $e) {
             return show(config('status.error'), $e->getMessage());
         }
-        return show(config('status.success'), 'OK');
+        if ($id) {
+            return show(config('status.success'), 'OK');
+        }
+        return show(config('status.error'), '新增分类失败');
+    }
+
+    public function listorder()
+    {
+        $id = input('param.id', 0, 'intval');
+        $listorder = input('param.listorder', 0, 'intval');
+
+        if (!$id) {
+            return show(config('status.error'), '参数错误');
+        }
+
+        try {
+            $res = (new CategoryBusiness())->listorder($id, $listorder);
+        } catch (\Exception $e) {
+            return show(config('status.error'), $e->getMessage());
+        }
+        if ($res) {
+            return show(config('status.success'), '排序成功');
+        } else {
+            return show(config('status.error'), '排序失败');
+        }
     }
 }

+ 27 - 11
app/admin/view/category/index.html

@@ -70,7 +70,8 @@
                     </td>
                     <td>
                         <div class="layui-input-inline">
-                            <input type="text" value="{$vo.listorder}" data-id="{$vo.id}" class="changeSort layui-input">
+                            <input type="text" value="{$vo.listorder}" data-id="{$vo.id}"
+                                   class="changeSort layui-input">
                         </div>
                     </td>
                     <td>
@@ -82,10 +83,12 @@
                     <td>
                         {$vo.operate_user}
                     </td>
-                    <td data-id="1"><input type="checkbox" {if $vo.status == 1} checked {/if} name="status" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
+                    <td data-id="1"><input type="checkbox" {if $vo.status== 1} checked {/if} name="status"
+                        lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
                     </td>
                     <td>
-                        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete del-child" data-ptype="1" lay-event="delete" data-id="{$vo.id}">删除</a>
+                        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete del-child" data-ptype="1"
+                           lay-event="delete" data-id="{$vo.id}">删除</a>
                         <a href="{:url('index', ['pid'=>$vo.id])}">获取子栏目</a>
                     </td>
                 </tr>
@@ -94,15 +97,24 @@
                 </tbody>
             </table>
         </div>
-<!--        <div>{$categorys.render|raw}</div>-->
+        <!--        <div>{$categorys.render|raw}</div>-->
         <div id="pages"></div>
     </div>
 
 </div>
 <style>
-    .pagination li{display: inline;padding-left: 10px;}
-    .pagination .active{color: red;}
-    .pagination .disabled{color: #DDD;}
+    .pagination li {
+        display: inline;
+        padding-left: 10px;
+    }
+
+    .pagination .active {
+        color: red;
+    }
+
+    .pagination .disabled {
+        color: #DDD;
+    }
 </style>
 <script src="{__STATIC_PATH}admin/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
 <script src="{__STATIC_PATH}admin/lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
@@ -118,8 +130,8 @@
             , count: {$categorys.total}
             , theme: '#FFB800'
             , limit: {$categorys.per_page}
-            , curr : {$categorys.current_page}
-            , jump : function (obj, first) {
+            , curr: {$categorys.current_page}
+            , jump: function (obj, first) {
                 if (!first) {
                     location.href = "?page=" + obj.curr
                 }
@@ -198,10 +210,14 @@
             if (!val) {
                 return;
             }
-            let url = '{:url("admin/sort")}?id=' + id + '&sort=' + val;
+            let url = '{:url("category/listorder")}?id=' + id + '&listorder=' + val;
             // let url = 'http:www.baidu.com';
             layObj.get(url, function (res) {
-                console.log(res, 'sort');
+                if (res.status == 1) {
+                    window.location.reload();
+                } else {
+                    layer.msg("排序失败")
+                }
             })
 
         })

+ 52 - 7
app/common/business/Category.php

@@ -7,11 +7,11 @@ use app\common\model\mysql\Category as CategoryModel;
 
 class Category
 {
-    public $categoryObj = null;
+    public $model = null;
 
     public function __construct()
     {
-        $this->categoryObj = new CategoryModel();
+        $this->model = new CategoryModel();
     }
 
     /**
@@ -25,17 +25,17 @@ class Category
         $data['status'] = config('status.mysql.table_normal');
 
         $name = $data['name'];
-        $count = $this->categoryObj->where('name', $name)->count();
+        $count = $this->model->where('name', $name)->count();
         if ($count > 0) {
             throw new \think\Exception('分类名已经存在');
         }
 
         try {
-            $this->categoryObj->save($data);
+            $this->model->save($data);
         } catch (\Exception $e) {
             throw new \think\Exception('服务内部异常');
         }
-        return $this->categoryObj->getLastInsID();
+        return $this->model->id;
     }
 
     /**
@@ -48,7 +48,7 @@ class Category
     public function getNormalCategory()
     {
         $field = "id, name, pid";
-        $categorys = $this->categoryObj->getNormalCategory($field);
+        $categorys = $this->model->getNormalCategory($field);
         if (!$categorys) {
             return [];
         }
@@ -65,7 +65,7 @@ class Category
      */
     public function getLists($data, $num)
     {
-        $list = $this->categoryObj->getLists($data, $num);
+        $list = $this->model->getLists($data, $num);
         if (!$list) {
             return [];
         }
@@ -73,4 +73,49 @@ class Category
         $result['render'] = $list->render();
         return $result;
     }
+
+    /**
+     * 根据id获取某一条记录
+     * @param $id
+     * @return array|\think\Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getById($id)
+    {
+        $result = $this->model->find($id);
+        if (empty($result)) {
+            return [];
+        }
+        $result = $result->toArray();
+        return $result;
+    }
+
+    /**
+     * 修改分类表排序的值
+     * @param $id
+     * @param $listorder
+     * @return bool
+     * @throws \think\Exception
+     */
+    public function listorder($id, $listorder)
+    {
+        $res = $this->getById($id);
+        if (!$res) {
+            throw new \think\Exception('不存在该条记录');
+        }
+        $data = [
+            'listorder' => $listorder
+        ];
+
+        try {
+            $res = $this->model->updateById($id, $data);
+        } catch (\Exception $e) {
+            // 记录日志
+            return false;
+        }
+
+        return $res;
+    }
 }

+ 12 - 0
app/common/model/mysql/Category.php

@@ -49,4 +49,16 @@ class Category extends Model
             ->order($order)
             ->paginate($num);
     }
+
+    /**
+     * 根据id更新库里面的数据
+     * @param $id
+     * @param $data
+     * @return bool
+     */
+    public function updateById($id, $data)
+    {
+        $data['update_time'] = time();
+        return $this->where(['id' => $id])->save($data);
+    }
 }