소스 검색

8-3新增分类逻辑完成

Home 4 년 전
부모
커밋
9ab2bba2f7

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

@@ -3,7 +3,8 @@
 
 namespace app\admin\controller;
 
-
+use app\admin\validate\Category as CategoryValidate;
+use app\common\business\Category as CategoryBusiness;
 use think\facade\View;
 
 class Category extends AdminBase
@@ -25,4 +26,31 @@ class Category extends AdminBase
     {
         return View::fetch();
     }
+
+    /**
+     * 新增
+     * @return \think\response\Json
+     */
+    public function save()
+    {
+        $pid = input("param.pid", 0, "intval");
+        $name = input("param.name", '', "trim");
+
+        // 参数校验
+        $data = [
+            'pid' => $pid,
+            'name' => $name
+        ];
+        $validate = new CategoryValidate();
+        if (!$validate->check($data)) {
+            return show(config('status.error'), $validate->getError());
+        }
+
+        try {
+            $result = (new CategoryBusiness)->add($data);
+        } catch (\Exception $e) {
+            return show(config('status.error'), $e->getMessage());
+        }
+        return show(config('status.success'), 'OK');
+    }
 }

+ 27 - 0
app/admin/validate/Category.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/1/4 11:17
+ */
+
+declare(strict_types=1);
+
+namespace app\admin\validate;
+
+use think\Validate;
+
+class Category extends Validate
+{
+    protected $rule = [
+        'name' => 'require',
+        'pid' => 'require',
+    ];
+
+    protected $message = [
+        'name' => '分类名必须',
+        'pid' => '分类ID必须',
+    ];
+}

+ 10 - 4
app/admin/view/category/add.html

@@ -19,7 +19,7 @@
         <div class="layui-inline">
             <label class="layui-form-label" style="width: 200px;">父级分类</label>
             <div class="layui-input-inline">
-                <select name="parent" id="classif"></select>
+                <select name="pid" id="classif"></select>
             </div>
         </div>
     </div>
@@ -86,14 +86,20 @@
         //监听提交
         form.on('submit(demo1)', function (data) {
             console.log(data.field, '最终的提交信息')
+            data = data.field;
             let url = '';
             // layObj.post(url,data,function (res) {
             //
             // });
-            $ajax({
-                url: '{:url("save")}?val=' + data.field,
+            $.ajax({
+                type:"POST",
+                data:data,
+                url: '{:url("save")}',
                 success: (res) => {
-                   setTimeout('window.location.reload()',1000);
+                   // todo
+                    if (res.status === 1) {
+
+                    }
                 }
             })
 

+ 4 - 4
app/admin/view/category/index.html

@@ -30,7 +30,7 @@
 <body>
 <div class="layuimini-container">
     <div class="layuimini-main">
-        <button type="button" class="layui-btn add">添 加</button>
+        <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">
@@ -133,9 +133,9 @@
 
 
         // 添加 分类
-        $('.add').on('click', function () {
-            layObj.dialog("{:url('add')}")
-        });
+        // $('.add').on('click', function () {
+        //     layObj.dialog("{:url('add')}")
+        // });
 
         //监听状态 更改
         form.on('switch(switchTest)', function (obj) {

+ 28 - 0
app/common/business/Category.php

@@ -0,0 +1,28 @@
+<?php
+declare(strict_types=1);
+
+namespace app\common\business;
+
+use app\common\model\mysql\Category as CategoryModel;
+
+class Category
+{
+    public $categoryObj = null;
+
+    public function __construct()
+    {
+        $this->categoryObj = new CategoryModel();
+    }
+
+    /**
+     * 新增一条分类
+     * @param array $data
+     * @return int
+     */
+    public function add(array $data)
+    {
+        $data['status'] = config('status.mysql.table_normal');
+        $this->categoryObj->save($data);
+        return $this->categoryObj->getLastInsID();
+    }
+}

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

@@ -0,0 +1,18 @@
+<?php
+
+
+namespace app\common\model\mysql;
+
+
+use think\Model;
+
+class Category extends Model
+{
+    /**
+     * 自动生成写入时间
+     * @var bool
+     */
+    protected $autoWriteTimestamp = true;
+
+
+}