2 Commits 5f39b7ac98 ... b2c4235ee8

Tác giả SHA1 Thông báo Ngày
  Home b2c4235ee8 8-6分页获取分类列表 4 năm trước cách đây
  Home 0786f7f71a 8-5新增分类优化 4 năm trước cách đây

+ 22 - 2
app/admin/controller/Category.php

@@ -15,7 +15,19 @@ class Category extends AdminBase
      */
     public function index()
     {
-        return View::fetch();
+        $pid = input('param.id', 0, 'intval');
+        $data = [
+            "pid" => $pid
+        ];
+        try {
+            $categorys = (new CategoryBusiness())->getLists($data, 5);
+        } catch (\Exception $e) {
+            $categorys = [];
+        }
+
+        return View::fetch('', [
+            'categorys' => $categorys
+        ]);
     }
 
     /**
@@ -24,7 +36,15 @@ class Category extends AdminBase
      */
     public function add()
     {
-        return View::fetch();
+        try {
+            $categorys = (new CategoryBusiness)->getNormalCategory();
+        } catch (\Exception $e) {
+            $categorys = [];
+        }
+
+        return View::fetch('', [
+            'categorys' => json_encode($categorys)
+        ]);
     }
 
     /**

+ 8 - 11
app/admin/view/category/add.html

@@ -47,16 +47,8 @@
         function _classif(res=[]) {
             // res 分类数据 先期模拟
             let temps = '<option value="0">-| 顶级菜单</option>';
-            var data = [
-                {id: 1, name: "办公管理", pid: 0},
-                {id: 2, name: "请假申请", pid: 1},
-                {id: 3, name: "出差申请", pid: 1},
-                {id: 4, name: "请假记录", pid: 2},
-                {id: 5, name: "系统设置", pid: 0},
-                {id: 6, name: "权限管理", pid: 5},
-                {id: 7, name: "用户角色", pid: 6},
-                {id: 8, name: "菜单设置", pid: 6},
-            ];
+
+            var data = {$categorys|raw}
 
             let toTrees = toTree(data);
             for (let item of toTrees) {
@@ -98,7 +90,12 @@
                 success: (res) => {
                    // todo
                     if (res.status === 1) {
-
+                        layer.msg("新增成功",function () {
+                            window.location = "{:url('index')}";
+                        })
+                    } else {
+                        layer.msg(res.message);
+                        return false;
                     }
                 }
             })

+ 36 - 56
app/admin/view/category/index.html

@@ -30,24 +30,30 @@
 <body>
 <div class="layuimini-container">
     <div class="layuimini-main">
-        <a href="{:url('add')}"><button type="button" class="layui-btn add">添 加</button></a>
+        <a href="{:url('add')}">
+            <button type="button" class="layui-btn add">添 加</button>
+        </a>
 
         <div class="layui-form" style="margin-top: 20px;">
             <table class="layui-table">
                 <colgroup>
-                    <col width="90">
-                    <col width="110">
-                    <col width="800">
+                    <col width="40">
+                    <col width="60">
+                    <col width="100">
                     <col width="130">
+                    <col width="130">
+                    <col width="70">
                     <col width="70">
-                    <col width="85">
+                    <col width="185">
                 </colgroup>
                 <thead>
                 <tr>
                     <th>id</th>
-                    <th>一级分类</th>
-                    <th class="text-center">二 / 三级分类</th>
+                    <th>标题</th>
+                    <th>排序</th>
                     <th class="text-center">创建时间</th>
+                    <th class="text-center">操作时间</th>
+                    <th class="text-center">操作人</th>
                     <th class="text-center">状 态</th>
                     <th>操作管理</th>
                 </tr>
@@ -56,56 +62,30 @@
                 <!--一级类目循环-->
                 <tr>
                     <td>
-                        <div class="layui-input-inline">
-                          <input type="text" value="1" data-id="1" class="changeSort layui-input">
-                        </div>
+                        1
+                    </td>
+                    <td>
+                        abcd
                     </td>
                     <td>
                         <div class="layui-input-inline">
-                            <input type="text" name="p0" autocomplete="off" class="layui-input" value="一级类目"
-                                   onchange="editCls(this,id=1,ptype=1)">
+                            <input type="text" value="1" data-id="1" class="changeSort layui-input">
                         </div>
                     </td>
                     <td>
-                        <!--二级类目循环 start-->
-                        <div class="layui-input-block" style="margin-left: 0;">
-                            <button data-ptype="2" type="button"
-                                    class="layui-btn layui-btn-primary layui-btn-sm del-child"
-                                    style="border: none;"><i class="layui-icon"></i></button>
-                            <div class="layui-input-inline">
-                                <input type="text" name="p1" style="background: #ffb800;" autocomplete="off"
-                                       class="inoutCls" onchange="editCls(this,id=1,ptype=2)" value="二级类目">
-                            </div>
-
-                            <!-- 三级类目循环 start-->
-                            <div class="layui-input-inline">
-                                --<input type="text" name="p1" autocomplete="off" class="inoutCls"
-                                         onchange="editCls(this,id=1,ptype=3)" value="1。1">
-                            </div>
-                            <div class="layui-input-inline">
-                                --<input type="text" name="p1" autocomplete="off" class="inoutCls"
-                                         onchange="editCls(this,id=1,ptype=3)" value="1。2">
-                            </div>
-                            <div class="layui-input-inline">
-                                --<input type="text" name="p1" autocomplete="off" class="inoutCls"
-                                         onchange="editCls(this,id=1,ptype=3)" value="1。3">
-                            </div>
-                            <!-- 三级类目循环 end-->
-                        </div>
-
-                        <!--二级类目循环 end-->
+                        创建时间
                     </td>
-                    <td>2019-11-20 10:40</td>
-
-                    <td data-id="1"><input type="checkbox" checked="" name="status" lay-skin="switch"
-                                           lay-filter="switchTest"
-                                           lay-text="ON|OFF">
+                    <td>
+                        更新时间
+                    </td>
+                    <td>
+                        singwa
+                    </td>
+                    <td data-id="1"><input type="checkbox" checked="" 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="$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="$id">删除</a>
+                        <a href="{:url('index', ['pid'=>1])}">获取子栏目</a>
                     </td>
                 </tr>
                 <!--一级类目循环 end-->
@@ -161,8 +141,8 @@
             if (!name && type == 3) { // 演示 应该放到修改回调中  进行处理
                 return $(that).parent().remove()
             }
-            let   url = '{:url("admin/edit")}?id=' + id + '&name=' + name
-            layObj.get(url,  (res) =>{
+            let url = '{:url("admin/edit")}?id=' + id + '&name=' + name
+            layObj.get(url, (res) => {
                 if (name && res) {
                     $(that).val(name)
                 }
@@ -188,24 +168,24 @@
                 msg = '二';
             }
             layObj.box(`是否删除${msg}级分类`, () => {
-                let   url = '{:url("admin/del")}?id=' + id
-                layObj.get(url,  (res) =>{
+                let url = '{:url("admin/del")}?id=' + id
+                layObj.get(url, (res) => {
                     $(this).parent().remove()
                 })
 
             })
         })
 
-        $('.changeSort').on('change',function () {
+        $('.changeSort').on('change', function () {
             let id = $(this).attr('data-id');
             let val = $(this).val();
 
-            if(!val){
+            if (!val) {
                 return;
             }
-            let url = '{:url("admin/sort")}?id=' + id + '&sort='+val;
+            let url = '{:url("admin/sort")}?id=' + id + '&sort=' + val;
             // let url = 'http:www.baidu.com';
-            layObj.get(url,function (res) {
+            layObj.get(url, function (res) {
                 console.log(res, 'sort');
             })
 

+ 35 - 1
app/common/business/Category.php

@@ -25,7 +25,7 @@ class Category
         $data['status'] = config('status.mysql.table_normal');
 
         $name = $data['name'];
-        $count = $this->categoryObj->where('name',$name)->count();
+        $count = $this->categoryObj->where('name', $name)->count();
         if ($count > 0) {
             throw new \think\Exception('分类名已经存在');
         }
@@ -37,4 +37,38 @@ class Category
         }
         return $this->categoryObj->getLastInsID();
     }
+
+    /**
+     * 获取正常状态的分类
+     * @return array|\think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getNormalCategory()
+    {
+        $field = "id, name, pid";
+        $categorys = $this->categoryObj->getNormalCategory($field);
+        if (!$categorys) {
+            return [];
+        }
+        $categorys = $categorys->toArray();
+        return $categorys;
+    }
+
+    /**
+     * 分页获取分类列表
+     * @param $data
+     * @param $num
+     * @return array
+     * @throws \think\db\exception\DbException
+     */
+    public function getLists($data, $num)
+    {
+        $list = $this->categoryObj->getLists($data, $num);
+        if (!$list) {
+            return [];
+        }
+        return $list->toArray();
+    }
 }

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

@@ -14,5 +14,39 @@ class Category extends Model
      */
     protected $autoWriteTimestamp = true;
 
+    /**
+     * 获取正常状态的分类
+     * @param string $field
+     * @return \think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getNormalCategory($field = '*')
+    {
+        $where = [
+            'status' => config('status.mysql.table_normal')
+        ];
+
+        return $this->where($where)->field($field)->select();
+    }
 
+    /**
+     * 分页获取未删除的分类列表
+     * @param $where
+     * @param int $num
+     * @return \think\Paginator
+     * @throws \think\db\exception\DbException
+     */
+    public function getLists($where, $num = 10)
+    {
+        $order = [
+            'listorder' => 'desc',
+            'id' => 'desc',
+        ];
+        return $this->where('status', '<>', config('status.mysql.table_delete'))
+            ->where($where)
+            ->order($order)
+            ->paginate($num);
+    }
 }