Parcourir la source

8-5新增分类优化

Home il y a 4 ans
Parent
commit
0786f7f71a

+ 9 - 1
app/admin/controller/Category.php

@@ -24,7 +24,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;
                     }
                 }
             })

+ 19 - 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,22 @@ 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) {
+            $categorys = [];
+        }
+        $categorys = $categorys->toArray();
+        return $categorys;
+    }
 }

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

@@ -14,5 +14,20 @@ 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();
+    }
 }